/*
* CAMP-Arduino
* rndCol
* REMASTERD
*
* - COLOR SCORE:
* - RED - 1
* - GREEN - 2
* - YELLOW - 3
* - BLUE - 4
* - MAGENTA - 5
* - CYAN - 6
* - WHITE - 7
*
*0.30 - Basic setup
*0.32 - Testing buttpns
*0.34 - LCD_I2C
*0.36 - PRESS TO START
*0.38 - Adding score + You WIN sequence
*0.40 - Assing CMYW +Random+Seed
*0.42 - colChoose and LCDdisplay
*0.44 - Switch-case also LED displays rndCol
*0.46 - Blocking color repeatition
*0.50 - User input rewrite
*/
float ver = 0.50;
#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define Rbtn 4
#define Gbtn 3
#define Bbtn 2
#define Ybtn 8
#define Cbtn 9
#define Mbtn 10
#define Wbtn 11
#define PIN 5 // The pin your WS2812 strip is connected to
#define NUMPIXELS 16 // Number of LEDs in your strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
bool rbStt = true; //Red Button state
bool gbStt = true; //Green Button state
bool bbStt = true; //Blue Button state
//For wokwi only:
bool ybStt = true; //Yellow Button state
bool mbStt = true; //Magenta Button state
bool cbStt = true; //Cyan Button state
bool wbStt = true; //White Button state
bool gameOn = false; //Boolean to indicate the game has started
bool colSelectStt = false;
int score = 0;
int maxi = 7;
int rndCol; //The random color that needs to be produce
int oldCol; //A var to prevent multi repetiton
int btnSum = 0;
int fdt = 1; //Fast Delay Time
int debounce = 50; //Debounce delay time
int RDV; //RED Display Value
int GDV; //GREEN Display Value
int BDV; //BLUE Display Value
//Button Values
int rbVal;
int gbVal;
int ybVal;
int bbVal;
int mbVal;
int cbVal;
int wbVal;
String colName;
void setup() {
Serial.begin(115200);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
lcd.init();
pinMode(Rbtn, INPUT_PULLUP);
pinMode(Gbtn, INPUT_PULLUP);
pinMode(Bbtn, INPUT_PULLUP);
pinMode(Ybtn, INPUT_PULLUP);
pinMode(Cbtn, INPUT_PULLUP);
pinMode(Mbtn, INPUT_PULLUP);
pinMode(Wbtn, INPUT_PULLUP);
lcd.clear();
lcd.backlight();
lcd.setCursor(2, 0);
lcd.print("PRESS ANY KEY");
lcd.setCursor(4, 1);
lcd.print("TO START");
randomSeed(analogRead(0));
rndCol = random(1,8);
oldCol = rndCol;
}
void loop() {
if(gameOn ==0){
rbStt = digitalRead(Rbtn);
//Serial.print(rbStt);
gbStt = digitalRead(Gbtn);
//Serial.print(gbStt);
bbStt = digitalRead(Bbtn);
//Serial.print(bbStt);
if (rbStt == 0 || gbStt == 0 || bbStt == 0){
lcd.clear();
colSelectStt = true;
}
}
//Color select sequence
if (colSelectStt){
colSelectStt = false;
//Color choosing sequence here
colChoose();
rndCol = random(1,8);
if(oldCol == rndCol){
rndCol = random(1,8);
}
switch (rndCol){
case 1:
colName = "RED";
setColor(strip.Color(255, 0, 0));
break;
case 2:
colName = "GREEN";
setColor(strip.Color(0, 255, 0));
break;
case 3:
colName = "YELLOW";
setColor(strip.Color(255, 255, 0));
break;
case 4:
colName = "BLUE";
setColor(strip.Color(0, 0, 255));
break;
case 5:
colName = "MAGENTA";
setColor(strip.Color(255, 0, 255));
break;
case 6:
colName = "CYAN";
setColor(strip.Color(0, 255, 255));
break;
case 7:
colName = "WHITE";
setColor(strip.Color(255, 255, 255));
break;
}
strip.show(); //Show chosen rndCol
delay(1000);
setColor(strip.Color(0, 0, 0));
strip.show();
/*
Serial.print("Random color is: ");
Serial.print(rndCol);
Serial.print(", ");
Serial.println(colName);
*/
//Display chosen color effect here
lcd.setCursor(2, 0);
lcd.print("COLOR CHOSEN:");
lcd.setCursor(4, 1);
lcd.print(colName);
delay(1000);
oldCol = rndCol;
gameOn = true;
}//End of color select sequence
//Game ON. user input
if(gameOn && !colSelectStt){
//Simple Debounce
rbStt = digitalRead(Rbtn);
gbStt = digitalRead(Gbtn);
bbStt = digitalRead(Bbtn);
ybStt = digitalRead(Ybtn);
mbStt = digitalRead(Mbtn);
cbStt = digitalRead(Cbtn);
wbStt = digitalRead(Wbtn);
/*
Serial.print(rbStt);
Serial.print(gbStt);
Serial.print(bbStt);
Serial.print(ybStt);
Serial.print(cbStt);
Serial.print(mbStt);
Serial.println(wbStt);
*/
if(rbStt == 0 || gbStt == 0 || bbStt == 0 || ybStt == 0 || mbStt == 0 || cbStt == 0 || wbStt == 0){
delay(debounce);
rbStt = digitalRead(Rbtn);
gbStt = digitalRead(Gbtn);
bbStt = digitalRead(Bbtn);
ybStt = digitalRead(Ybtn);
mbStt = digitalRead(Mbtn);
cbStt = digitalRead(Cbtn);
wbStt = digitalRead(Wbtn);
if(rbStt == 0){
rbVal = 1;
RDV = 255;
} else {
rbVal = 0;
RDV = 0;
}
if(gbStt == 0){
gbVal = 2;
GDV = 255;
} else {
gbVal = 0;
GDV = 0;
}
if(ybStt == 0){
ybVal = 3;
RDV = 255;
GDV = 255;
Serial.print("ybVal is: ");
Serial.println(ybVal);
} else {
ybVal = 0;
RDV = 0;
GDV = 0;
}
if(bbStt == 0){
bbVal = 4;
BDV = 255;
} else {
bbVal = 0;
BDV = 0;
}
if(mbStt == 0){
mbVal = 5;
RDV = 255;
BDV = 255;
} else {
mbVal = 0;
RDV = 0;
BDV = 0;
}
if(cbStt == 0){
cbVal = 6;
GDV = 255;
BDV = 255;
} else {
cbVal = 0;
GDV = 0;
BDV = 0;
}
if(wbStt == 0){
wbVal = 7;
RDV = 255;
GDV = 255;
BDV = 255;
Serial.print("wbVal is: ");
Serial.println(wbVal);
} else {
wbVal = 0;
RDV = 0;
GDV = 0;
BDV = 0;
}
setColor(strip.Color(RDV, GDV, BDV)); // RedDisplaying user selected color
strip.show();
//LCD message here
btnSum = rbVal + gbVal + bbVal + ybVal + mbVal + cbVal + wbVal;
Serial.print("btnSum is: ");
Serial.println(btnSum);
delay(1000);
digitalWrite(Rbtn,HIGH);
digitalWrite(Gbtn,HIGH);
digitalWrite(Bbtn,HIGH);
digitalWrite(Cbtn,HIGH);
digitalWrite(Mbtn,HIGH);
digitalWrite(Ybtn,HIGH);
digitalWrite(Wbtn,HIGH);
if(btnSum == rndCol){
score++;
colSelectStt = true;
if(score >= maxi){
score = 0;
youWin();
gameOn = false;
}
}
else {
score = 0;
//gameOn = false;
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("YOU LOSE");
lcd.setCursor(2, 1);
lcd.print("SCORE = 0");
}
}
}
}
/*
if(gameOn && !colSelectStt){
if (digitalRead(Rbtn) == LOW) {
digitalWrite(Rbtn,HIGH);
setColor(strip.Color(255, 0, 0)); // Red
delay(200);
score++;
lcd.clear();
colSelectStt = true;
} else if (digitalRead(Gbtn) == LOW) {
digitalWrite(Gbtn,HIGH);
setColor(strip.Color(0, 255, 0)); // Green
delay(200);
score++;
lcd.clear();
colSelectStt = true;
} else if (digitalRead(Ybtn) == LOW) {
digitalWrite(Gbtn,HIGH);
setColor(strip.Color(255, 255, 0)); // Green
delay(200);
score++;
lcd.clear();
colSelectStt = true;
} else if (digitalRead(Bbtn) == LOW) {
digitalWrite(Bbtn,HIGH);
setColor(strip.Color(0, 0, 255)); // Blue
delay(200);
score++;
lcd.clear();
colSelectStt = true;
} else if (digitalRead(Mbtn) == LOW) {
digitalWrite(Gbtn,HIGH);
setColor(strip.Color(255, 0, 255)); // Green
delay(200);
score++;
lcd.clear();
colSelectStt = true;
} else if (digitalRead(Cbtn) == LOW) {
digitalWrite(Gbtn,HIGH);
setColor(strip.Color(0, 255, 255)); // Green
delay(200);
score++;
lcd.clear();
colSelectStt = true;
} else if (digitalRead(Wbtn) == LOW) {
digitalWrite(Gbtn,HIGH);
setColor(strip.Color(255, 255, 255)); // Green
delay(200);
score++;
lcd.clear();
colSelectStt = true;
}else {
setColor(strip.Color(0, 0, 0)); // Off
}
if(score >=5){
youWin();
}
}
}//**END OF LOOP
*/
void setColor(uint32_t color) {
for(int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
}
strip.show();
}
void youWin(){
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("You WON!");
rainbow(20);
setColor(strip.Color(0, 0, 0));
strip.show();
lcd.setCursor(3, 1);
lcd.print("THANK-YOU");
score = 0;
gameOn =0;
delay(1000);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("PRESS ANY KEY");
lcd.setCursor(4, 1);
lcd.print("TO START");
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}
void colChoose(){
//Fade to Red and out
for(int i=0; i<256; i++){
setColor(strip.Color(i, 0, 0));
strip.show();
delay(fdt);
}
setColor(strip.Color(255, 0, 0));
strip.show();
delay(150);
for(int i=255; i=0; i--){
setColor(strip.Color(i, 0, 0));
strip.show();
delay(fdt);
}
//Fade to Green and out
for(int i=0; i<256; i++){
setColor(strip.Color(0, i, 0));
strip.show();
delay(fdt);
}
setColor(strip.Color(0, 255, 0));
strip.show();
delay(150);
for(int i=255; i=0; i--){
setColor(strip.Color(0, i, 0));
strip.show();
delay(fdt);
}
//Fade to Blue and out
for(int i=0; i<256; i++){
setColor(strip.Color(0, 0, i));
strip.show();
delay(fdt);
}
setColor(strip.Color(0, 0, 255));
strip.show();
delay(150);
for(int i=255; i=0; i--){
setColor(strip.Color(0, 0, i));
strip.show();
delay(fdt);
}
lcd.clear();
}