Not working with other condition

Hello, i have a problem with my program , the base program is this :

void loop(){
  if (digitalRead(pinCapteur) == HIGH) {
  digitalWrite(vert, LOW);
  digitalWrite(orange, HIGH);
  delay(4000);
  digitalWrite(orange, LOW);
  digitalWrite(rouge, HIGH);
  delay(20000);
  }
  else {
  digitalWrite(vert, HIGH);
  digitalWrite(orange, LOW);
  digitalWrite(rouge, LOW);

  }  
  delay(10);
}

and it’s working perfectly, but when i add the 2nd part like that :

void loop(){
  if (digitalRead(pinCapteur) == HIGH) {
  digitalWrite(vert, LOW);
  digitalWrite(orange, HIGH);
  delay(4000);
  digitalWrite(orange, LOW);
  digitalWrite(rouge, HIGH);
  delay(20000);
  }
  else if (digitalRead(bouton) == HIGH) {
    delay(20000);
    digitalWrite(ledv, HIGH);
    digitalWrite(ledr, LOW);
    delay(15000);
    digitalWrite(ledv, LOW);
    digitalWrite(ledr, HIGH);
  }
  else {
  digitalWrite(vert, HIGH);
  digitalWrite(orange, LOW);
  digitalWrite(rouge, LOW);
  digitalWrite(ledv, LOW);
  digitalWrite(ledr, HIGH);
  }  
  delay(10);
}

it’s not working and everything is broken , even with only an if it is not working , im not pro at arduino and i need your help for this thanks
(The program just making light up LEDs with a button press)

Hello
generally spoken: avoid the usage of the delay() function. This function blocks the execution of sketch.
Take a look into the design of a timer() funktion by using the IDE example BLINKWITHOUTDELAY as basis.

You have two pins that you read; is that two buttons or a button and something else?

Please give a better description.
Please povide full code,
Please explain how the buttons are wired

  1. Between Vcc and pin? In which case the question is if you’re using pull-down resistors?
  2. Between pin and GND? In which case the question is if you’re using internal or external pull-up resistors?

Here is the full code :

int pinCapteur = 8;
int vert = 1;
int orange = 2;
int rouge = 3;
int ledv = 7;
int ledr = 6;
int bouton = 4;
int etat = 0;

void setup(){
  pinMode(3,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(1,OUTPUT);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
  pinMode(bouton, INPUT);
  pinMode(ledv, OUTPUT);
  pinMode(ledr, OUTPUT);
}
void loop(){
  if (digitalRead(pinCapteur) == HIGH) {
  digitalWrite(vert, LOW);
  digitalWrite(orange, HIGH);
  delay(4000);
  digitalWrite(orange, LOW);
  digitalWrite(rouge, HIGH);
  delay(20000);
  }
  else if (digitalRead(bouton) == HIGH) {
    delay(20000);
    digitalWrite(ledv, HIGH);
    digitalWrite(ledr, LOW);
    delay(15000);
    digitalWrite(ledv, LOW);
    digitalWrite(ledr, HIGH);
  }
  else {
  digitalWrite(vert, HIGH);
  digitalWrite(orange, LOW);
  digitalWrite(rouge, LOW);
  digitalWrite(ledv, LOW);
  digitalWrite(ledr, HIGH);
  }  
  delay(10);
}

It’s a bad idea using pin 1 for an LED. That’s one of the Serial pins. It’s also confusing giving the pins names like rouge and then using the pin numbers instead of the names in some places. You declare pin 9 as input but then it’s never used for anything.

Instead of just “not working” please describe exactly what you expect to happen and what does happen instead.

Steve

I execpted that the both part work , like the start code was doing its just too press a buton and it enter in a loop for simulating a traffic light , i added the 2nd part that is also a button that will press to make pedestrians light that turn green after a certain time when pressed , so i added this 2nd part and everything stopped working , pressing did nothing etc… and no errors

So you are saying that you can press either of the buttons but none of the LEDs ever light up?

In that case you have messed up the connections and/or wiring. Or you are just not waiting long enough. When ‘bouton’ is pressed you do NOTHING for a full 20 seconds before any LED is switched on.

Steve

i mean that when i added the 2nd part of the program led didn’t even lighted up

I tried not to change the logic of your program.

I took out the delays in most places calling a dummy noDelay function in their stead.

I added printing statements.

I moved the problematic pin 1 to pin 5, and changed your int pin names to constants. And used those constants where you used numbers.

I rearranged things so they are in one order in the declarations and setup().

It seems to function correctly. Either button runs its code section, no button runs the final else part.

I think the original delays are just killing the response OR you have a wiring error. I looked at your picture until my eyes hurt, admittedly that only lasted about 400 milliseconds.

Just check your wiring.

I attached no LEDs as it is entirely unclear what you are trying to make it do.

// some code

void noDelay(unsigned long dummy)
{
  // no delay!
}


const int vert = 5;  // don't use pin 1
const int orange = 2;
const int rouge = 3;
const int ledv = 7;
const int ledr = 6;

const int pinCapteur = 8;
const int bouton = 4;

void setup() {
  pinMode(vert, OUTPUT);

  pinMode(orange, OUTPUT);
  pinMode(rouge, OUTPUT);
  pinMode(ledv, OUTPUT);
  pinMode(ledr, OUTPUT);

  pinMode(pinCapteur, INPUT);
  // pinMode(9, INPUT);
  pinMode(bouton, INPUT);

  Serial.begin(115200);
  Serial.println("Bonjour!");
}

void loop() {
  if (digitalRead(pinCapteur) == HIGH) {

    Serial.println("pinCapteur bien!");

    digitalWrite(vert, LOW);
    digitalWrite(orange, HIGH);
    noDelay(4000);
    digitalWrite(orange, LOW);
    digitalWrite(rouge, HIGH);
    noDelay(20000);
  }
  else if (digitalRead(bouton) == HIGH) {

    Serial.println("bouton bien!");

    noDelay(20000);
    digitalWrite(ledv, HIGH);
    digitalWrite(ledr, LOW);
    noDelay(15000);
    digitalWrite(ledv, LOW);
    digitalWrite(ledr, HIGH);
  }
  else {

    Serial.println("else...");

    digitalWrite(vert, HIGH);
    digitalWrite(orange, LOW);
    digitalWrite(rouge, LOW);
    digitalWrite(ledv, LOW);
    digitalWrite(ledr, HIGH);
  }
  delay(500);  // yes, just to slow it down
}

You could make the noDelay work as a faster version like

void noDelay(unsigned long dummy)
{
  delay(dummy / 100);  // delay, but not so massively
}

Je n’ai pas de vie.

I wired up some LEDs and used the alternate faster but not instant delay (dummy / 100)
and it does three different things like maybe you want.

Oh, shouldn’t matter: I’m using the UNO for this.

HTH

a7

thanks for your time but now the things is that the solo green and red light are blinking
and basically all the things that dont work is when i add the 2nd button in a if

Please try my version of your code. It works.

Use the second version of myDelay() I supplied where we just speed things up a bit.

Watch the serial monitor mark the flow through the program.

If that doesn’t work, you’ve got some wiring thing going on. I built the circuitry using my best guess at how yours is wired,

1, LEDs come on when HIGH.

2, buttons are pulled down normally and go HIGH when pressed.

HTH

a7

i used your program and added normal delays because your nodelays wasn’t working for me , not every part works but my new problem is that i need to when the end of the loop to make the other button work

That is odd.

In my lab right now, the complete program below will

“pinCapteur” button pressed section:

orange blinking
rouge ON
others off

“bouton” button pressed section:

vert ON
ledv, ledr alternating

else:

vert ON
ledr ON
others off.

Try the below as is and carefully. I am using the UNO Arduino, what model are you using? I don’t think that’s you problem, but anyway.

I made a few additional modifications which to the best of my ability should affect only the timing and not the logic of your program.

// some code

void noDelay(unsigned long dummy)
{
  // quicker delay!

  delay(dummy / 10);
}

# define K4000	6000
# define K20000	6000
# define K15000	6000

const int vert = 5;  // don't use pin 1
const int orange = 2;
const int rouge = 3;
const int ledv = 7;
const int ledr = 6;

const int pinCapteur = 8;
const int bouton = 4;

void setup() {
  pinMode(vert, OUTPUT);

  pinMode(orange, OUTPUT);
  pinMode(rouge, OUTPUT);
  pinMode(ledv, OUTPUT);
  pinMode(ledr, OUTPUT);

  pinMode(pinCapteur, INPUT);
  // pinMode(9, INPUT);
  pinMode(bouton, INPUT);

  Serial.begin(115200);
  Serial.println("Bonjour!");
}

void loop() {
  if (digitalRead(pinCapteur) == HIGH) {

    Serial.println("pinCapteur bien!");

    digitalWrite(vert, LOW);
    digitalWrite(orange, HIGH);
    noDelay(K4000);
    digitalWrite(orange, LOW);
    digitalWrite(rouge, HIGH);
    noDelay(K20000);
  }
  else if (digitalRead(bouton) == HIGH) {

    Serial.println("bouton bien!");

    noDelay(K20000);
    digitalWrite(ledv, HIGH);
    digitalWrite(ledr, LOW);
    noDelay(K15000);
    digitalWrite(ledv, LOW);
    digitalWrite(ledr, HIGH);
  }
  else {

    Serial.println("else...");

    digitalWrite(vert, HIGH);
    digitalWrite(orange, LOW);
    digitalWrite(rouge, LOW);
    digitalWrite(ledv, LOW);
    digitalWrite(ledr, HIGH);
  }
  delay(10);  // yes, just to slow it down
}

a7

This works perfectly even that the delay are too fast but with some change on my side it will be perfect thanks a lot , but do you know how to activate the both loops withtout waiting the end of one ? like run the 2 at the same time

Well that’s a relief.

Yes, yes I do. This is a perfect place to learn about doing more than one thing at a time, which is an often problem around here.

The usual good recommendation is to throw out your code and start again using an approach based on “state machines”.

And to get rid of delay() as the basis for timing. delay() means your machine is literally doing nothing, as you have seen, nothing at all for the entire 20 seconds or whatever. That is what moved me to just speed things along a bit so we could see that the logic was at least correct.

The mechanism is formally called a “finite state machine”. Usually abbreviated “FSM” (finite state machine).

I just googled “arduino FSM traffic lights”. Evidently this is popular problem to solve using FSMs.

I highly recommend avoiding using any FSM libraries your search might turn up - it is good to create and appreciate the mechanism by doing it through your own code.

There’s a crap-ton of examples, one or the other will catch your imagination and match your learning style.

We can help if you get stuck on any aspect of this and look forward to doing, but it is not that hard to pick up, I promise.

HTH and bonne chance!

a7

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.