Pages: [1]   Go Down
Author Topic: create a button on arduino Mega  (Read 1421 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 i`m having problems to the traffic light can feel the cars. To the system i`ll 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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2480
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
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;
    }
}
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Riva!! You helped me a lot!
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2480
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Riva!! You helped me a lot!
Glad to help
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
//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).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why couldn't you run the program?

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

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2480
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 66
Posts: 2480
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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?
Logged

There is no such thing as a stupid question but there are a lot of inquisitive idiots.

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//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?
Logged

Pages: [1]   Go Up
Jump to: