E11 Blaster - Need Help With Rotary Switch

Hey everyone,

I'm working on an E11 blaster rifle and I've got my lights and sounds working as I'd like them, however, the 3 point rotary switch that came with the blaster doesn't have a common ground.

Apparently how it makes the connection is through a ball bearing that connects the two pins.

So, Pin1 + Pin2 = Position 1; Pin2 + Pin3 = Position 2; Pin3 + Pin4 = Position 3

I would like the rotary switch to change modes - primary fire, stun, bonus sounds

Here is the switch:

Here is the blaster. The wires coming out of the mag are from my trigger and the rotary switch.

How I'm running my code is that I'm checking to see if SWITCHPIN1 and SWITCHPIN2 are high at the same time. Go down to where is says ///MODE1.

However, this isn't working. I feel like I'm close, but I'm missing something.

Any help is appreciated.

Thanks in advance!



#include "Adafruit_Soundboard.h"
#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define PIXEL_PIN     5   // Pin connected to neo pixels
#define FIREPIN       7   // Fire button 
#define SWITCHPIN1    15  // Analog
#define SWITCHPIN2    16  // Analog
#define SWITCHPIN3    17  // Analog
#define SWITCHPIN4    18  // Analog
#define LED_COUNT     30
#define NUM_LEDS      30

// Choose any two pins that can be used with SoftwareSerial to RX & TX
#define SFX_TX  1
#define SFX_RX  0
#define SFX_RST 2

Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);

#define NUM_SOUNDS 5
// The names of the found files.
// Note that they are 8 characters followed by a 3 character file
// type (.OGG or .WAV). Spaces are inserted to make up the
// 8 characters as needed.
char *soundName[NUM_SOUNDS] = {
  "EWOKHORNWAV",//0
  "LOCKLOADWAV",//1
  "PRIMARY1WAV",//2
  "STUNGUN2WAV",//3
  "BLASTER3WAV" //4
};

int buttonState = 0;
int lastButtonState = 0;
int startPressed = 0;
int endPressed = 0;
int holdTime = 0;
int idleTime = 0;

int mode1 = 0;
int mode2 = 0;
int mode3 = 0;


Adafruit_NeoPixel strip(LED_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);



void setup() {
  
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.
  
strip.begin();
strip.show();  
strip.setBrightness(255); // Set BRIGHTNESS to about 1/5 (max = 255)

pinMode(FIREPIN, INPUT_PULLUP);

pinMode(SWITCHPIN1, INPUT_PULLUP);
pinMode(SWITCHPIN2, INPUT_PULLUP);
pinMode(SWITCHPIN3, INPUT_PULLUP);
pinMode(SWITCHPIN4, INPUT_PULLUP);



Serial1.begin(9600);
Serial.println("Adafruit Sound Board!");

if (!sfx.reset()) {
    Serial.println("Not found");
    while (1);
  }
  Serial.println("SFX board found");

sfx.playTrack(soundName[1]);
delay(4000);

}



void loop() {
  
static unsigned long timer = millis();

digitalRead(SWITCHPIN1);
digitalRead(SWITCHPIN2);
digitalRead(SWITCHPIN3);
digitalRead(SWITCHPIN4);

buttonState = digitalRead(FIREPIN);


////////////////////////////////////////////MODE 1


if (SWITCHPIN1 && SWITCHPIN2 == HIGH){

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[2]); 
    colorWipe(0xff,0x00,0x00, 5);
    colorWipe(0x00,0x00,0x00, 5);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}

////////////////////////////////////////////MODE 2


////////////////////////////////////////////MODE 3

  
  
  
//////////////////Soundboard Stuff/////////////////////  
  
// flushInput();
  
//  Serial.println(F("What would you like to do?"));
//  Serial.println(F("[r] - reset"));
//  Serial.println(F("[+] - Vol +"));
//  Serial.println(F("[-] - Vol -"));
//  Serial.println(F("[L] - List files"));
//  Serial.println(F("[P] - play by file name"));
//  Serial.println(F("[#] - play by file number"));
//  Serial.println(F("[=] - pause playing"));
//  Serial.println(F("[>] - unpause playing"));
//  Serial.println(F("[q] - stop playing"));
//  Serial.println(F("[t] - playtime status"));
//  Serial.println(F("> "));
  
//  while (!Serial.available());
//  char cmd = Serial.read();
  
//   flushInput();
//  
//  switch (cmd) {
//    case 'r': {
//      if (!sfx.reset()) {
//        Serial.println("Reset failed");
//      }
//      break; 
//    } 
//    
//    case '#': {
//      Serial.print("Enter track #");
//      uint8_t n = readnumber();
//
//      Serial.print("\nPlaying track #"); Serial.println(n);
//      if (! sfx.playTrack((uint8_t)n) ) {
//        Serial.println("Failed to play track?");
//      }
//      break;
//    }
//    
//    case 'P': {
//      Serial.print("Enter track name (full 12 character name!) >");
//      char name[20];
//      readline(name, 20);
//
//      Serial.print("\nPlaying track \""); Serial.print(name); Serial.print("\"");
//      if (! sfx.playTrack(name) ) {
//        Serial.println("Failed to play track?");
//      }
//      break;
//   }
//
//   case '+': {
//      Serial.println("Vol up...");
//      uint16_t v;
//      if (! (v = sfx.volUp()) ) {
//        Serial.println("Failed to adjust");
//      } else {
//        Serial.print("Volume: "); Serial.println(v);
//      }
//      break;
//   }
//
//   
//   case 'q': {
//      Serial.println("Stopping...");
//      if (! sfx.stop() ) Serial.println("Failed to stop");
//      break;
//   }  
//
//   case 't': {
//      Serial.print("Track time: ");
//      uint32_t current, total;
//      if (! sfx.trackTime(&current, &total) ) Serial.println("Failed to query");
//      Serial.print(current); Serial.println(" seconds");
//      break;
//   }  
//
//   case 's': {
//      Serial.print("Track size (bytes remaining/total): ");
//      uint32_t remain, total;
//      if (! sfx.trackSize(&remain, &total) ) 
//        Serial.println("Failed to query");
//      Serial.print(remain); Serial.print("/"); Serial.println(total); 
//      break;
//   }  
//
//  }





  
} /////END LOOP



/************************ MENU HELPERS ***************************/

void flushInput() {
  // Read all available serial input to flush pending data.
  uint16_t timeoutloop = 0;
  while (timeoutloop++ < 40) {
    while(Serial.available()) {
      Serial.read();
      timeoutloop = 0;  // If char was received reset the timer
    }
    delay(1);
  }
}

char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}

uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
    //Serial.print(c);
  }
  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;
}

uint8_t readline(char *buff, uint8_t maxbuff) {
  uint16_t buffidx = 0;
  
  while (true) {
    if (buffidx > maxbuff) {
      break;
    }

    if (Serial.available()) {
      char c =  Serial.read();
      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

      if (c == '\r') continue;
      if (c == 0xA) {
        if (buffidx == 0) {  // the first 0x0A is ignored
          continue;
        }
        buff[buffidx] = 0;  // null term
        return buffidx;
      }
      buff[buffidx] = c;
      buffidx++;
    }
  }
  buff[buffidx] = 0;  // null term
  return buffidx;
}


/////////////////////////NEOPIXEL STUFF///////////////////////////

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      setPixel(i, red, green, blue);
      showStrip();
      delay(SpeedDelay);
  }
}```

Maybe ground pins 1,4 and read pins 2,3 with input pullups. If 2 is low it's in position 1. If 3 is low it is in position 3. If both are high it is in position 2.

Thanks @haywardt

I’m a bit busy this week with classes getting back in gear, but I will try your solution this weekend hopefully. I’ll report back!

So there's no Position 4, which would be Pin4 + Pin1. If there were, it would be a continuous rotary encoder and with the same mechanism, it could be read with two pins, A0 and A1, as follows:


Place a pulldown resistor on top pin and pullup resistor on bottom pin. Place 5 V on left pin and 0V on right pin. Then read A0 and A1. Your case is the same, except you don't have the fourth position.

You might come up with a simpler circuit, but you still need two digital pins. You might be good with one single analog pin with clever use of resistors. Like this:

Here the fourth position is not used (X). The analog pin A0 reads 5V, 2.5V and 0V at the three remaining positions. In the middle position both resistors divide the voltage.

@haywardt

I grounded pins 1 and 4 like you suggested. I also altered the code a bit to clean it up and use functions.

Unfortunately, I get the same result every time. I only get Mode1 to work. This is with switching between all modes on the switch.

Any other thoughts?

@Johan_Ha

I'm not 100% on where you're going with that. I'd like to stick with internal pull-up resistors.

New code:



#include "Adafruit_Soundboard.h"
#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define PIXEL_PIN     5   // Pin connected to neo pixels
#define FIREPIN       7   // Fire button 
//#define SWITCHPIN1    15  // Analog
//#define SWITCHPIN2    16  // Analog
#define SWITCHPIN2    17  // Analog
#define SWITCHPIN3    18  // Analog
#define LED_COUNT     30
#define NUM_LEDS      30

// Choose any two pins that can be used with SoftwareSerial to RX & TX
#define SFX_TX  1
#define SFX_RX  0
#define SFX_RST 2

Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);

#define NUM_SOUNDS 5
// The names of the found files.
// Note that they are 8 characters followed by a 3 character file
// type (.OGG or .WAV). Spaces are inserted to make up the
// 8 characters as needed.
char *soundName[NUM_SOUNDS] = {
  "EWOKHORNWAV",//0
  "LOCKLOADWAV",//1
  "PRIMARY1WAV",//2
  "STUNGUN2WAV",//3
  "BLASTER3WAV" //4
};

int buttonState = 0;
int lastButtonState = 0;
int startPressed = 0;
int endPressed = 0;
int holdTime = 0;
int idleTime = 0;




Adafruit_NeoPixel strip(LED_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);



void setup() {
  
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.
  
strip.begin();
strip.show();  
strip.setBrightness(255); // Set BRIGHTNESS to about 1/5 (max = 255)

pinMode(FIREPIN, INPUT_PULLUP);

pinMode(SWITCHPIN2, INPUT_PULLUP);
pinMode(SWITCHPIN3, INPUT_PULLUP);
//pinMode(SWITCHPIN3, INPUT_PULLUP);
//pinMode(SWITCHPIN4, INPUT_PULLUP);



Serial1.begin(9600);
Serial.println("Adafruit Sound Board!");

if (!sfx.reset()) {
    Serial.println("Not found");
    while (1);
  }
  Serial.println("SFX board found");

sfx.playTrack(soundName[1]);
delay(4000);



}



void loop() {
  
static unsigned long timer = millis();
buttonState = digitalRead(FIREPIN);

digitalRead(SWITCHPIN2);
digitalRead(SWITCHPIN3);

mode1();
mode2();
mode3();


} 

////////////////////////////////////////////MODE 1

void mode1() {

if (digitalRead(SWITCHPIN2) == LOW){
//sfx.playTrack(soundName[1]);
//delay(1000);
//sfx.stop();

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[2]); 
    colorWipe(0xff,0x00,0x00, 5);
    colorWipe(0x00,0x00,0x00, 5);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}
}
 

////////////////////////////////////////////MODE 2

void mode2 (){
 
if (digitalRead(SWITCHPIN2) && digitalRead(SWITCHPIN3) == HIGH){
//sfx.playTrack(soundName[1]);
//delay(1000);
//sfx.stop();

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[3]); 
    colorWipe(0xff,0x00,0x00, 5);
    colorWipe(0x00,0x00,0x00, 5);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}
}

////////////////////////////////////////////MODE 3

void mode3 (){

if (digitalRead(SWITCHPIN3) == LOW){
//sfx.playTrack(soundName[1]);
//delay(1000);
//sfx.stop();

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[4]); 
    colorWipe(0xff,0x00,0x00, 5);
    colorWipe(0x00,0x00,0x00, 5);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}
} 
  
  


/************************ SOUNDBOARD ***************************/

void flushInput() {
  // Read all available serial input to flush pending data.
  uint16_t timeoutloop = 0;
  while (timeoutloop++ < 40) {
    while(Serial.available()) {
      Serial.read();
      timeoutloop = 0;  // If char was received reset the timer
    }
    delay(1);
  }
}

char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}

uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
    //Serial.print(c);
  }
  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;
}

uint8_t readline(char *buff, uint8_t maxbuff) {
  uint16_t buffidx = 0;
  
  while (true) {
    if (buffidx > maxbuff) {
      break;
    }

    if (Serial.available()) {
      char c =  Serial.read();
      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

      if (c == '\r') continue;
      if (c == 0xA) {
        if (buffidx == 0) {  // the first 0x0A is ignored
          continue;
        }
        buff[buffidx] = 0;  // null term
        return buffidx;
      }
      buff[buffidx] = c;
      buffidx++;
    }
  }
  buff[buffidx] = 0;  // null term
  return buffidx;
}


/////////////////////////NEOPIXEL STUFF///////////////////////////

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      setPixel(i, red, green, blue);
      showStrip();
      delay(SpeedDelay);
  }
}```
if (digitalRead(SWITCHPIN2) && digitalRead(SWITCHPIN3) == HIGH) {

Each digitalRead() must be accompanied by a corresponding '== HIGH/LOW'.

Thanks @dougp

I corrected the code. It only seems to want to go into Mode 2, with both pins being LOW. This is trying the switch at all positions.

I'm wondering if my wiring is still incorrect.

I have pin 1 and 4 of the switch grounded. Pins 2 and 3 of the switch are going to my board.

Edit: Another thought about the wiring.

When I unplug the jumpers for the grounds, Mode3 starts on all positions of the switch. When I unplug one of the jumpers for pins 2 or 3, Mode1 starts on all positions of the switch.

So the modes work in the code. There is something about this switch I am not understanding.



#include "Adafruit_Soundboard.h"
#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define PIXEL_PIN     5   // Pin connected to neo pixels
#define FIREPIN       7   // Fire button 
//#define SWITCHPIN1    15  // Analog
//#define SWITCHPIN2    16  // Analog
#define SWITCHPIN2    17  // Analog
#define SWITCHPIN3    18  // Analog
#define LED_COUNT     30
#define NUM_LEDS      30

// Choose any two pins that can be used with SoftwareSerial to RX & TX
#define SFX_TX  1
#define SFX_RX  0
#define SFX_RST 2

Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);

#define NUM_SOUNDS 5
// The names of the found files.
// Note that they are 8 characters followed by a 3 character file
// type (.OGG or .WAV). Spaces are inserted to make up the
// 8 characters as needed.
char *soundName[NUM_SOUNDS] = {
  "EWOKHORNWAV",//0
  "LOCKLOADWAV",//1
  "PRIMARY1WAV",//2
  "STUNGUN2WAV",//3
  "BLASTER3WAV" //4
};

int buttonState = 0;
int lastButtonState = 0;
int startPressed = 0;
int endPressed = 0;
int holdTime = 0;
int idleTime = 0;




Adafruit_NeoPixel strip(LED_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);



void setup() {
  
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.
  
strip.begin();
strip.show();  
strip.setBrightness(255); // Set BRIGHTNESS to about 1/5 (max = 255)

pinMode(FIREPIN, INPUT_PULLUP);

pinMode(SWITCHPIN2, INPUT_PULLUP);
pinMode(SWITCHPIN3, INPUT_PULLUP);
//pinMode(SWITCHPIN3, INPUT_PULLUP);
//pinMode(SWITCHPIN4, INPUT_PULLUP);



Serial1.begin(9600);
Serial.println("Adafruit Sound Board!");

if (!sfx.reset()) {
    Serial.println("Not found");
    while (1);
  }
  Serial.println("SFX board found");

sfx.playTrack(soundName[1]);
delay(4000);



}



void loop() {
  
static unsigned long timer = millis();
buttonState = digitalRead(FIREPIN);

digitalRead(SWITCHPIN2);
digitalRead(SWITCHPIN3);

mode1();
mode2();
mode3();


} 

////////////////////////////////////////////MODE 1

void mode1() {

if (digitalRead(SWITCHPIN2) == LOW && digitalRead(SWITCHPIN3) == HIGH){
//sfx.playTrack(soundName[1]);
//delay(1000);
//sfx.stop();

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[2]); 
    colorWipe(0xff,0x00,0x00, 5);
    colorWipe(0x00,0x00,0x00, 5);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}
}
 

////////////////////////////////////////////MODE 2

void mode2 (){
 
if (digitalRead(SWITCHPIN2) == LOW && digitalRead(SWITCHPIN3) == LOW){
//sfx.playTrack(soundName[1]);
//delay(1000);
//sfx.stop();

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[3]); 
    colorWipe(0xff,0x00,0x00, 5);
    colorWipe(0x00,0x00,0x00, 5);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}
}

////////////////////////////////////////////MODE 3

void mode3 (){

if (digitalRead(SWITCHPIN2) == HIGH && digitalRead(SWITCHPIN3) == HIGH){
//sfx.playTrack(soundName[1]);
//delay(1000);
//sfx.stop();

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[4]); 
    colorWipe(0xff,0x00,0x00, 5);
    colorWipe(0x00,0x00,0x00, 5);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}
} 
  
  


/************************ SOUNDBOARD ***************************/

void flushInput() {
  // Read all available serial input to flush pending data.
  uint16_t timeoutloop = 0;
  while (timeoutloop++ < 40) {
    while(Serial.available()) {
      Serial.read();
      timeoutloop = 0;  // If char was received reset the timer
    }
    delay(1);
  }
}

char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}

uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
    //Serial.print(c);
  }
  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;
}

uint8_t readline(char *buff, uint8_t maxbuff) {
  uint16_t buffidx = 0;
  
  while (true) {
    if (buffidx > maxbuff) {
      break;
    }

    if (Serial.available()) {
      char c =  Serial.read();
      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

      if (c == '\r') continue;
      if (c == 0xA) {
        if (buffidx == 0) {  // the first 0x0A is ignored
          continue;
        }
        buff[buffidx] = 0;  // null term
        return buffidx;
      }
      buff[buffidx] = c;
      buffidx++;
    }
  }
  buff[buffidx] = 0;  // null term
  return buffidx;
}


/////////////////////////NEOPIXEL STUFF///////////////////////////

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      setPixel(i, red, green, blue);
      showStrip();
      delay(SpeedDelay);
  }
}

Is the switch still wired as @haywardt suggested in post #2?

Have you verified this with an ohmmeter?

1 Like

With two external resistors you need only one analog pin. The expected read values would be 0, 511 and 1023.

Hey all,

Just wanted to thank you all for your help. I managed to figure out what the issue was. The trigger housing for the switch is metal (normally my props are plastic and I don't think anything of it).

So checking for continuity led me to find out that all of the pins on the switch were connected through the metal trigger housing.

I took some tape and wrapped the edges, and everything works great as expected.

Here is the finished code for anyone needing something similar:

#include "Adafruit_Soundboard.h"
#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define PIXEL_PIN     5   // Pin connected to neo pixels
#define FIREPIN       7   // Fire button 
#define SWITCHPIN2    17  // Analog
#define SWITCHPIN3    18  // Analog
#define LED_COUNT     30
#define NUM_LEDS      30

// Choose any two pins that can be used with SoftwareSerial to RX & TX
#define SFX_TX  1
#define SFX_RX  0
#define SFX_RST 2

Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);

#define NUM_SOUNDS 5
// The names of the found files.
// Note that they are 8 characters followed by a 3 character file
// type (.OGG or .WAV). Spaces are inserted to make up the
// 8 characters as needed.
char *soundName[NUM_SOUNDS] = {
  "EWOKHORNWAV",//0
  "LOCKLOADWAV",//1
  "PRIMARY1WAV",//2
  "STUNGUN2WAV",//3
  "BLASTER3WAV" //4
};

int buttonState = 0;
int lastButtonState = 0;
int startPressed = 0;
int endPressed = 0;
int holdTime = 0;
int idleTime = 0;




Adafruit_NeoPixel strip(LED_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);



void setup() {
  
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif
  // END of Trinket-specific code.
  
strip.begin();
strip.show();  
strip.setBrightness(255); // Set BRIGHTNESS to about 1/5 (max = 255)

pinMode(FIREPIN, INPUT_PULLUP);

pinMode(SWITCHPIN2, INPUT_PULLUP);
pinMode(SWITCHPIN3, INPUT_PULLUP);




Serial1.begin(9600);
Serial.println("Adafruit Sound Board!");

if (!sfx.reset()) {
    Serial.println("Not found");
    while (1);
  }
  Serial.println("SFX board found");

sfx.playTrack(soundName[0]);
delay(4000);



}



void loop() {
  
static unsigned long timer = millis();
buttonState = digitalRead(FIREPIN);

digitalRead(SWITCHPIN2);
digitalRead(SWITCHPIN3);

if (digitalRead(SWITCHPIN2) == LOW && digitalRead(SWITCHPIN3) == HIGH){
mode1();
}

if (digitalRead(SWITCHPIN2) == HIGH && digitalRead(SWITCHPIN3) == HIGH){
mode2();
}


if (digitalRead(SWITCHPIN2) == HIGH && digitalRead(SWITCHPIN3) == LOW){
mode3();
}


} 

////////////////////////////////////////////MODE 1

void mode1() {

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[2]); 
    colorWipe(0xff,0x00,0x00, 3);
    colorWipe(0x00,0x00,0x00, 3);
    delay(23);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}

 

////////////////////////////////////////////MODE 2

void mode2 (){
 

if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[3]); 
    Strobe(0x03, 0xab, 0xff, 10, 50, 800);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{
    
//    digitalRead(FIREPIN);
//      if (FIREPIN == HIGH){
//    endPressed = millis();
//    holdTime = endPressed - startPressed;
    
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;

}


////////////////////////////////////////////MODE 3

void mode3 (){


if (buttonState != lastButtonState){
  if (buttonState == HIGH) {

    startPressed = millis();
    idleTime = startPressed - endPressed;

    sfx.playTrack(soundName[1]); 
    delay (1000);
    sfx.stop();

    if (idleTime >=500) {
      buttonState = LOW;
      }
    }
  else{  
    setAll(0,0,0); 
    sfx.stop();
//      }    
    }
    
}
 
lastButtonState = buttonState;


} 
  
  


/************************ SOUNDBOARD ***************************/

void flushInput() {
  // Read all available serial input to flush pending data.
  uint16_t timeoutloop = 0;
  while (timeoutloop++ < 40) {
    while(Serial.available()) {
      Serial.read();
      timeoutloop = 0;  // If char was received reset the timer
    }
    delay(1);
  }
}

char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}

uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
    //Serial.print(c);
  }
  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;
}

uint8_t readline(char *buff, uint8_t maxbuff) {
  uint16_t buffidx = 0;
  
  while (true) {
    if (buffidx > maxbuff) {
      break;
    }

    if (Serial.available()) {
      char c =  Serial.read();
      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

      if (c == '\r') continue;
      if (c == 0xA) {
        if (buffidx == 0) {  // the first 0x0A is ignored
          continue;
        }
        buff[buffidx] = 0;  // null term
        return buffidx;
      }
      buff[buffidx] = c;
      buffidx++;
    }
  }
  buff[buffidx] = 0;  // null term
  return buffidx;
}


/////////////////////////NEOPIXEL STUFF///////////////////////////

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue); 
  }
  showStrip();
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      setPixel(i, red, green, blue);
      showStrip();
      delay(SpeedDelay);
  }
}

void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause){
  for(int j = 0; j < StrobeCount; j++) {
    setAll(red,green,blue);
    showStrip();
    delay(FlashDelay);
    setAll(0,0,0);
    showStrip();
    delay(FlashDelay);
  }
 
 delay(EndPause);
}```

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.