Pushbutton not being recognized

Good afternoon, I have this code here where I want to simulate a car and pedestrian semaphore, and, when i press the pedestrian button it goes right to green for pedestrian.

Unfortunately, the arduino project is not recognizing when i press the button, why? Thank you,
Best Regards,

(vermelho stands for red, amarelo for yellow and verde for green)

int vermelhocarro = 9;

int amarelocarro = 8;

int verdecarro = 7;

int vermelhopeao = 5;

int verdepeao = 4;

int botao = 6;

int sirene = 3;

int statusbotao = 0;

void setup(){

  Serial.begin(9600);

  pinMode(vermelhocarro, OUTPUT);

  pinMode(amarelocarro, OUTPUT);

  pinMode(verdecarro,  OUTPUT);

  pinMode(botao, INPUT_PULLUP);

  pinMode(vermelhopeao, OUTPUT);

  pinMode(verdepeao, OUTPUT);

  pinMode(sirene, OUTPUT);

 

}

void semaforoNORMAL(){

//INICIO:

Serial.print("INICIO");

Serial.print('\n');

  digitalWrite(vermelhocarro, HIGH);

  digitalWrite(verdepeao, HIGH);

  delay(10000);

digitalWrite(verdepeao,  LOW);

delay (500);

digitalWrite(verdepeao, HIGH);

digitalWrite(sirene, HIGH);

 delay(1000);

digitalWrite(verdepeao,  LOW);

digitalWrite(sirene, LOW);

delay (500);

digitalWrite(verdepeao, HIGH);

digitalWrite(sirene, HIGH);

 delay(1000);

digitalWrite(verdepeao,  LOW);

digitalWrite(sirene, LOW);

delay (500);

digitalWrite(verdepeao, HIGH);

digitalWrite(sirene, HIGH);

delay(1000);

digitalWrite(verdepeao,  LOW);

digitalWrite(sirene, LOW);

delay (500);

digitalWrite(vermelhocarro,  LOW);

 

digitalWrite(vermelhopeao,  HIGH);

digitalWrite(verdecarro, HIGH);

for (int i = 0; i < 150; i++)

{

    byte statusbotao = digitalRead(botao);

if (botao == HIGH){

Serial.print("BOTAO PRESSIONADO");

Serial.print('\n');

Serial.print('\n');

goto VERMELHOPEAO;

}

else{

Serial.print("BOTAO NAO PRESSIONADO");

Serial.print('\n');}

delay(100);

}

//delay(15000);

VERMELHOPEAO:

digitalWrite(verdecarro,  LOW);

digitalWrite(amarelocarro, HIGH);

 delay(5000);

digitalWrite(amarelocarro,  LOW);

digitalWrite(vermelhopeao,  LOW);

}

//void semaforoVERDE(){

//  

//}

void loop(){

semaforoNORMAL();

//statusbotao = digitalRead(botao);

//if (botao == 1){semaforoNORMAL};

//else semaforoVERDE;

 

 

}


You have 15 seconds of delay() ("do nothing") happening before the program gets a few milliseconds to read your button.

Read the button constantly (every loop) then call a function call when the button is pressed.

Can't be bothered to read unformatted code. Use Tools/Auto Format and also reduce the pointless vertical white space.

        byte statusbotao = digitalRead(botao);

        if (botao == HIGH)

You are reading the status of the button pin but testing the actual number of the button pin, In your code this will always be 6. Test the status that you read in the previous line. Why do it 150 times, each with a delay() of 100 milliseconds between them ?

There may be other problems but this one is fundamentally wrong

Incidentally, the use of goto is frowned upon as it can cause confusion and if not needed

  • Wire switches and LEDs as seen here.

What @LarryD said plus:


You have the input defined as INPUT_PULLUP, and when you press the button it connects the input to + rail, so whether it's pushed or not, it's always going to be high when you read the input. That red wire needs to go to the - rail so that reading the input changes when you press the button. Also need to remove the pull down resistor in my screen snip.

Are you sure? I think it will depend which of the button's 4 pins are permanently connected, and which are connected only when the button is pushed.

If the top two pins are permanently connected and the bottom two pins are permanently connected, then it won't work for the reason you described.

But if the left two pins are permanently connected and the right two pins are permanently connected, it should work. (The 1K pulldown resistor should easily overcome the internal pull-up resistor when the button is not pressed.)

@Pedro_Oliveira to avoid connecting 4-pin buttons incorrectly, always make your connections to one diagonal pair of pins and leave the other diagonal pair of pins unconnected. And you don't need to add an external resistor if you are using the internal pull-up resistor on the Arduino pin.

In this case, the goto does nothing anyway. The code will execute exactly the same without it, I think. It would skip the code in the else clause, which would happen anyway. So the goto can be removed without any need to make other changes.

It's hard to be sure because of the terrible indentation, but I think there is a missing }, and the code posted would not compile.

Not now :-) Thank you for making me think again about my unstated assumption.

I'd assumed: what you said below.

Hi, @Dave_Lowther

Do you have a DMM? Digital MultiMeter?

Thanks... Tom.... :smiley: :+1: :coffee: :australia:

Yes I do, but I think perhaps you meant to ask the OP?

:+1: :+1: :+1: Caffiene hadn't cut in yet.

Thank you very much. This solved my problem! God bless!

I am glad that I could help