Hi Everyone,
I am really struggling with using a struct and an a subloop. Or I should say thats the latest issue after 8 plus hours lol.
The code is for 8 inputs, the first 6 control gages and neopixels. So what I would like to do is to check the first 6 inputs against the solution which is a pot input and have the corresponding neopixel change from red to green and when incorrect go from green to red. The win situation is having all 8 match. Was trying to make it so it can reverse the led blend direction mid stream but I am starting to give up.
The update function is where I would like it to cycle from (255, 0, 0) to (0, 255, 0) in steps defined by a constant. I would like the struct to hold the information for each of the 6 LEDs.
While this sounds easy I have tried everything I can think of and am getting crazy results still.
Below is my latest failure, I would appreciate a little help on how this should all link together.
Thank you!
//Guage Puzzle June 4 Swithcing to adafruit lib
// INCLUDES
#include <Adafruit_NeoPixel.h>
// CONSTANTS
#define NUM_LEDS 6
#define DATA_PIN 2
#define BRIGHTNESS 50 //change to 255 for game
int numOutputs = 8;
int numInputs = 8;
const byte meterPins[] = {3, 5, 6, 9, 10, 11};
const byte sliderPins[] = {A0, A1, A2, A3, A4, A5, A6, A7};
// Win condition const byte relayPin = 2;
const int targetValues[] = {128, 128, 128, 128, 128, 128, 128, 128};
const int tolerance = 4;
int y = 0;
const byte smoothInterval = 40;
struct Timer {
uint32_t previousMillis;
bool Case = true;
byte counterr = 255;
byte counterg = 0;
} timer[NUM_LEDS];
// GLOBALS
// 10-bit input values from the ADC have values in the range (0-1023)
int inputValues[8] = {};
// 8-bit output values to pass to AnalogWrite PWM output have values in the range (0-255)
int outputValues[8] = {};
bool isSolved = false;
Adafruit_NeoPixel pixels(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
pixels.begin();
pixels.setBrightness(BRIGHTNESS);
for(int i=0; i<NUM_LEDS; i++){
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
}
pixels.show();
//Initialise the output pins
for(int i=0; i<6; i++) {
pinMode(meterPins[i], OUTPUT);
}
}
//Sequence for LED blend
void update(byte currentLed){
uint32_t currentMillis = millis();
if (millis() - timer[currentLed].previousMillis > smoothInterval)
{
timer[currentLed].previousMillis = currentMillis;
if (timer[currentLed].counterr < 255 && (timer[currentLed].Case = true))
{
timer[currentLed].counterr++;
timer[currentLed].counterg -1;
Serial.print(currentLed);
Serial.print(timer[currentLed].Case);
Serial.print("Red: ");
Serial.print(timer[currentLed].counterr);
Serial.print(" Green:");
Serial.print(timer[currentLed].counterg);
Serial.println("");
}
if (timer[currentLed].counterg < 255 && (timer[currentLed].Case = false))
{
timer[currentLed].counterg++;
timer[currentLed].counterr -1;
//Serial.print(timer[currentLed].counterr);
//Serial.print(timer[currentLed].counterg);
}
pixels.setPixelColor(currentLed, pixels.Color (timer[currentLed].counterr, timer[currentLed].counterg, 0));
pixels.show();
}
}
void loop() {
for(int i=0; i<numInputs; i++){
// Clear ADC
for(int x=0; x<2; x++) {
analogRead(sliderPins[i]);
delay(5);
}
inputValues[i] = analogRead(sliderPins[i]);
}
// Quick set for outputs
for(int i=0; i<numOutputs; i++){
outputValues[i] = inputValues[i];
//Serial.print(i);
//Serial.print("-");
//Serial.print(outputValues[i]);
//Serial.print(",");
//if(i>(numOutputs-2)){
//Serial.println("");
//}
}
//Setting up custom paramters/relationships
//outputValues[0] = (inputValues[0] + inputValues[1]/ 5.5);
//outputValues[1] = (inputValues[1] - inputValues[2]);
//outputValues[2] = (inputValues[2]);
//outputValues[3] = (inputValues[3]);
//outputValues[4] = (inputValues[4]);
//outputValues[5] = (inputValues[5]);
// Check outputs vs targets
bool allMetersCorrect = true;
// Loop over each output
for(int i=0; i<numOutputs; i++){
outputValues[i] = inputValues[i] >> 2; //map 10 to 8 bit
if(abs(outputValues[i] - targetValues[i]) > tolerance) {
allMetersCorrect = false;
}
if((abs(outputValues[i] - targetValues[i]) > tolerance) && (i<6)){
// Turn incorrect matches to red
timer[i].Case = true;
}
else {
// Turn LED Green when input is correct
timer[i].Case = false;
}
update(i);
}
//Write to the meters
for (int i=0; i<6; i++){
analogWrite(meterPins[i], outputValues[i]);
//Serial.print(i);
//Serial.print("-");
//Serial.print(outputValues[i]);
//Serial.print(",");
//if(i>(4)){
//Serial.println("");
}
if(allMetersCorrect && !isSolved) {
//Serial.println("Solved!");
isSolved = true;
//Wipe gauges on solve need to add delay on return to stop gauge bounce
//y = y+20;
//if(y >= 850){
//y=0;
//}
//for(int i=0; i<6; i++){
//outputValues[i] = y;
}
// If the puzzle had been solved, but now the meters are no longer correct
else if(isSolved && !allMetersCorrect) {
//Serial.println("Unsolved!");
isSolved = false;
}
}