I have a Y-Wing model I've built where I've installed LEDs to act as engines, lasers and canons. The engine portion works great, but my problem arises with the button reads.
What I want to happen is when I press button 1 (laserSWITCH) and hold, the laser LEDs fire repeatedly while the button is pushed. And when button 2 (IonSWITCH) is pushed and released, I want the canon LED to light up for about 1/2 sec. Both of these codes work just fine by themselves in their own sketch, but when they're combined in the same sketch together, no matter which button I push, only the laser LEDs come on, and it blinks about every 1/2 second. And the canon LED doesn't light at all. I'm not sure where my problem lies in the coding.
I know my wiring is correct, because they both work fine in their own sketches without the other present.
I have been searching the internet for days, trying different things based on codes I find, but nothing gets it to work correctly.
// Y-Wing code by Christopher Olson 2018
#include <Adafruit_NeoPixel.h>
#define IonLED 0
#define PIN 1
#define laserLED 2
#define laserSWITCH 3
#define IonSWITCH 4
int ledState = LOW;
long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long interval = 120;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(laserLED, OUTPUT);
pinMode(IonLED, OUTPUT);
pinMode(laserSWITCH, INPUT_PULLUP);
pinMode(IonSWITCH, INPUT_PULLUP);
digitalWrite(laserSWITCH, HIGH);
digitalWrite(laserLED, LOW);
digitalWrite(IonSWTICH, HIGH);
digitalWrite(IonLED, LOW);
strip.begin();
strip.setPixelColor(0, 255, 255, 0); // pixel color set to yellow
strip.show();
void engineBrighten();
// begin brightening 0 to 255
int i = 0;
int c = 255;
do {
i = i + 1;
c = c - 1;
strip.setPixelColor(0, 255, c, 0); // set pixel color
strip.show();
delay(25); // speed of color fade
strip.setBrightness(0 + i); // brings up brightness from 0 to full
strip.show();
delay (15); // speed of brightness adjustment
} while (i < 255, c > 0);
delay(1500);
}
void loop()
{
// Laser Button Push
if (! digitalRead(laserSWITCH)) { // laser button is pushed
//begin laser LED
unsigned long currentMillis1 = millis();
if (currentMillis1 - previousMillis1 > interval) {
previousMillis1 = currentMillis1;
if (ledState == LOW)
ledState = 255;
else
ledState = LOW;
digitalWrite(laserLED, ledState);
}
} else digitalWrite(laserLED, LOW);
// Ion Button Push
if (! digitalRead(IonSWITCH)) { // laser button is pushed
//begin Ion LED
unsigned long currentMillis3 = millis();
if (currentMillis3 - previousMillis3 > 400) {
previousMillis3 = currentMillis3;
if (ledState == LOW)
ledState = 255;
else
ledState = LOW;
digitalWrite(IonLED, ledState);
}
} else digitalWrite(IonLED, LOW);
// begin engine flicker
strip.setBrightness(random(25, 255)); // random brightness adjust for flicker effect
strip.show();
unsigned long currentMillis2 = millis();
if (currentMillis2 - previousMillis2 > (random(500,1200))) {
previousMillis2 = currentMillis2;
}
}
[code]