Button

Hello world... ::slight_smile:

I'm making this board, with a button that would switch a trafficlight to a different code.
Could some of you please help me with the code? I've tried over and over again, but my button always freezes the trafficlight..

#define ledPin(redV) = 7
#define ledPin(redH) = 4
#define ledPin(yellowV) = 8
#define ledPin(yellowH) = 5
#define ledPin(greenV) = 3
#define ledPin(greenH) = 6
#define inputPin(knap) = 2

int ledPinredV = 7;
int ledPinredH = 4;
int ledPinyellowV = 8;
int ledPinyellowH = 5;
int ledPingreenV = 3;
int ledPingreenH = 6;
int inputPin = 2;
volatile int state = LOW;
int var = 1;

void setup()
{  
  pinMode(ledPinredV, OUTPUT);
  pinMode(ledPinredH, OUTPUT);
  pinMode(ledPinyellowV, OUTPUT);
  pinMode(ledPinyellowH, OUTPUT);
  pinMode(ledPingreenV, OUTPUT);
  pinMode(ledPingreenH, OUTPUT);
  pinMode(inputPin, INPUT);
  attachInterrupt(0, loop, RISING);
}

void loop()
{
    digitalWrite(inputPin, state); 
    switch (var) {
    case 1:
      digitalWrite(ledPinredV, HIGH);
      digitalWrite(ledPingreenH, HIGH);
      delay(10000);
      digitalWrite(ledPingreenH, LOW);
      digitalWrite(ledPinredV, HIGH);
      digitalWrite(ledPinyellowH, HIGH);
      delay(2500);
      digitalWrite(ledPinredV, HIGH);
      digitalWrite(ledPinyellowH, LOW);
      digitalWrite(ledPinredH, HIGH);
      delay(1000);
      digitalWrite(ledPinredV, LOW);
      digitalWrite(ledPinyellowV, HIGH);
      digitalWrite(ledPinredH, HIGH);
      delay(2500);
      digitalWrite(ledPinyellowV, LOW);
      digitalWrite(ledPingreenV, HIGH);
      digitalWrite(ledPinredH, HIGH);
      delay(10000);
      digitalWrite(ledPingreenV, LOW);
      digitalWrite(ledPinredH, HIGH);
      digitalWrite(ledPinredH, HIGH);
      digitalWrite(ledPingreenV, HIGH);
      delay(10000);
      digitalWrite(ledPinredH, HIGH);
      digitalWrite(ledPingreenV, LOW);
      digitalWrite(ledPinredH, HIGH);
      digitalWrite(ledPinyellowV, HIGH);
      delay(2500);
      digitalWrite(ledPinredH, HIGH);
      digitalWrite(ledPinyellowV, LOW);
      digitalWrite(ledPinredV, HIGH);
      delay(1000);
      digitalWrite(ledPinredH, LOW);
      digitalWrite(ledPinyellowH, HIGH);
      digitalWrite(ledPinredV, HIGH);
      delay(2500);
      digitalWrite(ledPinyellowH, LOW);
      digitalWrite(ledPingreenH, HIGH);
      digitalWrite(ledPinredV, HIGH);
      delay(10000);
      digitalWrite(ledPingreenH, LOW);
      digitalWrite(ledPinredV, HIGH);
    case 0:
      digitalWrite(ledPingreenH, HIGH);
      digitalWrite(ledPingreenV, HIGH);
      noInterrupts();     
 }
}

Here are some suggestions that I hope will help:

Don't use loop as your interrupt handler. I suggest you don't use an interrupt at all.
In loop, check for the state of the switch and call some functions to handle the state of the lights.

You may want to write a function like:
setLight( int color, int delay)
where you pass RED, YELLOW or GREEN and the number of milliseconds to delay
You can make your code easier to read by defining constants :
#define RED 0
#define YELLOW 1
#define GREEN 2

Example: setLight(RED,2500)

Try to write and test this function without worrying about the switch at first, just get it to sequence through some colors.

When you have that working you can add the switch logic in loop:
If(digitalRead(inputPin))
// do switch pressed sequence

have fun!

p.s.
If you want to turn on multiple leds, your function can do something like this:

#define RED 1
#define YELLOW 2
#define GREEN 4

setLight( int color, int delay)

if (color & RED)
// turn red led on
else
// turn red led off

if (color & YELLOW)
// turn yellow led on
else
// turn yellow led off

// same for green

Usage : setLight( RED + YELLOW, 2500);
// turns red and yellow light on for 2.5 seconds

thanks alot :slight_smile:

i'll try it out