Go Down

Topic: LED stay on without using delay- Solved! (Read 2799 times) previous topic - next topic

AWOL

What stops "incomingByte" being an 'H' next time through "loop()" ?

I think you should be acting on "incomingByte" when you read it, not every time through "loop()"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


AWOL

Code: [Select]
void loop() // run over and over
{
  unsigned long newtime = millis();
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = (char)Serial.read();
    Serial.println(incomingByte);
  }

  // if it's a capital H (ASCII 72), turn on the LED:
  if (incomingByte == 'H')
  {
(assuming this is your current code)
Let's assume that you receive an 'H'.
So, you read it it and assign that value to "incomingByte".
Then you test "incomingByte" to see if it is an 'H', it is so you perform some actions, fall out the bottom of "loop()", see if there's any serial characters to read (there aren't) so you test "incomingByte" to see if it is an 'H'.

It still is, so you perform the actions again.
Move the actions inside the "if" condition that reads "incomingByte"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

1. Thanks. That was a good tip, AWOL.
I deleted the bracket } at the end of the incomingByte
reading part in cyclegadgets line 40:
Code: [Select]
void loop() // run over and over
{
  unsigned long newtime = millis();
  // see if there's incoming Serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the Serial buffer:
    incomingByte = (char)Serial.read();
    Serial.println(incomingByte);
  }

  // if it's a capital H (ASCII 72), turn on the LED:
  if (incomingByte == 'H')

to
Code: [Select]
void loop() // run over and over
{
  unsigned long newtime = millis();
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = (char)Serial.read();
    Serial.println(incomingByte);
 
 
  // if it's a capital H (ASCII 72), turn on the LED:
  if (incomingByte == 'H')

thus rendering reading the 'H' as a dependency for all the actions following it,
including it inside of the previous "if", as you wrote.
Now that was not getting rid of the blinking/flickering of the ledPin 13.
2. But also I spotted that we wrote all the time in line 45:
Code: [Select]
TimerledPin = millis(),
when it should have been
Code: [Select]
TimerledPin = newtime,
like we used "newtime" correctly further down at the ButtonLed,
that already worked fine with the new 'avoid delay' commands.
That solved the flickering problem and now it works!
(By the way, I adjusted it to 1000UL (500UL too short and the previous 4000UL too long)
and the system is responsive, calm and quick as a dream.
I think the topic how to have an "LED staying on without delay"
with more than one LED and "if" dependencies from Serial input
has been solved.
Thanks to you!
I will now again put the new, fast delay-free stuff into my
combined server-receiver sketch for the communicating
radio machines (inside jewellery brooches) that have
proximity sensing and button listening abilities.
I'll probably post that in my other thread that went
for the combination problems, what do you think?
http://arduino.cc/forum/index.php/topic,110849.0.html

cyclegadget


Thanks Awol for the nice solution, and improvement to the sketch!

  I wasn't seeing the flickering with my on-board led 13 of my Uno. I did see that it was repeating the "if" statement with my serial.prints but, the sketch seemed to work for me. However, I can see now the sketch needed Awol's fix.
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

As I was fearing I'm again stuck combining this receiver code with the server code
and get the machines working as equals.
I have posted this in the before mentioned topic, if you want to take a look, cyclegadget
and AWOL?
http://arduino.cc/forum/index.php/topic,110849.0.html

Go Up