Hi all,
got a problem with a neopixel ring not registering new data. So the switching of animations is handled via a relay but the problem i'm having is with the third ring after the puzzle has been solved. I've moved the lines of code between the onsolve function and the while (true) loop. Am i missing something obvious?
/*
Hospital Maze
Player has to find the patients in the correct order.
If correct patent has been found the LED ring will light solid if not, the ring will chase in the colour corosponding to the patient
*/
// FastLED
#include "FastLED.h" // Include Neopixel library
#define SPOT_1 9 // Defines pinouts for Neopixel rings
#define SPOT_2 8 // Defines pinouts for Neopixel rings
#define SPOT_3 7 // Defines pinouts for Neopixel rings
#define NUM_LEDS 16 // Number of neopixels per ring.
#define LED_TYPE NEOPIXEL
struct CRGB spot1[NUM_LEDS];
struct CRGB spot2[NUM_LEDS];
struct CRGB spot3[NUM_LEDS];
// DEFINES
// Provides debugging information over serial connection
#define DEBUG
// Number of possible inputs/sensors
const byte numInputs = 3;
const byte inputPins[numInputs] = {12, 11, 10};
// Number of steps in the sequence
const byte numSteps = 3;
// Sequence of inputs required to solve the puzzle.
const byte steps[numSteps] = {0, 1, 2}; // i.e. press button #1 once, then button #2 once, then button #3 once
// These pins are used to trigger the relays if the correct patient has been found
const byte relayPins[numSteps] = {5, 6, 4};
// When solved sound will play
const byte soundPin = A0;
// GLOBALS
// Assume the default state of each switch is HIGH. Hall effect power lines are wired backward to relect this
bool lastInputState[] = {HIGH, HIGH, HIGH, HIGH};
// What step of the sequence is the player currently on?
int currentStep = 0;
// Sensor debounce
// The last time the input switch was toggled
unsigned long lastDebounceTime = 0;
// The amount of time (in ms) to wait before reading again
unsigned long debounceDelay = 1000;
void setup() {
// Initialise the input pins
for (int i = 0; i < numInputs; i++) {
pinMode(inputPins[i], INPUT_PULLUP);
}
// Initialise the LED pins that show progress through the sequence
for (int i = 0; i < numSteps; i++) {
pinMode(relayPins[i], OUTPUT);
}
// Set the lock pin as output and secure the lock
pinMode(soundPin, OUTPUT);
//digitalWrite(lockPin, HIGH);
FastLED.addLeds<LED_TYPE, SPOT_1>(spot1, NUM_LEDS);
FastLED.addLeds<LED_TYPE, SPOT_2>(spot2, NUM_LEDS);
FastLED.addLeds<LED_TYPE, SPOT_3>(spot3, NUM_LEDS);
#ifdef DEBUG
Serial.begin(9600);
Serial.println(F("Serial communication started"));
#endif
}
// The main program loop runs continuously
void loop() {
fill_solid( spot1, NUM_LEDS, CRGB::Blue);
fill_solid( spot2, NUM_LEDS, CRGB::Green);
fill_solid( spot3, NUM_LEDS, CRGB::GreenYellow);
FastLED.show();
// Check that we've waited at least "debounceDelay" since last input
if ( (millis() - lastDebounceTime) > debounceDelay) {
// Loop through all the inputs
for (int i = 0; i < numInputs; i++) {
int currentInputState = digitalRead(inputPins[i]);
// If the input has changed, reset the debounce timer
if (currentInputState != lastInputState[i]) {
lastDebounceTime = millis();
}
// If the input is currently being pressed (and wasn't before)
// Note that since the input pins are configured as INPUT_PULLUP,
// they read as LOW when pressed and HIGH when not.
if (currentInputState == LOW && lastInputState[i] == HIGH) {
// Was this the correct input for this step of the sequence?
if (steps[currentStep] == i) {
currentStep++;
#ifdef DEBUG
Serial.print(F("Correct input! Onto step #"));
Serial.println(currentStep);
#endif
}
// Incorrect input
else {
currentStep = 0;
Serial.println(F("Incorrect input! Back to the beginning!"));
}
}
// Update the stored value for this input
lastInputState[i] = currentInputState;
}
}
// Check whether the puzzle has been solved
if (currentStep == numSteps) {
fill_solid( spot3, NUM_LEDS, CRGB::GreenYellow);
FastLED.show();
onSolve();
fill_solid( spot3, NUM_LEDS, CRGB::GreenYellow);
FastLED.show();
delay(100);
}
// Turn on the number of relays corresponding to the current step - These turn the rings solid
for (int i = 0; i < numSteps; i++) {
digitalWrite(relayPins[i], (i < currentStep ? LOW : HIGH));
}
}
// Takes action when the puzzle becomes solved
void onSolve() {
#ifdef DEBUG
// Print a message
Serial.println(F("Correct sequence"));
#endif
fill_solid( spot3, NUM_LEDS, CRGB::GreenYellow);
FastLED.show();
digitalWrite(4, LOW); //Set the relay 3 for solid colour
digitalWrite(soundPin, HIGH);
delay (50);
Serial.println(F("Sound playing"));
delay(50);
// Loop forever
while (true) {
digitalWrite(4, LOW); //Make sure this is set the relay 3 for solid colour
fill_solid( spot3, NUM_LEDS, CRGB::GreenYellow);
FastLED.show();
}
}
Cheers