Go Down

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

Do you mean in the last one that I posted or in the one from cyclegadget?

AWOL

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

I'm sorry, I don't understand.
I check if XBee is available and it does read an 'L',
when I press the button,
otherwise the thing wouldn't be working.
What I don't understand is why the
new proposal from cyclegadget
doesn't work at all and my previous one
with both avoiding using delay only
works partly.
What are you suggesting?

cyclegadget


I made a mistake and did not see you had incomingbyte defined as int. You are wanting an ascii character and those are variable char.

Put this in your defines area at the top of the sketch.
Code: [Select]
char incomingByte;

In the loop make your xbee.read section look like this:
Code: [Select]
if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = (char)xbee.read();
    }


I had to write your sketch a little differently to test it on my Uno without the X-bee. I used lots of serial.prints to look for my mistake.
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

cyclegadget


So that I could test the sketch I made it a little different and commented out the Xbee setup and used Serial.print and read.

Here is that code. It is not clean but, I didn't want to make a lot of changes and miss the original problem.

Code: [Select]
/*
  By Alexandra da Fonseca-Klein, Digital Jewellery project,
For interactive Jewellery 'proximity sensing hearts'
www.ADFK.co.uk; 2012
2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
If the 2 wearers are within reach (30 feet approx) one LED
comes on on the other wearers piece, if a button is pressed, another LED
lights up. This is the receiver side.
*/
#include <SoftwareSerial.h>        // use these specified set of commands

const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
const int ButtonLed = 5;     // the number of the PushLed pin
const int ledPin =  13;      // the number of the LED pin
int ledState = LOW;
int ledState2 = LOW;
SoftwareSerial xbee (RXPin, TXPin);// defining the new serial name
char incomingByte;    // a variable to read incoming serial data into
unsigned long TimerledPin;
unsigned long TimerButtonLed;

void setup()                // definitions that are always true
{
  Serial.begin(9600);
// xbee.begin(9600);// set the data rate for the SoftwareSerial port
  pinMode(ledPin, OUTPUT);
  pinMode(ButtonLed, OUTPUT);
}

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')
  {
    TimerledPin = millis(),
    digitalWrite(ledPin, HIGH);
    ledState = HIGH;
    Serial.println("I got an H");
   
  }
  if (newtime - TimerledPin >= 4000UL && ledState == HIGH)
  {
    digitalWrite(ledPin,LOW);
    ledState = LOW;
    Serial.println("time is up for TimerledPin");
  }
 
  // if it's a capital L, turn on the LED:
  if (incomingByte == 'L')
  {
    TimerButtonLed = newtime;
    digitalWrite(ButtonLed, HIGH);
    ledState2 = HIGH;
    Serial.println("I got a L");
  }
  if (newtime - ButtonLed >= 4000UL && ledState2 == HIGH)
  {
    digitalWrite(ButtonLed, LOW);
    ledState2 = LOW;
    Serial.println("time is up for ButtonLed");
  }
}


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

I like your comments (...time's up...), I have replaced Serial with Xbee, but still not working.
No LED13 no Button LED.
The code now looks like this:
Code: [Select]
/*
  By Alexandra da Fonseca-Klein, Digital Jewellery project,
For interactive Jewellery 'proximity sensing hearts'
www.ADFK.co.uk; 2012
2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
If the 2 wearers are within reach (30 feet approx) one LED
comes on on the other wearers piece, if a button is pressed, another LED
lights up. This is the receiver side.
*/
#include <SoftwareSerial.h>        // use these specified set of commands

const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
const int ButtonLed = 5;     // the number of the PushLed pin
const int ledPin =  13;      // the number of the LED pin
int ledState = LOW;
int ledState2 = LOW;
SoftwareSerial Xbee (RXPin, TXPin);// defining the new serial name
char incomingByte;    // a variable to read incoming serial data into
unsigned long TimerledPin;
unsigned long TimerButtonLed;

void setup()                // definitions that are always true
{
  Xbee.begin(9600);
// xbee.begin(9600);// set the data rate for the SoftwareSerial port
  pinMode(ledPin, OUTPUT);
  pinMode(ButtonLed, OUTPUT);
}

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

  // if it's a capital H (ASCII 72), turn on the LED:
  if (incomingByte == 'H')
  {
    TimerledPin = millis(),
    digitalWrite(ledPin, HIGH);
    ledState = HIGH;
    Xbee.println("I got an H");
   
  }
  if (newtime - TimerledPin >= 4000UL && ledState == HIGH)
  {
    digitalWrite(ledPin,LOW);
    ledState = LOW;
    Xbee.println("time is up for TimerledPin");
  }
 
  // if it's a capital L, turn on the LED:
  if (incomingByte == 'L')
  {
    TimerButtonLed = newtime;
    digitalWrite(ButtonLed, HIGH);
    ledState2 = HIGH;
    Xbee.println("I got a L");
  }
  if (newtime - ButtonLed >= 4000UL && ledState2 == HIGH)
  {
    digitalWrite(ButtonLed, LOW);
    ledState2 = LOW;
    Xbee.println("time is up for ButtonLed");
  }
}

Why doesn't the LED13 even work?
As I wrote earlier: The code
Code: [Select]
/*
  By Alexandra da Fonseca-Klein, Digital Jewellery project,
For interactive Jewellery 'proximity sensing hearts'
www.ADFK.co.uk; 2012
2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
If the 2 wearers are within reach (30 feet approx) one LED
comes on on the other wearers piece, if a button is pressed, another LED
lights up. This is the receiver side.
*/
#include <SoftwareSerial.h>        // use these specified set of commands

const int RXPin = A4 ;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
const int ButtonLed = 5;     // the number of the PushLed pin
const int ledPin =  13;      // the number of the LED pin
SoftwareSerial xbee (RXPin, TXPin);// defining the new serial name
int incomingByte;      // a variable to read incoming serial data into
unsigned long TimerledPin;
unsigned long TimerButtonLed;

void setup()                // definitions that are always true
{
  xbee.begin(9600);// set the data rate for the SoftwareSerial port
  pinMode(ledPin, OUTPUT);
  pinMode(ButtonLed, OUTPUT);
}

void loop() // run over and over
{

  if (millis()-TimerledPin>= 4000UL)
  {
    digitalWrite(ledPin,LOW);
  }
  // see if there's incoming serial data:
  if (xbee.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = xbee.read();
    // if it's a capital H (ASCII 72), turn on the LED:

    if (incomingByte == 'H')
    {
      TimerledPin=millis(),
      digitalWrite(ledPin, HIGH);
    }
  }
  if
    (xbee.read() == 'L')     // if you read the letter 'L'
  {
    digitalWrite(ButtonLed, HIGH);// set the ButtonLED on
    digitalWrite(ledPin, HIGH);// set the LED on
    delay(1000);               // wait for a second
  }
  else
  {
    digitalWrite(ButtonLed, LOW);// turn the ButtonLED off
  }
}
does work, but it is not nice to give in to 'delay' after all we've been through now.
I used the avoiding delay routine on LED13 and delay on the ButtonLed.
Maybe we can find out why with this one both LEDs work and with your last ones none.
I might have done a mistake replacing all the Serial with Xbee in the sketch.
By the way I have now finished the silver heart casing and replaced the LED13 with
ambient mood light changing diodes on Pin11- looks good and they don't flicker thanks to
all your help. Sometimes the vibe board that vibrates when the button is pressed on the
other brooch doesn't come on. I blame that on the delay that I still use for the prototype
until we have solved the problem. I'm also working on an extension of the program so
the receiver of the button call can also send a signal back to the server XBee Brooch.
Thought it's nice to know what the project is about...
Thanks everyone so far!

cyclegadget


In the first code on the above reply, you forgot to redo this line in the first part of the loop().

Code: [Select]
incomingByte = (char)Serial.read();
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

#22
Jun 18, 2012, 10:27 am Last Edit: Jun 18, 2012, 10:29 am by Coding Badly Reason: 1
Of course.
Now it shows life again, but with the following symptoms:
Like earlier, the ButtonLed only comes on once- then
it stays off. And the LED13 that I wanted not to flicker
in the first place (that's why I used delay) does flicker now.
Since it is a colour-changing LED in the full project it only
flickers in red because this is the first colour in the cycle.
I tried to use a longer value for mills like 6000UL but no
change.
Here is the code in which I corrected my error:
Code: [Select]
/*
 By Alexandra da Fonseca-Klein, Digital Jewellery project,
For interactive Jewellery 'proximity sensing hearts'
www.ADFK.co.uk; 2012
2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
If the 2 wearers are within reach (30 feet approx) one LED
comes on on the other wearers piece, if a button is pressed, another LED
lights up. This is the receiver side.
*/
#include <SoftwareSerial.h>        // use these specified set of commands

const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
const int ButtonLed = 5;     // the number of the PushLed pin
const int ledPin =  13;      // the number of the LED pin
int ledState = LOW;
int ledState2 = LOW;
SoftwareSerial Xbee (RXPin, TXPin);// defining the new serial name
char incomingByte;    // a variable to read incoming serial data into
unsigned long TimerledPin;
unsigned long TimerButtonLed;

void setup()                // definitions that are always true
{
 Xbee.begin(9600);
// xbee.begin(9600);// set the data rate for the SoftwareSerial port
 pinMode(ledPin, OUTPUT);
 pinMode(ButtonLed, OUTPUT);
}

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

 // if it's a capital H (ASCII 72), turn on the LED:
 if (incomingByte == 'H')
 {
   TimerledPin = millis(),
   digitalWrite(ledPin, HIGH);
   ledState = HIGH;
   Xbee.println("I got an H");
   
 }
 if (newtime - TimerledPin >= 4000UL && ledState == HIGH)
 {
   digitalWrite(ledPin,LOW);
   ledState = LOW;
   Xbee.println("time is up for TimerledPin");
 }
 
 // if it's a capital L, turn on the LED:
 if (incomingByte == 'L')
 {
   TimerButtonLed = newtime;
   digitalWrite(ButtonLed, HIGH);
   ledState2 = HIGH;
   Xbee.println("I got a L");
 }
 if (newtime - ButtonLed >= 4000UL && ledState2 == HIGH)
 {
   digitalWrite(ButtonLed, LOW);
   ledState2 = LOW;
   Xbee.println("time is up for ButtonLed");
 }
}
Thanks.


Moderator edit: [code] [/code] tags added.

Thank you for all your help!
As I have mentioned I got the project working with a limp
(leaving 'delay' for the 2nd LED).
And since there is no solution for avoiding delay for all 2 LEDs
I took the working Sketch to the next level:
Combining the working Server and the working Receiver side,
so both machines can do the same to each other.
Since this is another topic now I put this one as a new post.
If you're still with it maybe you can see why again only half of it works?
Thanks.
http://arduino.cc/forum/index.php/topic,110849.msg832863.html#msg832863

Coming back to this topic.
I have managed to combine 2 sketches of communicating Arduinos
successfully only to find having not solved the original problem, having 2 separate
LEDs staying on longer without delay, drags the whole sketch into
a long waiting period until it is responding again to the button press.
So. I went back to take another look at cyclegadgets post up there and don't find
the problem still. The LED13 flickers and the button LED doesn't come on at all.
Pressing the button long enough makes the LED13 go off though, so there must be
some kind of getting through of the letter 'L' through Serial.
Code: [Select]
/*
  By Alexandra da Fonseca-Klein, Digital Jewellery project,
For interactive Jewellery 'proximity sensing hearts'
www.ADFK.co.uk; 2012
2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
If the 2 wearers are within reach (30 feet approx) one LED
comes on on the other wearers piece, if a button is pressed, another LED
lights up. This is the receiver side.
*/
#include <SoftwareSerial.h>        // use these specified set of commands

const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
const int ButtonLed = 5;     // the number of the PushLed pin
const int ledPin =  13;      // the number of the LED pin
int ledState = LOW;
int ledState2 = LOW;
SoftwareSerial xbee (RXPin, TXPin);// defining the new serial name
char incomingByte;    // a variable to read incoming serial data into
unsigned long TimerledPin;
unsigned long TimerButtonLed;

void setup()                // definitions that are always true
{
  Serial.begin(9600);
// xbee.begin(9600);// set the data rate for the SoftwareSerial port
  pinMode(ledPin, OUTPUT);
  pinMode(ButtonLed, OUTPUT);
}

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')
  {
    TimerledPin = millis(),
    digitalWrite(ledPin, HIGH);
    ledState = HIGH;
    Serial.println("I got an H");
   
  }
  if (newtime - TimerledPin >= 4000UL && ledState == HIGH)
  {
    digitalWrite(ledPin,LOW);
    ledState = LOW;
    Serial.println("time is up for TimerledPin");
  }
 
  // if it's a capital L, turn on the LED:
  if (incomingByte == 'L')
  {
    TimerButtonLed = newtime;
    digitalWrite(ButtonLed, HIGH);
    ledState2 = HIGH;
    Serial.println("I got a L");
  }
  if (newtime - ButtonLed >= 4000UL && ledState2 == HIGH)
  {
    digitalWrite(ButtonLed, LOW);
    ledState2 = LOW;
    Serial.println("time is up for ButtonLed");
  }
}


cyclegadget


What power supply are you using?  Do you have resistors in series with your LEDs? Are you seeing the messages with time in between? Such as
Code: [Select]
Serial.println("I got an H");
Code: [Select]
Serial.println("time is up for TimerledPin");

In this part of the code, it repeats back to you the incomingbyte, do you get anything back?
Code: [Select]
Serial.println(incomingByte);

You are aware that these pins are analog 4 and 5? They can be used for digital and rx, tx but I just want to make sure you know they are on the analog side of the board.
Code: [Select]
const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX


If you like you can even add:
Code: [Select]
Serial.println(newtime);  //shows the updated time , pretty close to millis() if sketch is working




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

At work.
Trying it out when I get home.
Thanks.

As power supply I use either the FTDI breakout board or a 3.7V Lipo directly at the Lipo connector.
The LilyPad is converting it down to 3.3V internally.
I don't have any resistors in use.
The LEDs are standard 2.4-5V and work fine with the other projects.
The project does work electrically as such, too, were there not those delays that I want to get rid off.
Yes I can read the lines on the receiver's Serial: "I got an H" and "time is up for TimerledPin".
They come in very fast at a rate of 5 or more to 1. Almost unreadable when autoscroll is on.
I wasn't aware that A5 means analog 5- but now I know, of course!  Should have read the files- but now that they
do work and I have soldered them already to the XBee shield, I think for now I'll keep them connected.
Now the updated time is counting up from 100 to 57.000 or higher, after I added that line.
The LED is flickering manically and the buttonLed doesn't do anything.

cyclegadget


I am still learning so, I had one more mistake. I didn't have a Led in digital 5 so, I did not fully test my sketch before. I just finished adding the LED to D5 and I found my problem. This line has the wrong variable in it. I used "ButtonLed" and it should have been "TimerButtonLed".
Code: [Select]
if (newtime - ButtonLed >= 4000UL && ledState2 == HIGH)

Here is the corrected code:
Code: [Select]
/*
  By Alexandra da Fonseca-Klein, Digital Jewellery project,
For interactive Jewellery 'proximity sensing hearts'
www.ADFK.co.uk; 2012
2 Arduino Lilypad Simple Boards are connected via Xbee Series 2 radios.
The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
If the 2 wearers are within reach (30 feet approx) one LED
comes on on the other wearers piece, if a button is pressed, another LED
lights up. This is the receiver side.
*/
#include <SoftwareSerial.h>        // use these specified set of commands

const int RXPin = A4;              // setting the pin to RX
const int TXPin = A5;              // setting the pin to TX
const int ButtonLed = 5;     // the number of the PushLed pin
const int ledPin =  13;      // the number of the LED pin
int ledState = LOW;
int ledState2 = LOW;
SoftwareSerial xbee (RXPin, TXPin);// defining the new serial name
char incomingByte;    // a variable to read incoming serial data into
unsigned long TimerledPin;
unsigned long TimerButtonLed;

void setup()                // definitions that are always true
{
  Serial.begin(9600);
// xbee.begin(9600);// set the data rate for the SoftwareSerial port
  pinMode(ledPin, OUTPUT);
  pinMode(ButtonLed, OUTPUT);
}

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')
  {
    TimerledPin = millis(),
    digitalWrite(ledPin, HIGH);
    ledState = HIGH;
    Serial.println("I got an H");
   
  }
  if (newtime - TimerledPin >= 4000UL && ledState == HIGH)
  {
    digitalWrite(ledPin,LOW);
    ledState = LOW;
    Serial.println("time is up for TimerledPin");
  }
 
  // if it's a capital L, turn on the LED:
  if (incomingByte == 'L')
  {
    TimerButtonLed = newtime;
    digitalWrite(ButtonLed, HIGH);
    ledState2 = HIGH;
    Serial.println("I got a L");
  }
  if (newtime - TimerButtonLed >= 4000UL && ledState2 == HIGH)
  {
    digitalWrite(ButtonLed, LOW);
    ledState2 = LOW;
    Serial.println("time is up for ButtonLed");
  }
}
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

#29
Jun 21, 2012, 11:59 am Last Edit: Jun 21, 2012, 12:01 pm by napok Reason: 1
Yes, of course- I didn't see that either.
Now the buttonLED works great, I have even managed to tweak the on time
by playing with the mills a little, putting it to 200UL and the buttonLED comes
on really crisp without delay and misses no button push.
BUT:
The ledPin 13 still flickers like mad despite the Serial reading of
'time's up'. It should stay on calmly as long as there is an 'H' coming
out of the other machine indicating it's in proximity.
The flickering was the reason why I used 'delay' in the first place.
And:
Now when I turn off the other machine, making sure it can definitely
not pick up an 'H' coming, it still reads:
"I got an H
I got an H
time is up for TimerledPin
I got an H
I got an H"
and is happy to flicker, even when there's no sender of that message
available anymore. It doesn't start by itself though, there has to
be an initial turning on of the other machine. But it keeps on as if
it still got the read.
I think we might have to put the ledPin somehow on LOW when there's no 'H'
or Serial available. I tried to put this command in but I don't know where
it would work, it doesn't with me:

Code: [Select]
else
 {
   digitalWrite(ledPin,LOW);
   ledState = LOW;
   Serial.println("no signal");
 }


Go Up