Arduino program help needed (Serial LED FLASH, NO Delay)

  • Thread starter Deleted User
  • Start date
  • Views 755
  • Replies 0
D

Deleted User

Guest
OP
Hi all, hopefully someone can help me with this.

I recently got an Arduino, and have been having some fun playing around with it, but I cannot for the life of me understand, or get, Millis() working with Serial commands.

Here's what I'm trying to do:

I want to send a letter/number over serial usb to the ARduino, in order to flash an LED. However, what I have in mind requires as little lag as possible due to timing, so I cannot use Delay(). Instead, I must use Millis(). The flash needs to be fast, but it can only be 1 single flash per each time I send a command, it cannot be a "toggle" command.

Where I've come to the problem, is that I can get it to turn on the led, but only after sending my command twice. Furthermore, It does not automatically turn off, i, again, must send the same command, twice, which is exceptionally odd.

If someone is willing to tell/help me in whatever way possible, I'd greatly appreciate it, as the Arduino forums haven't been of any help, and it's been about 2 days worth of googling now and I'm getting quite fedup.

Below is my code, but its only 1 of the variants I've tried.

Code:
const int ledPin = 3;
const int interval = 250;
unsigned long startMillis;
unsigned long currentMillis;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  while (!Serial);
}

void loop(){
  startMillis = millis();
  int state = Serial.parseInt();
 
  if (Serial.available()){
   
      if (state == 2) {
      digitalWrite(3, HIGH);
      currentMillis = millis();
          if(currentMillis - startMillis >= interval) {
          digitalWrite(3, LOW);
          startMillis = currentMillis;
      }
    }
  }
}

Thanks in advanced for any help! I seriously appreciate it, brain is fried at this point!
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • JuanMena @ JuanMena:
    Will you give me mouth to mouth oxygen if my throat closes?
  • K3N1 @ K3N1:
    Nah the air can do that
  • K3N1 @ K3N1:
    Ask @x65943 he's trained for that stuff
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
    A @ abraarukuk: hi guys