create a button on arduino Mega

Hi guys,
Sorry for my english.
I'm trying to do a intelligent traffic light with Arduino. Now I have a normal traffic light running, but im having problems to the traffic light can feel the cars. To the system ill use buttons like the sensors, and i`m trying implement this buttons, but I'm not getting do this.
The problem:
I want the traffic light change the color when the button stay pressed for more than 3 seconds.
I tried a lot of things, but nothing worked.
I firstly tried use the millis like:

if(millis() - time) >3000)

but nothing happened.
and I want to do something like:

if(button pressed for more three second){

change the color traffic light;}
else
continue with the same color

thanks for the help.

I firstly tried use the millis like:

if(millis() - time) >3000)

but nothing happened.

Something needs to set time (which needs to be of the correct time), and something needs to follow that statement. Without seeing what follows that statement, the only explanation for why it didn't works that is possible is that the code is wrong. Post it if you want to know why. All of your code, that is.

I just posted similar code for another forum member and have hopefully adapted it (untested) to what you want.

const long debounceDelay = 20; //The button debounce time

// Button = pin number to read
int checkButton(int Button) {
    int buttonState = digitalRead(Button);    // Read button
    if (buttonState == HIGH) {                // If button pressed then wait a bit to allow for debounce
        long Time = millis(); 
        while ((millis() - Time) < debounceDelay) {    
        }
        Time = millis(); 
        while ((millis() - Time) < 3000) {    
            buttonState = digitalRead(Button);      // Read button again
            if (buttonState == LOW) {                // If button not pressed then exit early
                return LOW;
            }
        }
        return buttonState;                     // Button held down for >3000 milliseconds
    }
    else {
        return LOW;
    }
}

Thank you Riva!! You helped me a lot!

bezerra:
Thank you Riva!! You helped me a lot!

Glad to help

Hi guys,

I`m very grateful for the help, but could not adapt the code of RIVA in my project.

This is my code. I tried to adjust the code RIVA but could not run the program.

//semáforo controlado pelo fluxo
const int BTNP1 = 2;   //SENSOR VIA PRINCIPAL 1 (FLUXO)
const int BTNP2 = 3;   //SENSOR VIA PRINCIPAL 2 (FLUXO)
const int BTNS1 = 4;   //SENSOR VIA SECUNDÁRIA 1(FLUXO)
const int BTNS2 = 5;   //SENSOR VIA SECUNDÁRIA 2(FILA CHEIA)
const int VDEP = 24;   //SEMÁFORO VERDE DA VIA PRINCIPAL
const int AMP = 23;    //SEMÁFORO AMARELO DA VIA PRINCIPAL
const int VERMP= 22;   //SEMÁFORO VERMELHO DA VIA PRINCIPAL
const int VDES = 25;   //SEMÁFORO VERDE DA VIA SECUNDÁRIA
const int AMS = 26;    //SEMÁFORO AMARELO DA VIA SECUNDÁRIA
const int VERMS = 27;  //SEMÁFORO VERMELHO DA VIA SECUNDÁRIA
int buttonstate;
const long debounceDelay = 20;


int semaf[]={ VDEP, AMP, VERMP, VDES, AMS, VERMS };
int i=0;  
int length = sizeof semaf/sizeof *semaf;
void setup() {
for(int i=0;i<length ;i++)
pinMode(semaf[i], OUTPUT);
pinMode(BTNP1,INPUT);
pinMode(BTNP2,INPUT);
pinMode(BTNS1,INPUT);
pinMode(BTNS2,INPUT);
}

void loop(){

if (i==0){                                      //inicio
digitalWrite(VERMP, HIGH);                        //inicio
digitalWrite(VERMS, HIGH);                        //inicio
delay(5000);                                      //inicio
digitalWrite(VERMP,LOW);
i=i+1;}                                            
  
  else;
 mudaverdep();
 mudavermelhop();
 mudaverdes();
 mudavermelhos();
}

void mudaverdep(){
  digitalWrite(VERMP, LOW);
  digitalWrite(VDEP, HIGH);
  delay (5000);}


void mudavermelhop(){
  digitalWrite(VDEP, LOW);
  digitalWrite(AMP, HIGH);
  delay (1000);  
  digitalWrite(AMP, LOW);
  digitalWrite(VERMP, HIGH);
delay (1000);}


void mudaverdes(){
  digitalWrite(VERMS, LOW);
  digitalWrite(VDES, HIGH);
  delay (5000);}

void mudavermelhos(){  
  digitalWrite(VDES, LOW);
  digitalWrite(AMS, HIGH);
  delay (1000);  
  digitalWrite(AMS, LOW);
  digitalWrite(VERMS, HIGH);
delay (1000);}

What it takes to run?

This code is for a traffic light, releasing the flow, feeling the cars on the main road, and release to full queue (secondary route).

I`m very grateful for the help, but could not adapt the code of RIVA in my project.

This is my code. I tried to adjust the code RIVA but could not run the program.

Why couldn't you run the program?

Why couldn't you run the program?

because I could not adapt the RIVA`s code on my code

A bit late to this and you have most likely fixed it but the problem will probably be due to the delay statements in your main loop. These will make the button press detection very very unresponsive.

A bit late to this and you have most likely fixed it but the problem will probably be due to the delay statements in your main loop. These will make the button press detection very very unresponsive.

So RIVA what do you think i have to Do? I not more have idea. I'm new in programming with arduino.

bezerraicarus:
So RIVA what do you think i have to Do? I not more have idea. I'm new in programming with arduino.

Start by incorporating simple (not 3 second delay) button press code or at the very least put some comments in the program where you would read the buttons and what you expect to happen when the button is pressed.

if (i==0){                                      //inicio
digitalWrite(VERMP, HIGH);                        //inicio
digitalWrite(VERMS, HIGH);                        //inicio
delay(5000);                                      //inicio
digitalWrite(VERMP,LOW);
i=i+1;}

This only seems to be run once in loop() so why not put it in setup() instead?

//semáforo controlado pelo fluxo
const int BTNP1 = 2;   //SENSOR VIA PRINCIPAL 1 (FLUXO)
const int BTNP2 = 3;   //SENSOR VIA PRINCIPAL 2 (FLUXO)
const int BTNS1 = 4;   //SENSOR VIA SECUNDÁRIA 1(FLUXO)
const int BTNS2 = 5;   //SENSOR VIA SECUNDÁRIA 2(FILA CHEIA)
const int VDEP = 24;   //SEMÁFORO VERDE DA VIA PRINCIPAL
const int AMP = 23;    //SEMÁFORO AMARELO DA VIA PRINCIPAL
const int VERMP= 22;   //SEMÁFORO VERMELHO DA VIA PRINCIPAL
const int VDES = 25;   //SEMÁFORO VERDE DA VIA SECUNDÁRIA
const int AMS = 26;    //SEMÁFORO AMARELO DA VIA SECUNDÁRIA
const int VERMS = 27;  //SEMÁFORO VERMELHO DA VIA SECUNDÁRIA
int buttonstate;
const long debounceDelay = 20;


int semaf[]={ VDEP, AMP, VERMP, VDES, AMS, VERMS };
int i=0;  
int length = sizeof semaf/sizeof *semaf;
void setup() {
for(int i=0;i<length ;i++)
pinMode(semaf[i], OUTPUT);
pinMode(BTNP1,INPUT);
pinMode(BTNP2,INPUT);
pinMode(BTNS1,INPUT);
pinMode(BTNS2,INPUT);

                                      
digitalWrite(VERMP, HIGH);            //start in red, in case of blackouts            
digitalWrite(VERMS, HIGH);                        
delay(5000);                                      
digitalWrite(VERMP,LOW);
                  
}

void loop(){
 mudaverdep();
 mudavermelhop();
 mudaverdes();
 mudavermelhos();
}

void mudaverdep(){
  digitalWrite(VERMP, LOW);
  digitalWrite(VDEP, HIGH);
  delay (5000);}


void mudavermelhop(){
  digitalWrite(VDEP, LOW);
  digitalWrite(AMP, HIGH);
  delay (1000);  
  digitalWrite(AMP, LOW);
  digitalWrite(VERMP, HIGH);
delay (1000);}


void mudaverdes(){
  digitalWrite(VERMS, LOW);
  digitalWrite(VDES, HIGH);
  delay (5000);}

void mudavermelhos(){  
  digitalWrite(VDES, LOW);
  digitalWrite(AMS, HIGH);
  delay (1000);  
  digitalWrite(AMS, LOW);
  digitalWrite(VERMS, HIGH);
delay (1000);}

I had not thought about RIVA.
Have I to adapt your code in the loop?