Skip to content

Instantly share code, notes, and snippets.

@cowdinosaur
Created October 8, 2020 04:53
Show Gist options
  • Save cowdinosaur/28031ad5cbeefaed93d24f2f8fb1590f to your computer and use it in GitHub Desktop.
Save cowdinosaur/28031ad5cbeefaed93d24f2f8fb1590f to your computer and use it in GitHub Desktop.
NYP Spectrum DDR Step Input
#include <Adafruit_NeoPixel.h>
// Which pin is connected to the pressure sensors
#define STEP_UP_PIN A0
#define STEP_LEFT_PIN A1
#define STEP_DOWN_PIN A2
#define STEP_RIGHT_PIN A3
// Which pin is connected to the NeoPixels
#define LED_UP_PIN 2
#define LED_LEFT_PIN 3
#define LED_DOWN_PIN 4
#define LED_RIGHT_PIN 5
#define LED_COUNT 10
// First 10 for UP, 11-20 for LEFT, 21-30 for DOWN, 31-40 for RIGHT
uint32_t values[LED_COUNT*4];
int read_value_idx = 0;
int step_state[4];
int last_step_state[4];
unsigned long step_last_debounce_time[4];
#define DEBOUNCE_DELAY 200
#define THRESHOLD_VALUE 5000
// Key Presses
// Up - 0001
// Left - 0010
// Down - 0100
// Right - 1000
#define UP_VAL B0001
#define LEFT_VAL B0010
#define DOWN_VAL B0100
#define RIGHT_VAL B1000
// Fire States
int FIRE_STATES[] = { B0000, LEFT_VAL+DOWN_VAL, UP_VAL, DOWN_VAL, UP_VAL+RIGHT_VAL, DOWN_VAL, LEFT_VAL+RIGHT_VAL, UP_VAL };
int WATER_STATES[] = { B0000, LEFT_VAL, UP_VAL+RIGHT_VAL, RIGHT_VAL, RIGHT_VAL, DOWN_VAL, LEFT_VAL, LEFT_VAL+RIGHT_VAL };
int EARTH_STATES[] = { B0000, UP_VAL+LEFT_VAL,UP_VAL+RIGHT_VAL, DOWN_VAL+RIGHT_VAL, LEFT_VAL+DOWN_VAL, LEFT_VAL, RIGHT_VAL, UP_VAL+DOWN_VAL };
int fire_states_idx = 0;
int water_states_idx = 0;
int earth_states_idx = 0;
bool ready_for_next_press = true;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip[] {
Adafruit_NeoPixel(LED_COUNT, LED_UP_PIN, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(LED_COUNT, LED_LEFT_PIN, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(LED_COUNT, LED_DOWN_PIN, NEO_GRB + NEO_KHZ800),
Adafruit_NeoPixel(LED_COUNT, LED_RIGHT_PIN, NEO_GRB + NEO_KHZ800)
};
uint32_t magenta = strip[0].Color(255, 0, 255);
uint32_t white = strip[0].Color(255, 255, 255);
void setup() {
// Serial
Serial.begin(9600);
// Pressure sensor pins
pinMode(STEP_UP_PIN, INPUT); // UP
pinMode(STEP_LEFT_PIN, INPUT); // LEFT
pinMode(STEP_DOWN_PIN, INPUT); // DOWN
pinMode(STEP_RIGHT_PIN, INPUT); // RIGHT
for (int i=0; i<4; i++) {
strip[i].begin();
strip[i].show(); // Initialize all pixels to 'off'
step_state[i] = LOW;
last_step_state[i] = LOW;
}
}
void read_step_value() {
if (read_value_idx < 10) { // UP value
values[read_value_idx] = analogRead(STEP_UP_PIN);
} else if (read_value_idx < 20) { // LEFT value
values[read_value_idx] = analogRead(STEP_LEFT_PIN);
} else if (read_value_idx < 30) { // DOWN value
values[read_value_idx] = analogRead(STEP_DOWN_PIN);
} else { // RIGHT value
values[read_value_idx] = analogRead(STEP_RIGHT_PIN);
}
read_value_idx = read_value_idx + 10;
if (read_value_idx == 49) {
read_value_idx = 0;
} else if (read_value_idx > 39) {
read_value_idx = read_value_idx - 39;
}
for (int j=0; j < 4; j++) {
int step_value = 0;
for (int i=0; i < 10; i++) {
step_value += values[j*10 + i];
}
int this_step_state = LOW;
if (step_value < THRESHOLD_VALUE) {
this_step_state = HIGH;
}
if (this_step_state != last_step_state[j]) {
step_last_debounce_time[j] = millis();
}
if (millis() - step_last_debounce_time[j] > DEBOUNCE_DELAY) {
if (this_step_state != step_state[j]) {
step_state[j] = this_step_state;
}
}
last_step_state[j] = this_step_state;
}
}
void show_led_states() {
for (int i=0; i<4; i++) {
strip[i].fill(white);
if (step_state[i] == HIGH) {
strip[i].fill(magenta);
}
strip[i].show();
}
}
void loop() {
read_step_value();
show_led_states();
// Process STEP states
int combined_state = B000;
for (int i=0; i<4; i++) {
if (step_state[i] == HIGH) {
combined_state += B0001 << i;
}
}
if (combined_state == B0000) {
ready_for_next_press = true;
} else {
if (ready_for_next_press) {
if (fire_states_idx != 0) {
if (combined_state == FIRE_STATES[fire_states_idx+1]) {
fire_states_idx += 1;
Serial.println("F:"+fire_states_idx);
} else if ((combined_state ^ FIRE_STATES[fire_states_idx+1]) == B0000) {
// we have at least one of the states chosen
// we don't do anything while we wait
return;
} else {
fire_states_idx -= 1;
Serial.println("F:"+fire_states_idx);
}
} else if (water_states_idx != 0) {
if (combined_state == WATER_STATES[water_states_idx+1]) {
water_states_idx += 1;
Serial.println("W:"+water_states_idx);
} else if ((combined_state ^ WATER_STATES[water_states_idx+1]) == B0000) {
// we have at least one of the states chosen
// we don't do anything while we wait
return;
}else {
water_states_idx -= 1;
Serial.println("W:"+water_states_idx);
}
} else if (earth_states_idx != 0) {
if (combined_state == EARTH_STATES[earth_states_idx+1]) {
earth_states_idx += 1;
Serial.println("E:"+earth_states_idx);
} else if ((combined_state ^ EARTH_STATES[earth_states_idx+1]) == B0000) {
// we have at least one of the states chosen
// we don't do anything while we wait
return;
}else {
earth_states_idx -= 1;
Serial.println("E:"+earth_states_idx);
}
} else {
if (combined_state == FIRE_STATES[1]) {
fire_states_idx = 1;
} else if (combined_state == WATER_STATES[1]) {
water_states_idx = 1;
} else if (combined_state == EARTH_STATES[1]) {
earth_states_idx = 1;
}
}
ready_for_next_press = false;
// We reached state 7. Play video and reset
if ((fire_states_idx == 7) || (water_states_idx == 7) || (earth_states_idx == 7)) {
fire_states_idx = 0;
water_states_idx = 0;
earth_states_idx = 0;
}
}
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment