Loop() question

jbellavance:
And so does this:

const int ledPin =  13;

int ledState = LOW;
unsigned long previousMillis;
const long interval = 1000;
char receivedChar;
boolean newData = false;
boolean blinking =  false;

void setup() {
 Serial.begin(9600);
 Serial.println("");
 pinMode(ledPin, OUTPUT);
}

void loop() {
 recvOneChar();
 showNewData();
 if (blinking) blink();
}
void blink() {
 unsigned long currentMillis = millis();
 if (currentMillis - previousMillis >= interval) {
   previousMillis = currentMillis;
   if (ledState == LOW) {
     ledState = HIGH;
   } else {
     ledState = LOW;
   }
   digitalWrite(ledPin, ledState);
 }
}

void recvOneChar() {
 if (Serial.available() > 0) {
   receivedChar = Serial.read();
   newData = true;
 }
}

void showNewData() {
 if (newData == true) {
   Serial.println(receivedChar);
   newData = false;
   if (receivedChar == 'q') blinking = true;
 }
}

Yes

Set up your serial monitor to 'no line ending' characters.
. . .
A lower case q will turn on the LED (flashes), any other character will turn it off.

Set up your serial monitor to 'no line ending' characters.
. . .
A lower case q will turn on the LED (flashes), any other character will turn it off.

Not with my code. It will blink forever. That's what was asked in post #5 :wink:

Jacques

I was commenting on your "And so does this:" after my post.

Agree on what the OP asked for.

Hard to believe the OP would want to blink forever, but different stokes.

.

I wonder if the OP is ok!

The OP is blinking gone.

.

OP is fighting with blinking :slight_smile:
Thank you guys VERY MUCH, now I have new 'problem', I will post it later, about upgrading this code.

If you don't blink, you get dry eyes :cry:

You're right, eyes never been drier after hours and hours of trying to resolve this... :o

So, here is the code...

char x;
void setup() {
  Serial.begin(9600);
  for (int i = 0; i <= 3; i++)
  {
    pinMode(10, OUTPUT);
  }
}

void loop() {
  if ( Serial.available() )
  {
    x = Serial.read(); 
  }
  if ( x == 'p' )
  {
    digitalWrite (10, HIGH);
    delay(500);
    digitalWrite (10, LOW);
    delay(500);
  }
  if ( x == 'h' )
  {
     digitalWrite (10, LOW);
  }
}

I'm trying to understand why when I'm using " if (Serial.available()) " I can get LED blink forever (until I send 'h' in this case), but when I'm using while instead of if, LED go through blinks just one time and stops...

I'm trying to understand why when I'm using " if (Serial.available()) " I can get LED blink forever (until I send 'h' in this case), but when I'm using while instead of if, LED go through blinks just one time and stops...

I have this feeling that it's the other way round.

Well...

Suppose I tell you:
While you are awake, blink your eyes,
you will probably see yourself blinking your eyes all day.

And suppose I tell you:
If you see the flag rise, blink your eyes.
Now, how many times do you see yourself blink?

Jacques

If you want blink to stop... why don't you just add:

if (receivedChar == 'q') blinking = true;
if (receivedChar == 'h') { blinking = false; digitalWrite(ledPin, LOW); }  //<<<This

to the code I sent you in post #19?

Jacques

Using delay() in your code will eventually bite you in the *ss (donkey).

Either do what Jacques suggests or use the code posted way back in #14.

.