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(¤t, &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);
}
}```