Hi guys.
I borrowed my brother's UNO a while back to start learning how to build a light controller for my jeep. I've figured out how to work multiple lights in binary (stay on/off individually) or to alternatively strobe two lights for a chase light (think cop car), but i've completely hit a wall trying to marry the two concepts. I also tried building off the blink without delay example for a proof of concept, but I couldn't get that to jive either. Could anyone give me some pointers as to where I'm going wrong?
const int strobebutt = 2;
const int bumpbutt = 3;
const int strobe1 = 13;
const int strobe2 = 11;
const int bumplight = 8;
int strobePushCounter = 0;
volatile int strobeState = 0;
int lastStrobeState = 0;
int bumpPushCounter = 0;
volatile int bumpState = 0;
int lastBumpState = 0;
void setup() {
pinMode(strobebutt,INPUT_PULLUP);
pinMode(bumpbutt,INPUT_PULLUP);
pinMode(strobe1,OUTPUT);
pinMode(strobe2,OUTPUT);
pinMode(bumplight,OUTPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(2),strobe,HIGH);
attachInterrupt(digitalPinToInterrupt(3),bumper,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
}
void strobe(){
strobeState = digitalRead(strobebutt);
// compare the buttonState to its previous state
if (strobeState != lastStrobeState) {
// if the state has changed, increment the counter
if (strobeState == HIGH) {
// if the current state is HIGH then the button went from off to on:
strobePushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(strobePushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastStrobeState = strobeState;
// turns on the LED every other button push by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (strobePushCounter % 2 == 0) {
digitalWrite(strobe1,HIGH); // begin strobe on light 1
delay(50);
digitalWrite(strobe1,LOW);
delay(50);
digitalWrite(strobe1,HIGH);
delay(50);
digitalWrite(strobe1,LOW);
delay(50);
digitalWrite(strobe1,HIGH);
delay(50);
digitalWrite(strobe1,LOW);
delay(50);
digitalWrite(strobe1,HIGH);
delay(50);
digitalWrite(strobe1,LOW);
delay(50);
digitalWrite(strobe2,HIGH); // begin strobe on the light 2
delay(50);
digitalWrite(strobe2,LOW);
delay(50);
digitalWrite(strobe2,HIGH);
delay(50);
digitalWrite(strobe2,LOW);
delay(50);
digitalWrite(strobe2,HIGH);
delay(50);
digitalWrite(strobe2,LOW);
delay(50);
digitalWrite(strobe2,HIGH);
delay(50);
digitalWrite(strobe2,LOW);
delay(50);
} else {
digitalWrite(strobe1, LOW);
digitalWrite(strobe2, LOW);
}
}
void bumper(){
bumpState = digitalRead(bumpbutt);
// compare the buttonState to its previous state
if (bumpState != lastBumpState) {
// if the state has changed, increment the counter
if (bumpState == HIGH) {
// if the current state is HIGH then the button went from off to on:
bumpPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(bumpPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastBumpState = bumpState;
// turns on the LED every other button push by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (bumpPushCounter % 2 == 0) {
digitalWrite(bumplight, HIGH);
} else {
digitalWrite(bumplight, LOW);
}
}