Wireless Alarm
i trying create a wireless alarm system using 2 arduinos , 2 xbees. want arlam on when button not pressed , off when is.
my router code is:
const int buttonpin = 2;
int buttonstate = 0;
void setup() {
serial.begin(9600);
pinmode(buttonpin, output);
}
void loop(){
buttonstate = digitalread(buttonpin);
serial.println(buttonstate);
delay(500);
}
and coordinator code is:
int ledpin = 2;
int buttonstate;
void setup() {
serial.begin(9600);
pinmode(ledpin, output);
}
void loop(){
if (serial.available() > 0)
{
while(serial.available()>0) {buttonstate=serial.write(serial.read());}
if (buttonstate == low) {digitalwrite(ledpin, low);}
else {digitalwrite(ledpin, high);}
}
}
....
the coordinator receives signals router alarm doesn't turn off when button pushed.
my router code is:
const int buttonpin = 2;
int buttonstate = 0;
void setup() {
serial.begin(9600);
pinmode(buttonpin, output);
}
void loop(){
buttonstate = digitalread(buttonpin);
serial.println(buttonstate);
delay(500);
}
and coordinator code is:
int ledpin = 2;
int buttonstate;
void setup() {
serial.begin(9600);
pinmode(ledpin, output);
}
void loop(){
if (serial.available() > 0)
{
while(serial.available()>0) {buttonstate=serial.write(serial.read());}
if (buttonstate == low) {digitalwrite(ledpin, low);}
else {digitalwrite(ledpin, high);}
}
}
....
the coordinator receives signals router alarm doesn't turn off when button pushed.
oh start...
first, when posting code, use code tags; makes easier read. speaking of easier read, there should be statement per line, none of crap:
it should more this:
that makes more readable. may not suit "style" of coding if prefer other way, you're asking others help, not yourself.
now actual issue you're having:
say buttonstate high (or 1). code tells send ascii 1 new line character, you'll sending 49 followed 10. neither of equal low (or 0). since want actual value, , don't want encode data in ascii, should using serial.write.
first, when posting code, use code tags; makes easier read. speaking of easier read, there should be statement per line, none of crap:
code: [select]
while(serial.available()>0) {buttonstate=serial.write(serial.read());}
if (buttonstate == low) {digitalwrite(ledpin, low);}
it should more this:
code: [select]
while(serial.available()>0)
{
buttonstate=serial.write(serial.read());
}
if (buttonstate == low)
{
digitalwrite(ledpin, low);
}
that makes more readable. may not suit "style" of coding if prefer other way, you're asking others help, not yourself.
now actual issue you're having:
code: [select]
serial.println(buttonstate);
say buttonstate high (or 1). code tells send ascii 1 new line character, you'll sending 49 followed 10. neither of equal low (or 0). since want actual value, , don't want encode data in ascii, should using serial.write.
Arduino Forum > Using Arduino > Project Guidance > Wireless Alarm
arduino
Comments
Post a Comment