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;
              }
  }
}
share|improve this question
1  
How are you supposed to know which 1 and 0 come from which button? – Majenko Oct 12 '16 at 17:31

A simple approach would be to use bit 0 of serial data to control "button 1", and bit 1 of serial data to control "button 2". As follows:

char sd = Serial.read();
byte button1 = sd & 1;
byte button2 = sd & 2; // Or (sd & 2)>>1 if you prefer

With the above code, button 1 is true for odd-digit inputs, and button 2 is true when (sd & 2)>>1 is odd. For example, input 0 to turn off both buttons, 1 to turn on only button 1, 2 for only button 2, and 3 to turn on both.

You might also wish to button1 or button2 only when the input character represents a digit. For this, you can say:

char sd = Serial.read();
byte button1, button2;
... ;
if ('0' <= sd && sd <= '9') {
    button1 = sd & 1;
    button2 = (sd & 2)>>1;
}
share|improve this answer

Each button has a 1 state and a 0 state. The combination will always be something like this:

if (button1 == '1' and button2 == '1')
do something
if (button1 == '1' and button2 == '0')
do something
if (button1 == '0' and button2 == '1')
do something
if (button1 == '0' and button2 == '0')
do something
share|improve this answer
1  
How does this solve the problem in the question? The problem was "it is just looping forever". – Nick Gammon Oct 13 '16 at 4:29

The first problem is that your code does not wait for anything to be typed, so it loops very rapidly. Serial.read() returns -1 when there is nothing in the buffer, so I imagine your codes is printing something like:

Button1 value: 
Button1 value: 
Button1 value: 
Button1 value: 

Try inserting

while( !Serial.available() )
   ;

before your serial.read() calls. This will force it to wait until you type something before it continues.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.