Pages: [1]   Go Down
Author Topic: Led stay longer on without delay  (Read 776 times)
0 Members and 1 Guest are viewing this topic.
England
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a working xbee project with two arduinos. (Receiver side)
One Led shows that they are in reach of each other.
The other one I can trigger by pushing a button on the other board.
But:
I want the ledPin (13) to be on as long as there is SoftwareSerial available.
Right now it is coming on and off too nervously. Usually I just would increase the delay.
But if I increase the delay, there is no or very delayed reaction of the other ButtonLed,
that I trigger from the other board.
I haven't quite understood how to implement the 'blink without delay',
because I don't want it to blink, it should stay on as long as the two boards
are in reach of each other.
Could you help me to accomplish that?

Code:
#include <SoftwareSerial.h>

const int RXPin = 10;              // setting the pin to RX
const int TXPin = 11;              // 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

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

void loop() // run over and over
{
  if
  (xbee.available())
  {
  digitalWrite(ledPin, HIGH);// set the LED on
  delay(200);               // wait for a second
}
  else
  {
  digitalWrite(ledPin, LOW);// set the LED on
}
  if
  (xbee.read() == 'L')
  {
  digitalWrite(ButtonLed, HIGH);// set the LED on
  digitalWrite(ledPin, HIGH);// set the LED on
  delay(500);               // wait for a second
}
  else
  {
  digitalWrite(ButtonLed, LOW);// set the LED on
}
}
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 439
Posts: 23758
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How long are you willing to wait before deciding to turn off the LED?
What you can do is note the time ( millis() ) every time you get a character and turn on an LED.
Then set a time when the LED is to be turned off.
Every pass thru loop, you check if the time has been reached, and if so you turn off the LED.
As long as characters keep coming in, the turn-off time will keep getting pushed out, and the LED will stay on.

I would do the time check as:
Code:
if ( (millis() -timeCheck)>=onDuration){
//turn off LED
digitalWrite(ButtonLed, LOW);// set the LED off
}
declare the time variable as 'unsigned long' data types.
That's the heart of blink-without-delay.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

Thank you. I'll try it out.
Logged

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

Hi again.
Got these errors:
xbee17receiver.cpp: In function 'void loop()':
xbee17receiver:31: error: 'timeCheck' was not declared in this scope
xbee17receiver:31: error: expected primary-expression before '>=' token
xbee17receiver:31: error: 'onDuration' was not declared in this scope
xbee17receiver:31: error: expected `;' before ')' token
Not sure how to implement your proposed code.
Please help! You have put the blink without delay part into the ButtonLed,
that is only supposed to come on after the other board owner has pressed a
button. I wanted to have the LedPin 13 on for longer that replies to the
xbee available part, when the two boards are near. So I put it into that part.
Of course once I've understood how to implement staying on without delay
I also want the ButtonLed to stay on a second without delay.
The code looks like this now:
Code:
/*
  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 = 10;              // setting the pin to RX
const int TXPin = 11;              // 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

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

void loop() // run over and over
{
  if
  (xbee.available()) // check if there is actually data coming from
  {                  // communication port
  digitalWrite(ledPin, HIGH);// set the LED on
  if  (millis() -timeCheck)>=onDuration)
}
  else                      //if not
  {
  digitalWrite(ledPin, LOW);// turn the LED off
}
  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
}
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you stop cross-posting "Led stay longer on without delay" please? Locking this thread.
Logged

Pages: [1]   Go Up
Jump to: