Hi. This should combine a receiver and a server side of
2 Arduinos that are working fine together in one direction,
so each can do back what the other can do. That is:
- Light an LED to show that they're in proximity. (Serial letter 'H')
- Light another LED when a button is pressed on the other one. (Serial letter 'L')
- Light an Indicator LED to show the button has been pressed. (LED13)
So far I have only got them to work as server and receiver.
Only one can press a button and the other lights up.
I combined the two sketches and now I can only get
the 1. and the 3. to work properly. When I press the button
nothing is triggered in the other, when I press it longer, the
proximity indicating LED turns off, so I think that it does receive the 'L'
after all. I'm posting the combined sketch here.
/*
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 radios (for
the post called 'Serial'.
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 combined sketch for both machines. 'S' before the comments
stands for 'comes from the server 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 = 9; // the number of the PushLed pin
const int ProxInd = 11; // the number of the ProxInd
const int buttonPin = 5; //S the number of the pushbutton pin
const int ButtInd = 13; //S the number of the Button indicator LED pin
SoftwareSerial Serial (RXPin, TXPin);// defining the new serial name
int buttonState = 0; //S variable for reading the pushbutton status
// a variable to read incoming serial data into
char incomingByte;
unsigned long TimerProxInd;
unsigned long TimerButtonLed;
void setup() // definitions that are always true
{
Serial.begin(9600);// set the data rate for the SoftwareSerial port
pinMode(ProxInd, OUTPUT);
pinMode(ButtonLed, OUTPUT);
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
{
{
Serial.print('H'); //S send the letter H out
delay(500); //S wait half a second
}
{
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 indicator LED off:
else //S otherwise
{
Serial.print('L'); //S send the letter 'L' over the wireless connection
digitalWrite(ButtInd, HIGH);//S and turn LED on:
}
if (millis()-TimerProxInd>= 4000UL)
digitalWrite(ProxInd,LOW);
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = (char)Serial.read();
if (incomingByte == 'H')
{
TimerProxInd=millis(),
digitalWrite(ProxInd, HIGH);
}
}
if
(Serial.read() == 'L') // if you read the letter 'L'
{
digitalWrite(ButtonLed, HIGH);// set the ButtonLED on
digitalWrite(ProxInd, HIGH);// set the LED on
delay(1000); // wait for a second
}
else
{
digitalWrite(ButtonLed, LOW);// turn the ButtonLED off
}
}
}
Now if I leave the command right at the beginning
{
Serial.print('H'); //S send the letter H out
delay(500); //S wait half a second
}
out and upload that one to the receiver, leaving
the combined sketch as it is for the server side, everything works fine.
The Button LED triggering is not blocked.
But then I don't have 2 way communication.
What did I do wrong?