Pages: 1 2 [3]   Go Down
Author Topic: LED stay on without using delay- Solved!  (Read 1979 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()"
Logged

"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.

England
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't understand.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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"
Logged

"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.

England
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:
TimerledPin = millis(),
when it should have been
Code:
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
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 11
Posts: 1309
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 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.
Logged


England
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: 1 2 [3]   Go Up
Jump to: