0

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

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;
}
1

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.

0

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
1
  • 2
    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
0

as it has been pointed before you the read function returns a -1 when there are no information to access, what you can do is

while( !Serial.available() )
{
  //do nothing
}
Buton1=Serial.read();
Serial.clear();   //for having the buffer clear for the next button
0

See if this helps:

const int analogInPin = A0; //LDR PIN
const int digitalOutPin = 13; // LED PIN
int outputValue;

int sensorValue = 0; //

byte Buton1; //LDR button
byte Buton2; //Blutooth button


void setup() {
  Serial.begin(9600);
}

void loop() {
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (inChar == 'l' || inChar == 'L')
    {
      Buton1 = Serial.parseInt();
      Buton1 = constrain(Buton1,0,1);
    }  
    else if (inChar == 'b' || inChar == 'B')
    {
      Buton2 = Serial.parseInt();
      Buton2 = constrain(Buton2,0,1);
    }  
    else
    {
      Serial.println("invalid input");
      while (Serial.available() > 0)
        Serial.read();
    }
    Serial.print("Buton1 = ");
    Serial.println(Buton1);
    Serial.print("Buton2 = ");
    Serial.println(Buton2);
    Serial.println();    
  }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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