[Help] Simple project with 2 buttons

Hi,

I keep failing a programming this. 2 permanent switches: A and B. one LCD 16x2 Screen.
If switch A is pressed (and maintained), then LCD shows : A
If switch B is pressed (and maintained), then LCD shows : B
If switches A and B are pressed (and maintained), then LCD shows : A and B
If nothing is pressed, LCD shows : Free

Here is the code i did :

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int switcha = 7;
int switchb = 8;

void setup()
{
pinMode(switcha, INPUT); // OUTPUT signifie que c'est une sortie
pinMode(switchb, INPUT); // OUTPUT signifie que c'est une sortie

lcd.begin(16,2);
}
void loop()
{

if(digitalRead(switcha) == HIGH) // on regarde si notre entrée capte du courant

{
lcd.setCursor(0,0);

lcd.print("A");

lcd.setCursor(0,1);

lcd.print("A");

delay(1000); }

if(digitalRead(switchb) == HIGH) // on regarde si notre entrée capte du courant

{
lcd.setCursor(0,0);

lcd.print("B");

lcd.setCursor(0,1);

lcd.print("B");

delay(1000); }

if(digitalRead(switchb) == HIGH && digitalRead(switcha) == HIGH) // on regarde si notre entrée capte du courant

{
lcd.setCursor(0,0);

lcd.print("A and B");

lcd.setCursor(0,1);

lcd.print("A and B");

delay(1000); }

if(digitalRead(switchb) == LOW && digitalRead(switcha) == LOW) // on regarde si notre entrée capte du courant

{
lcd.setCursor(0,0);

lcd.print("Free");

lcd.setCursor(0,1);

lcd.print("Free");

delay(1000); }

}

Maybe the loop is not ood, maybe nothing is good, any help will be appreciated. Thank you !

I think it's the way you've connected your switches. How do you have them connected?

Do you know what a pull up resistor is?

sha33:
Maybe the loop is not ood, maybe nothing is good, any help will be appreciated. Thank you !

But, what exactly do you see?
My guess is that when you try to press the two buttons together, you only see BB, or AA then BB, and only after that "A and B". If so, then it's because most humans can't really press two buttons at exactly the same time, so you'll have to figure out a smarter test...

hello,

i see many things, completely laggy, and mixed words, like FrAA, and so things..

I wired the switch like 5V---- Switch---resistor---input Pin

The thing is when you do nothing, the screen says AA...

sha33:
The thing is when you do nothing, the screen says AA...

You should take care of that first, by reading (as LarryD suggested) about pull-up/pull-down resistors.
See for example:
http://playground.arduino.cc/CommonTopics/PullUpDownResistor

sha33:
hello,

i see many things, completely laggy, and mixed words, like FrAA, and so things..

I wired the switch like 5V---- Switch---resistor---input Pin

The thing is when you do nothing, the screen says AA...

It should have been
5v -- Switch -- inputpin --resistor ---GND

Yup, i just did that, but the problem is the same? I am sure the problem is something in the code. Whe nothing is engaged, the screen shows AA. How would you have done it (like me, with if loops, or switch-case, or for..while)

Your resistor that goes from pin 7 to GND is not connected properly.

I think in the loop function you need a while loop like this.

While ( int a = digitalRead(switcha) == HIGH || int b = digitalRead(switchb) == HIGH)) {
if ( a == b) {
Do both high code here
} else if ( a == HIGH)) {
Do a high code here
}
else {
Do b high code here
}
}

Do you need to define your default state as Low - Low?
Can you not just put lcd display "Free" as the first statement in the loop...then check for the other button state combinations with the if ?