I am trying to make a master mind/code breaker game where the player has to guess a random code that is made up of four colors. My goal is that each node of the neo pixel will fade in and out until the user cycles through the available colors and then selects a color. At which point, the node will stop flashing and then the next node will start to fade in and out until the player selects the next color for the code. Right now I have been able to get the nodes to flash until a color is selected. But once the color is selected, the next node in the sequence begins to flash ,but previous node just fade completely off.
this is my current code
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
boolean flag1 = false;
boolean flag2 = false;
boolean flag3 = false;
const int LED_PIN = 12;
const int ansLedPins[4] = {7, 8, 9, 10};
const int neoBtnPins[2] = {2, 3};
const int btnSubmit = 4;
const int dspNodeArray[4] = {0,1,2,3};
const long debounceTime = 50;
const long debounceTime2 = 500;
const long interval = 1000;
const long brtInterval = 25;
unsigned long prevMillis = 0;
static uint32_t lastPressTime = 0;
uint32_t neoPixArray[8];
uint32_t rmdPixArray[4];
uint32_t usrPixArray[4];
int colorIDX = 0;
int selectIDX = 0;
int dspNodeIDX = 0;
int dspNodeInc = 0;
int btnState = 1;
int ansIndex = 0;
int brtLevel = 100;
volatile byte state = LOW;
void cycleColor();
void selectColor();
void chkColorCode();
void randomize();
uint32_t Red;
uint32_t Blue;
uint32_t Yellow;
uint32_t Green;
uint32_t Gray;
uint32_t Magenta;
uint32_t Navy;
uint32_t Brown;
#define LED_COUNT 60
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
Serial.begin(9600);
strip.begin();
strip.show();
strip.setBrightness(brtLevel);
pinMode(btnSubmit, INPUT);
for(int i = 0; i < 2 ; i++ )
{
pinMode(neoBtnPins[i], INPUT);
}
attachInterrupt(digitalPinToInterrupt(neoBtnPins[0]), cycleColor, CHANGE);
attachInterrupt(digitalPinToInterrupt(neoBtnPins[1]), selectColor, CHANGE);
for(int l = 0; l < 4; l++)
{
pinMode(ansLedPins[l], OUTPUT);
}
for(int c = 0; c < 4; c++)
{
digitalWrite(ansLedPins[c], HIGH);
delay(100);
digitalWrite(ansLedPins[c], LOW);
delay(100);
}
Red = strip.Color(139, 0, 0);
Green = strip.Color(0, 100, 0);
Yellow = strip.Color(139, 139, 0);
Blue = strip.Color(0, 0, 139);
Gray = strip.Color(192, 192, 192);
Magenta = strip.Color(255, 69, 0);
Navy = strip.Color(25, 25, 112);
Brown = strip.Color(139, 69, 19);
neoPixArray[0] = Red;
neoPixArray[1] = Green;
neoPixArray[2] = Yellow;
neoPixArray[3] = Blue;
neoPixArray[4] = Gray;
neoPixArray[5] = Magenta;
neoPixArray[6] = Navy;
neoPixArray[7] = Brown;
for( int n = 0; n < 4; n++ )
{
randomize();
uint32_t randomColor = random(0,7);
rmdPixArray[n] = neoPixArray[randomColor];
}
}
void asgNodeColor( int numNode, int clrIndex)
{
int i = numNode;
int j = clrIndex;
if(flag3 == false)
{
Serial.print("numNode ");
Serial.print("\t");
Serial.print(i);
Serial.print("\t");
Serial.print("clrIndex ");
Serial.print("\t");
Serial.print(j);
Serial.println();
flag3 = true;
}
strip.clear();
usrPixArray[i] = neoPixArray[j];
strip.setPixelColor(i,usrPixArray[j]);
strip.show();
dspNodeInc++;
colorIDX = 0;
}
void loop()
{
/*
strip.setPixelColor(4,rmdPixArray[0]);
strip.setPixelColor(5,rmdPixArray[1]);
strip.setPixelColor(6,rmdPixArray[2]);
strip.setPixelColor(7,rmdPixArray[3]);
strip.show();
*/
unsigned long currentMillis = millis();
if(currentMillis - prevMillis >= brtInterval)
{
if(colorIDX <= 7 && dspNodeInc < 4)
{
strip.setPixelColor(dspNodeArray[dspNodeInc], neoPixArray[colorIDX]);
strip.show();
//unsigned long currentMillis = millis();
//if(currentMillis - prevMillis >= brtInterval)
//{
//prevMillis = currentMillis;
Serial.print("dspNode ");
Serial.print("\t");
Serial.print(dspNodeInc);
Serial.println();
if(brtLevel <= 100) //&& brtLevel != 0)
{
strip.setBrightness(brtLevel);
brtLevel--;
}
else if( brtLevel == 50)
{
brtLevel = 75;
strip.setBrightness(brtLevel);
//brtLevel = 75;
}
}
else
{
colorIDX = 0;
}
}
if(digitalRead(btnSubmit) == 0 && millis() - lastPressTime > debounceTime2)
{
lastPressTime = millis();
state = !state;
chkColorCode();
}
}
void cycleColor()
{
if(digitalRead(neoBtnPins[0]) && millis() - lastPressTime > debounceTime)
{
lastPressTime = millis();
state = !state;
colorIDX++;
}
}
void selectColor()
{
if(digitalRead(neoBtnPins[1]) && millis() - lastPressTime > debounceTime)
{
lastPressTime = millis();
state = !state;
usrPixArray[dspNodeInc] = neoPixArray[colorIDX];
dspNodeInc++;
colorIDX = 0;
//asgNodeColor(dspNodeInc,colorIDX);
flag2 = false;
flag1 = false;
flag3 = false;
}
}
void randomize()
{
uint32_t newSeed = 0;
for (int i=0; i < 32; i++)
{
uint32_t r = analogRead(A0);
r <<= (analogRead(A1) >> 3) & 0x03;
r += analogRead(A2);
newSeed <<= 1;
if (r & 0x04)
{
newSeed |= 0x1;
}
}
randomSeed(newSeed);
}
void chkColorCode()
{
for(int x = 0; x < 4; x++)
{
if(usrPixArray[x] == rmdPixArray[x])
{
digitalWrite(ansLedPins[x],HIGH);
Serial.println(usrPixArray[x]);
}
else if(usrPixArray[x] == rmdPixArray[0])
{
Serial.print("Right Color, Wrong Spot");
Serial.println();
}
else if(usrPixArray[x] == rmdPixArray[1])
{
Serial.print("Right Color, Wrong Spot");
Serial.println();
}
else if(usrPixArray[x] == rmdPixArray[2])
{
Serial.print("Right Color, Wrong Spot");
Serial.println();
}
else if(usrPixArray[x] == rmdPixArray[3])
{
Serial.print("Right Color, Wrong Spot");
Serial.println();
}
else if(usrPixArray[x] != rmdPixArray[x])
{
Serial.print("Color Not In Code");
Serial.println();
}
}
}
I think part of the problem is that I am using setBrightness() and it really isn't designed for what I am trying to do.
Any help would be greatly appreciated.
