Problem with a "if" and a multiple modes

Hello guys, i need to do a program as homeworks and basically no one told me how this language works, i'm fairly new to programming so i'm in need of help.

I need to make a program with a button that controls 4 different modes and there's a potentiometer too

I think i've located the problem, i wrote it after a // in the program (you may not need to read the stuff after the modes and stuff)

I really really hope you can help me out

This is the code :

int led12=12; int led11=11; int led10=10; int led9=9; int led8=8; int switch7=7; int pot0=A0; int compteur=1; int val=0; int etat = HIGH; int DerniereDate = 0;

void setup() { pinMode(led12,OUTPUT); pinMode(led11,OUTPUT); pinMode(led10,OUTPUT); pinMode(led9,OUTPUT); pinMode(led8,OUTPUT); pinMode(switch7,INPUT_PULLUP); Serial.begin(9600); }

void loop() { if (digitalRead(switch7==LOW)){ //Problem around here i think delay(20); compteur=compteur+1; } if (compteur==5){ delay(20);compteur=1; } if (compteur==1){ delay(20);mode1(); } if (compteur==2){ delay(20);mode2(); } if (compteur==3){ delay(20);mode3(); } if (compteur==4){ delay(20);mode4(); } }

void mode1(){ digitalWrite(led12,LOW); digitalWrite(led11,LOW); digitalWrite(led10,LOW); digitalWrite(led9,LOW); digitalWrite(led8,LOW); }

void mode2(){ digitalWrite(led12,HIGH); digitalWrite(led11,HIGH); digitalWrite(led10,HIGH); digitalWrite(led9,HIGH); digitalWrite(led8,HIGH); }

void mode3(){ val=analogRead(pot0); unsigned long ActuelMillis = millis(); if (ActuelMillis - DerniereDate > val) {DerniereDate = ActuelMillis; if (etat == LOW) {etat = HIGH;} else {etat = LOW;}

digitalWrite(led12,etat); digitalWrite(led11,etat); digitalWrite(led10,etat); digitalWrite(led9,etat); digitalWrite(led8,etat); } }

void mode4(){ val=analogRead(pot0); digitalWrite(led12,HIGH);digitalWrite(8,LOW);delay(val);val=analogRead(pot0); digitalWrite(led11,HIGH);digitalWrite(led12,LOW);delay(val);val=analogRead(pot0); digitalWrite(led10,HIGH);digitalWrite(led11,LOW);delay(val);val=analogRead(pot0); digitalWrite(led9,HIGH);digitalWrite(led10,LOW);delay(val);val=analogRead(pot0); digitalWrite(led8,HIGH);digitalWrite(led9,LOW);delay(val);val=analogRead(pot0); }

Research Switch Case Unless you are required to use IF statements.

I did not look through your sketch line by line, but I did paste your code in the IDE and it compiled. Can you be more specific as to what is going on when you run it? It will help others locate your problem faster without trying to decipher the entire sketch. Post you wiring as well because it could be there.

if (digitalRead(switch7==LOW)){ //Problem around here i think

Change to

if (digitalRead(switch7) == LOW) { //Problem around here i think


I tried to change the

if (digitalRead(switch7==LOW))


if (digitalRead(switch7) == LOW) {

and nothing changed.

Well basically i have a button which possesses 4 modes :

Mode 1 turns all the leds off

Mode 2 turns them on

Mode 3 makes them blink until i press again

Mode 4 makes them wave

I've tried everymode independently and they work

I've made a counter (compteur) to go up by 1 when i press the button, so i can get into the different modes But the problem is that when i press the button, nothing happens

What do you see if you print compteur after detecting a button press ?

  if (digitalRead(switch7) == LOW) //Problem around here i think
    compteur = compteur + 1;
    Serial.print("compteur : ");

Are you sure that the button is connected to pin 7 and wired to take the pin LOW when pressed ?

I did what you proposed, here's the result :

compteur : 2 compteur : 3 compteur : 4 compteur : 5 compteur : 2 compteur : 3 compteur : 4 compteur : 5 compteur : 2 compteur : 3 compteur : 4 compteur : 5 compteur : 2 compteur : 3 compteur : 4 compteur : 5

And it goes on and on, note that it goes like this when i'm still pressing the button. Maybe should i create a

while(digitalRead(switch7)==LOW){ delay(50);}

So that it doesn't make compteur go up when i'm still holding the button

By the way the compteur (counter) goes up but the modes do not activate, the leds still stay in mode1 (all offs)

I finally got it to work !!!

Thanks to all of you guys, i really appreciate that you spent time to look into my code !

You rock ! Keep coding my brothers

So that it doesn't make compteur go up when i'm still holding the button

You need to detect when the button [u]becomes[/u] pressed rather than when it [u]is[/u] pressed. Look at the StateChangeDetection example in the IDE.