Push-button on anagogic input (French speaker welcomed !!)

Hello,
Sorry if my message is not well written, i am french.

i try to make a machine and for that i must put a push-button on a anagogic input, because a have a lot of lights on digital port. The idea is that when i push the button, all the three light of a part of my machine go ON and stay like that (after, the lights are ON in function of the value “valeurTrois”).

Here is what i have done (part of), but i am very beginner and all light go ON without touching the button. Can you help me ?

int led2 = 6;
int led3 = 7;
int led4 = 8;
int alimTrois = A0;
unsigned int valeurTrois (0);

void setup ()
{
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(alimTrois,INPUT);
}

void loop ()
{

switch (valeurTrois)
{
case 0:
digitalWrite (6,LOW);
digitalWrite (7,LOW);
digitalWrite (8,LOW);
break;

case 1:
digitalWrite (6,HIGH);
digitalWrite (7,LOW);
digitalWrite (8,LOW);
break;

case 2:
digitalWrite (6,HIGH);
digitalWrite (7,HIGH);
digitalWrite (8,LOW);
break;

case 3:
digitalWrite (6,HIGH);
digitalWrite (7,HIGH);
digitalWrite (8,HIGH);
break;
}

if (analogRead(A0) == LOW) //I have tried so many thing here !!!
{
valeurtrois = 3;
}
}

You can use the analog pins as digital pins and that would be much easier. As a digital pin A0 is also called digital pin 14. But you can even use A0.

///  in the setup()
    pinMode(A0, INPUT);


/// anywhere later in code
    if(digitalRead(A0) == LOW) {
        // your button was pressed
}

Don't the analog inputs have internal pull ups as well, like other digital pins?

groundfungus:
Don’t the analog inputs have internal pull ups as well, like other digital pins?

They can do anything any other digital pin can. They really are just digital pins to begin with. The analog stuff is extra for them. But at their heart they’re just like the other 13 digital pins. They just have this one extra thing, analogRead, that they can also do. Much like some of the other 13 digital pins also have extra functions.

 valeurtrois = 3;

will have no effect on

switch (valeurTrois)

as they are two different variables.

Thanks for your help.

So, to try, I ve put that :

const int led3 = 7; const int led4 = 8; const int alimTrois = A0;

void setup () { pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); pinMode(led4,OUTPUT); pinMode(A0,INPUT); }

void loop () { if (digitalRead(A0) == LOW) { digitalWrite (7,HIGH); } else if (digitalRead(A0) == HIGH) { digitalWrite (6,HIGH); } }

But both light are going ON !! (same if I put "if" in state of "else if", or "while"...). This is have done first telling me perhaps is a input problem.

Ok, I have find a way !

etatAlimTrois = digitalRead(alimTrois); if (etatAlimTrois == LOW) { valeurTrois = 3; }

Thanks for your help ! If someone want my code : it is the enigma to resolve you can see in the film "Die Hard"

"You have a 3 and a 5 litre water container, each container has no markings except for that which gives you it's total volume. You also have a running tap. You must use the containers and the tap in such away as to exactly measure out 4 litres of water."

Except I say that your have to connect the machine with a 4V tension and you have a 3V and a 5V condensation to do it.

(You do realise that the forum has a whole section for francophones?)