Problem with a push-button control in a trafic light code

Hi, I´m new here and i´m doing a college project (pretty basic).
In my project I have to do a traffic light with traffic for pedestrians (including button) and cars.
I´ve mounted the LED´s and they work perfectly, but I can´t make the pussh bottom work.

Here´s the code that I´ve used:

int carRed = 12; // definindo os leds dos carros
int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // definindo os leds de pedestres
int pedGreen = 8;
int button = 2; // pino do botao
int crossTime = 10000; // tempo de travessia
unsigned long changeTime; // tempo desde a ultima vez que o botao foi pressionado

void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT);
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
}

void loop() {
int state = digitalRead(button);
/verifica se o botao foi pressionado e se faz mais de 5 seg desde a ultima vez/
if (state == HIGH && (millis() - changeTime) > 5000) {
//chama a funcao que altera as luzes
delay(4000);
changeLights();
}
}

void changeLights(){
digitalWrite(carGreen, LOW); //apaga carGreen
digitalWrite(carYellow, HIGH); //acende carYellow
delay(4000);
digitalWrite(carYellow, LOW); //apaga carYellow
digitalWrite(carRed, HIGH); //acende carRed
digitalWrite(pedRed, LOW); //apaga pedRed
digitalWrite(pedGreen, HIGH); //acende pedGreen
delay(crossTime); //aguarda crossTime

//pisca pedGreen
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}

digitalWrite(pedRed, HIGH); //acende pedRed
digitalWrite(carRed, LOW); //apaga carRed
digitalWrite(carGreen, HIGH); //acende carGreen

changeTime = millis();
}

I´ve bough a Arduino Uno few days ago and I’m still not used to working with him neither understand perfectly their codes.
Could you please help me ?

I dont now why the button dont work, I think its because of the code. Or if I have to download something to put in the library.

If you have a link or something that can help me (with a new code or an explanation for my problem) please leave it on a comment

Thank you

How is the button wired? Here is a good way to wire a button. Note that the digital input is LOW when the button is pushed. You will have to change your code to read the button. You can enable the internal pullup and eliminate the 10K resistor
( pinMode(pin, INPUT_PULLUP):wink:

This link was my guideline, this is how I´ve mounted my project.

http://baduino.blogspot.pt/2011/11/projeto-4-semaforo-interativo.html

I can´t really understand waht you´re trying to explain me :( I´m really a newbie on this kind of project.

The circuit in that link is a valid way to wire then switch. The circuit that I show has the advantage of using the internal pullup and allows hardware debounce. A potential problem with your code is in the delays. It will not be very responsive as the MPU is not doing anything but waiting during a delay. Take a look at the blink without delay example in the IDE (File, Examples, Digital) to time events and do other things while waiting. Also look into finite state machine programming.

Now I am not sure how much we should help you on this project if it is supposed to be your own work, but ...

This is such a common project - and is asked so often - that I have been working on a definitive code for it, but doing it slowly as - I of course do not have to submit it for a deadline. :D

The following prototype code implements timed delays for independent events - not quite what you seek, but certainly the basis for doing it properly (which is to say, without ever using the "delay()" function call). It also implements an extremely robust button debounce and in the better engineering practice, expects the button to be wired from the pin to ground and use the internal pull-up.

// Blink without "delay()" - multi!

const int led1Pin =  13;    // LED pin number
const int led2Pin =  10;
const int led3Pin =  11;
const int button1 =  4;
int led1State = LOW;        // initialise the LED
int led2State = LOW;
int led3State = LOW;
char bstate1 = 0;
unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check 
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(led2Pin, OUTPUT);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button1, INPUT);      
  digitalWrite(button1,HIGH);        // internal pullup all versions
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW; 
    } 
    digitalWrite(led1Pin, led1State);
  } 

  // Act if the latter time (ms) has now passed on this particular counter,
  if (timeout(&count2, 300UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
    }
    else {
      led2State = LOW; 
    } 
    digitalWrite(led2Pin, led2State);
  } 

  if (timeout(&count3, 77UL )) {
    if (led3State == LOW) {
      led3State = HIGH;
    }
    else {
      led3State = LOW; 
    } 
    digitalWrite(led3Pin, led3State);
  } 
}

Of course the trick is that if people use my code, those marking this assignment in the various schools and colleges are going to be able to recognise it very quickly!