SOLVED Multiple momentary button push issue

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]

see if Nick Gammon's switch manager libary will work for you

.

ieee488:
see if Nick Gammon's switch manager libary will work for you
Gammon Forum : Electronics : Microprocessors : Switches tutorial

.

Thanks. I'll take a look.

long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;

Time variables should be unsigned long, unless you plan to travel faster than the speed of light.

#define IonLED 0
#define PIN 1

Get that crap off the hardware serial pins, so you can debug your code.

  digitalWrite(laserSWITCH, HIGH);
  digitalWrite(IonSWTICH, HIGH);

WTF? Why are you writing to INPUT pins?

  void engineBrighten();

Why do you have a function prototype in the middle of setup()?

  } while (i < 255, c > 0);

Why are you misusing the comma operator?

Add a Serial.begin() call and some Serial.print() statements to figure out what your code is doing. Debugging by guessing sucks.

I also don't know if I should've mentioned I'm using an Adafruit Trinket. I know some the the code is slightly different from their example sketches.

PaulS:

#define IonLED 0

#define PIN 1



Get that crap off the hardware serial pins, so you can debug your code.

Sorry, not sure what you mean by getting off the serial pins so I can debug.

PaulS:

digitalWrite(laserSWITCH, HIGH);

digitalWrite(IonSWTICH, HIGH);



WTF? Why are you writing to INPUT pins?

That was something I saw in an example. It works, so didn't know it was not a way to do it.

PaulS:

void engineBrighten();

Why do you have a function prototype in the middle of setup()?

again, from another example. But this part of my sketch works just fine, so didn't know it wasn't correct.

PaulS:

} while (i < 255, c > 0);

Why are you misusing the comma operator?

And another example from a sketch, and again, a portion of my sketch that works perfectly fine though.

PaulS:
Add a Serial.begin() call and some Serial.print() statements to figure out what your code is doing. Debugging by guessing sucks.

That's something I guess I need to research more to see how that works.

colson2:
I also don't know if I should've mentioned I'm using an Adafruit Trinket.

You are on the Arduino forum. If you don't specify a particular board, the assumption here is you are using an Arduino UNO. As I know nothing about the Trinket and until PaulS replies you will need to compare the UNO to the trinket to rectify/correlate some of his comments such as the pin 0 pin 1 comment; which are the USB serial monitor TX/RX pins on an Arduino UNO (see what I mean).

adwsystems:
You are on the Arduino forum. If you don't specify a particular board, the assumption here is you are using an Arduino UNO. As I know nothing about the Trinket and until PaulS replies you will need to compare the UNO to the trinket to rectify/correlate some of his comments such as the pin 0 pin 1 comment; which are the USB serial monitor TX/RX pins on an Arduino UNO (see what I mean).

Yes, that was my mistake for not mentioning that it my first post. 0-4 are all IO pins on the Trinket, though 2 can be used as serial. There's just 5- IO pins, a USB pwr pin, reset pin, gnd pin and batt pin

My problem is solved. While pins 3 & 4 are inputs, they are also USB control, so after reviewing the Trinket documentation again, using pins 0 & 2 for my button inputs and changing my LEDs to 1, 3 & 4 has resolved the issue.