I have this code (2nd of the 2 codes at the bottom) finally working perfect with the help of forum members
especially cyclegadget and AWOL.
These are two interactive proximity sensing machines
2 Arduino Lilypad Simple Boards http://www.sparkfun.com/products/10274are connected via Xbee Series 2 radios Digi XBee Ecosystem | Everything You Need to Explore and Create Wireless Connectivity | Digi International via shieldhttp://www.sparkfun.com/products/8937.
The Simple Board lacks Rx/Tx, therefore the use of SoftwareSerial.
If the 2 wearers are within reach (30 feet approx) one ProximityIndicatorLED
comes on on the other wearers piece, if a button is pressed, another ButtonLED
lights up. This is the working combined sketch so that both machines can
do the same to each other.
I have now my LED's longer on without delay, but on the opposing
machine I want the first (ProximityLED) to imitate a heartbeat pulsing-
so using 'fade' instead of 'blink'. I was already able to implement 'blink'.
Yet I'm struggling to adjust the blinking frequency with 'long interval'.
And the ButtonLed should trigger a 'fire effect' of a couple of LEDs instead of just coming on.
First: Here is the code for 'fire effect'. Ideally it should do 3 independent LEDs. I would be happy,
if I could get 1 to flicker like it does as a simple sketch.
// LED Fire Effect for one LED
int ButtonLed = 9;
void setup()
{
pinMode(ButtonLed, OUTPUT);
}
void loop() {
analogWrite(ButtonLed, random(120)+135);
delay(random(100));
}
/*
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 ProximityIndicatorLED
comes on on the other wearers piece, if a button is pressed, another ButtonLED
lights up. This is the working combined sketch so that both machines can
do the same to each other. S before the comment stands for 'from server code'.
*/
#include <SoftwareSerial.h> // use these specified set of commands
//////////////////SERIAL PINS//////////////////
const int RXPin = A4; // setting the pin to RX
const int TXPin = A5; // setting the pin to TX
SoftwareSerial xbee (RXPin, TXPin);// defining the new serial name
/////////////////LED PINS//////////////////////
const int ButtonLed = 9; // the number of the PushLed pin
const int ProxInd = 11; // the number of the ProximityIndicatorLED pin
const int ButtInd = 13; //S the number of the indicator LED pin
////////////////BUTTON PINS/////////////////////
const int buttonPin = 5; // S the number of the pushbutton pin
////////////////STATE VARIABLES////////////////
int buttonState = 0; // S variable for reading the pushbutton status
int ledState = LOW;
int ledState2 = LOW;
////////////////TIMER VARIABLES///////////////
unsigned long TimerProxInd;
unsigned long TimerButtonLed;
unsigned long TimerprintH = 0; //timer for printing H
unsigned long TimerprintL = 0; //timer for printing L
///////////////SERIAL VARIABLES//////////////
char incomingByte; // a variable to read incoming serial data into
///////////////SET-UP////////////////////////
void setup() // definitions that are always true
{
Serial.begin(9600);
xbee.begin(9600);// set the data rate for the SoftwareSerial port
pinMode(ProxInd, OUTPUT); // the number of the ProximityIndicatorLED pin (that comes on when there's a steady 'H' or Serial input)
pinMode(ButtonLed, OUTPUT); // the number of the PushLed pin ( that comes on when there's an 'L' read on Serial)
pinMode(ButtInd, OUTPUT); //S declare LED as output the number of the indicator LED pin
//(that indicates on the same board that the button has been pressed)
pinMode(buttonPin, INPUT); //S declare pushbutton as input
digitalWrite(buttonPin, HIGH); //S default pin state to HIGH or 'on'
}
void loop() // run over and over
{
unsigned long newtime = millis();
if (newtime - TimerprintH >= 900UL) //S wait 900ms
{
xbee.print('H'); //S send the letter H out
TimerprintH = newtime; //update timer
}
/////////////////////////CHECK THE BUTTON//////////////////////
buttonState = digitalRead(buttonPin);//S read the state of the pushbutton value:
if (buttonState == HIGH) //S is the button not being pressed
{
digitalWrite(ButtInd, LOW);//S leave the ButtonIndicatorLED off:
}
else //S otherwise
{
if (newtime - TimerprintL >= 2000UL) //S wait 2 seconds between sending L
{
xbee.print('L'); //S send the letter 'L' over the wireless connection
digitalWrite(ButtInd, HIGH);//S and turn ButtonIndicatorLED on:
TimerprintL = newtime; //update timer
}
}
//////////////////////////CHECK FOR INCOMING SERIAL///////////
// see if there's incoming serial data:
if (xbee.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = (char)xbee.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H')
{
TimerProxInd = newtime,
digitalWrite(ProxInd, HIGH);
ledState = HIGH;
}
// if it's a capital L, turn on the LED:
if (incomingByte == 'L')
{
TimerButtonLed = newtime;
digitalWrite(ButtonLed, HIGH);
ledState2 = HIGH;
}
}
if (newtime - TimerProxInd >= 2000UL && ledState == HIGH)
{
digitalWrite(ProxInd,LOW);
ledState = LOW;
Serial.println("time is up for TimerProxInd");
}
if (newtime - TimerButtonLed >= 1000UL && ledState2 == HIGH)
{
digitalWrite(ButtonLed, LOW);
ledState2 = LOW;
Serial.println("time is up for ButtonLed");
}
}