I have a small project and I have some problems. The ideea of the project is the following: I have an LED which is commanded by two "buttons". The buttons are basically 1/0 values which I have to send from serial monitor. One button command a LDR sensor, which lights up the LED when it's value is below 300 (in my code). The other button just lights the led (Value 1) or turn it off (Value 0). The buttons are working together, so I have to consider both of them in my statements. The problem is that I cannot play with the serial monitor to let me input the values for the two buttons separately, he's just looping forever.
Can anybody, please, help me to find a solution to be able to input values for both buttons ?
Here is the code:
const int analogInPin = A0; //LDR PIN
const int digitalOutPin = 13; // LED PIN
int outputValue;
int sensorValue = 0; //
char Buton1; //LDR button
char Buton2; //Blutooth button
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available())
{
Serial.println("Button1 value: ");
Buton1=Serial.read();
if (Buton1 == '1')
{
Serial.println("Button2 value: ");
Buton2=Serial.read();
if (Buton1 == '1' and Buton2 == '0')
{
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
Serial.print ("Sensor: ");
Serial.println(sensorValue);
if (sensorValue<300);
{
digitalWrite(digitalOutPin, 255);
}
}
else if (Buton1 == '1' and Buton2 == '1')
{
digitalWrite(digitalOutPin, 255);
Serial.print("Both buttons are ON ! !");
}
}
else if (Buton1 == '0')
{
Serial.println("Button2 value: ");
Buton2=Serial.read();
}
if (Buton1 == '0' and Buton2 =='1')
{
digitalWrite(digitalOutPin, 255);
}
else if (Buton1 == '0' and Buton2 == '0')
{
Serial.println("Both buttons are inactive ! Please reset");
break;
}
}
}