I have a small project and I have some problems while working on that. The idea of the project is as below: I have an LED which is operated by two "buttons". The buttons takes two values (1 or0) which I have to send from serial monitor. One button is for LDR sensor, which lights up the LED when its value is below 300 (in my code). The other button turns 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 it's just looping continuously and I cant type anything as input.
Can anyone help me with this 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')
{
Buton2 = Serial.read();
Serial.println("Button2 value: ");
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;
}
}
}