Hello, I am writing a code to control led with Push Button and Serial communication. When I press push button led is on. Then I want to send "R" through serial monitor and led should be off. But the strange thing is that when I only open serial monitor then led is off :o . It doesn't wait for "R". I need to know what should be the correction in my code. Here is the code
int switch1 = 2;
int motorled1 = 3;
int switch2 = 4;
int motorled2 = 5;
int d1=0;
int d2=0;
const int RX = 0;
const int TX = 1;
int reset1=0;
int reset2=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
d1 = digitalRead(2);
d2 = digitalRead(4);
while (d1 == 1)
{
digitalWrite(motorled1, HIGH);
digitalWrite(motorled2,LOW);
while (Serial.available() > 0)
{ (Serial.write(1));
reset1 = (Serial.read());
if(reset1 == 'R')
{ digitalWrite(motorled1,LOW);
digitalWrite(motorled2,LOW);
}
}
}
while (d2 == 1)
{
digitalWrite(motorled2, HIGH);
digitalWrite(motorled1, LOW);
while(Serial.available() > 0)
{(Serial.write(2));
reset2 = (Serial.read());
if(reset2=='R')
{ digitalWrite(motorled1,LOW);
digitalWrite(motorled2,LOW);
}
}
}
}
I would also be interested to know how your script can be modified to stop/start a countdown timer. I am trying to build a football scoreboard, and i'm a little tuck on the timer. Your script sounds like what i need, but to function as a start/stop for a timer. Hope you make out ok with your issue.