Hi Folks, I'm new to arduino and attempting to write my own Ghostbusters proton pack code so I can understand how everything works . To start with, I put together a simple sketch to control 3 LED lights on the wand.
However, I'm getting very odd behaviour - the code below is what I'm using.
The goal here is to light LEDs on pins 2 and 4, and make the LED on pin 3 blink.
However, when the switch I'm using is ON, all 3 lights come on and stay on. When it's OFF, LEDs on pin 2 and 4 go off, but the LED on pin 3 starts to blink. I don't understand this - the blink code should NOT execute when the switch is off.
Any help would be appreciated.
// for led triggers
#define HIGH 0x1
#define LOW 0x0
int GunTipLEDPin = 2;
int GunTopLEDPin = 3;
int GunRearLEDPin = 4;
int ActivatePin = A0;
const unsigned long wandSlowFlashInterval = 1000; // interval at which we flash the top led on the wand
unsigned long prevFlashMillis = 0; // controls various blinking lights
bool flashState = false;
void setup(){
pinMode(GunTipLEDPin, OUTPUT);
pinMode(GunTopLEDPin, OUTPUT);
pinMode(GunRearLEDPin, OUTPUT);
pinMode(ActivatePin, INPUT);
}
void loop(){
if (digitalRead(ActivatePin)) {
digitalWrite(GunTipLEDPin, HIGH);
digitalWrite(GunRearLEDPin, HIGH);
GunTopLED();
}
else{
digitalWrite(GunTipLEDPin, LOW);
digitalWrite(GunTopLEDPin, LOW);
digitalWrite(GunRearLEDPin, LOW);
}
delay(1);
}
// END OF MAIN LOOP
//----------
void GunTopLED() {
// get the current time
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - prevFlashMillis) >= wandSlowFlashInterval) {
prevFlashMillis = currentMillis;
if (flashState == false){
digitalWrite(GunTopLEDPin, HIGH);
flashState = true;
}else{
digitalWrite(GunTopLEDPin, LOW);
flashState = false;
}
}
}