const int difPot = A0;
const int joyH = A1;
const int joyV = A2;
const int joyS = 0;
const int Start = 13;
const int Led1R = 2;
const int Led1G = 3;
const int Led1B = 4;
const int Led2R = 5;
const int Led2G = 6;
const int Led2B = 7;
const int Led3R = 8;
const int Led3G = 9;
const int Led3B = 10;
float varH = 0;
float varV = 0;
float varP = 0;
bool buttonState = 0;
String colorList1[] = {"255000000","255255000","000255000","000255255","000000255","255000255"};
String colorList2[] = {"255000000","255128000","255255000","128255000","000255000","000255128","000255255","000128255","000000255","127000255","255000255","255000127","128128128"};
String easyList[1] = {};
String normalList[] = {};
String hardList[] = {};
String impossibleList[] = {};
bool Game = false;
int Ronde = 0;
void setup() {
pinMode(joyS, INPUT_PULLUP);
pinMode(Start, INPUT_PULLUP);
pinMode(Led1R, OUTPUT);
pinMode(Led1G, OUTPUT);
pinMode(Led1B, OUTPUT);
pinMode(Led2R, OUTPUT);
pinMode(Led2G, OUTPUT);
pinMode(Led2B, OUTPUT);
pinMode(Led3R, OUTPUT);
pinMode(Led3G, OUTPUT);
pinMode(Led3B, OUTPUT);
Serial.begin(9600);
int Seed = analogRead(A0) + analogRead(A3)+ analogRead(A4)+ analogRead(A5)+ analogRead(A6)+ analogRead(A7)+ analogRead(A8)+ analogRead(A9)+ analogRead(A10)+ analogRead(A11)+ analogRead(A12)+ analogRead(A13)+ analogRead(A14)+ analogRead(A15);
randomSeed(Seed);
Serial.print("RandomSeed: ");
Serial.println(Seed);
}
void loop() {
varP = analogRead(difPot);
buttonState = !digitalRead(Start);
if(varP <256){
Led(1, 0, 255, 0);
Led(2, 0, 255, 0);
Led(3, 0, 255, 0);
Serial.println("Easy");
} else if (varP > 257 && varP < 512) {
Led(1, 255, 255, 0);
Led(2, 255, 255, 0);
Led(3, 255, 255, 0);
Serial.println("Normal");
} else if (varP > 513 && varP < 768) {
Led(1, 255, 128, 0);
Led(2, 255, 128, 0);
Led(3, 255, 128, 0);
Serial.println("Hard");
} else if (varP > 769){
Led(1, 255, 0, 0);
Led(2, 255, 0, 0);
Led(3, 255, 0, 0);
Serial.println("Impossible");
}
if(varP < 256 && buttonState == 1){
Serial.println("Easy Game Start");
easy();
} else if(varP >257 && varP < 512 && buttonState == 1) {
Serial.println("Norma Game Startl");
normal();
} else if (varP > 513 && varP < 768 && buttonState == 1){
Serial.println("Hard Game Start");
hard();
} else if (varP > 769 && buttonState == 1) {
Serial.println("Impossible Game Start");
impossible();
}
}
void Led(int led, int R, int G, int B){
int ledPinR, ledPinG, ledPinB;
if (led == 1) {
ledPinR = Led1R;
ledPinG = Led1G;
ledPinB = Led1B;
} else if (led == 2) {
ledPinR = Led2R;
ledPinG = Led2G;
ledPinB = Led2B;
} else if (led == 3) {
ledPinR = Led3R;
ledPinG = Led3G;
ledPinB = Led3B;
}
analogWrite(ledPinR, R);
analogWrite(ledPinG, G);
analogWrite(ledPinB, B);
}
void easy(){
Game = true;
//color list 1
while(Ronde < 20 && Game == true){
Led(1,0,0,0);
Led(2,0,0,0);
Led(3,0,0,0);
int color = random(0,6);
String SeleColor = colorList1[color];
String valR = SeleColor.substring(0,3);
String valG = SeleColor.substring(3,6);
String valB = SeleColor.substring(6);
int led =random(1,4);
easyList[Ronde] = String(led+SeleColor);
Ronde++;
Led(led, valR.toInt(), valG.toInt(), valB.toInt());
delay(1000);
Led(1,0,0,0);
Led(2,0,0,0);
Led(3,0,0,0);
bool roundComplete = true;
for (int i = 0; i < Ronde; i++) {
String PlayerChoice = player(6, "1", Ronde);
if (PlayerChoice != easyList[i]) {
Serial.println("Incorrect");
Serial.println("Je hebt ");
Serial.print(Ronde-1);
Serial.print(" overleeft!");
fout();
roundComplete = false;
delay(1000);
reset(Ronde);
delay(500);
Game = false;
break;
} else{
Serial.println("Correct");
}
}
if (roundComplete && Ronde == 20) {
Serial.println("You Won!");
Game = false;
}
}
}
void normal(){
Game = true;
//color list 1
while(Ronde < 20 && Game == true){
Led(1,0,0,0);
Led(2,0,0,0);
Led(3,0,0,0);
int color = random(0,6);
String SeleColor = colorList1[color];
String valR = SeleColor.substring(0,3);
String valG = SeleColor.substring(3,6);
String valB = SeleColor.substring(6);
int led =random(1,4);
easyList[Ronde] = String(led+SeleColor);
Ronde++;
Led(led, valR.toInt(), valG.toInt(), valB.toInt());
delay(800);
Led(1,0,0,0);
Led(2,0,0,0);
Led(3,0,0,0);
bool roundComplete = true;
for (int i = 0; i < Ronde; i++) {
String PlayerChoice = player(6, "1", Ronde);
if (PlayerChoice != easyList[i]) {
Serial.println("Incorrect");
Serial.println("Je hebt ");
Serial.print(Ronde-1);
Serial.print(" overleeft!");
roundComplete = false;
delay(1000);
reset(Ronde);
delay(500);
Game = false;
break;
} else{
Serial.println("Correct");
}
}
if (roundComplete && Ronde == 20) {
Serial.println("You Won!");
Game = false;
}
}
}
void hard(){
Game = true;
//color list 2
while(Ronde < 20 && Game == true){
Led(1,0,0,0);
Led(2,0,0,0);
Led(3,0,0,0);
int color = random(0,6);
String SeleColor = colorList2[color];
String valR = SeleColor.substring(0,3);
String valG = SeleColor.substring(3,6);
String valB = SeleColor.substring(6);
int led =random(1,4);
easyList[Ronde] = String(led+SeleColor);
Ronde++;
Led(led, valR.toInt(), valG.toInt(), valB.toInt());
delay(800);
Led(1,0,0,0);
Led(2,0,0,0);
Led(3,0,0,0);
bool roundComplete = true;
for (int i = 0; i < Ronde; i++) {
String PlayerChoice = player(13, "2", Ronde);
if (PlayerChoice != easyList[i]) {
Serial.println("Incorrect");
Serial.println("Je hebt ");
Serial.print(Ronde-1);
Serial.print(" overleeft!");
roundComplete = false;
delay(1000);
reset(Ronde);
delay(500);
Game = false;
break;
} else{
Serial.println("Correct");
}
}
if (roundComplete && Ronde == 20) {
Serial.println("You Won!");
Game = false;
}
}
}
void impossible(){
//rgb collor random
}
String player(int listLen, String List, int Rondes) {
int LedChoise = 2;
bool ledSelected = false;
int previousVerticalValue = analogRead(joyV);
int previousHorizontalValue = analogRead(joyH);
int color = 0;
int Colors = 0;
String choisenColor = "255000000";
Led(1,0,0,0);
Led(3,0,0,0);
Led(LedChoise, 255, 255, 255);
delay(500);
Led(LedChoise, 255, 0, 0);
while (!ledSelected && (Colors < Rondes)) {
int verticalValue = analogRead(joyV);
int horizontalValue = analogRead(joyH);
bool confirmButton = digitalRead(joyS);
// Horizontaal bewegen om LED te kiezen
if (abs(horizontalValue - previousHorizontalValue) > 50) {
if (horizontalValue < 400) {
LedChoise++;
if (LedChoise > 3) LedChoise = 1;
} else if (horizontalValue > 600) {
LedChoise--;
if (LedChoise < 1) LedChoise = 3;
}
updateLEDColor(LedChoise, choisenColor);
delay(300);
}
// Verticaal bewegen om de kleur van de LED aan te passen
if (abs(verticalValue - previousVerticalValue) > 50) {
if (verticalValue < 400) {
color--;
if (color < 0 ) color = listLen-1;
} else if (verticalValue > 600) {
color++;
if (color > listLen-1 ) color = 0;
}
updateLEDColor(LedChoise, choisenColor);
delay(300);
}
if(List == "1") choisenColor = colorList1[color];
else if(List == "2") choisenColor = colorList2[color];
// Controleer of de knop "joyS" is ingedrukt om de keuze te bevestigen
if (confirmButton == LOW) {
ledSelected = true;
String choise = LedChoise + choisenColor;
return choise;
}
previousVerticalValue = verticalValue;
previousHorizontalValue = horizontalValue;
}
}
// Functie om de LED-kleur te updaten op basis van de geselecteerde LED-keuze
void updateLEDColor(int led, String Color) {
String R = Color.substring(0,3);
String G = Color.substring(3,6);
String B = Color.substring(6);
Led(1,0,0,0);
Led(2,0,0,0);
Led(3,0,0,0);
Led(led, R.toInt() , G.toInt() , B.toInt() );
}
void reset(int Ronde){
for(int i=0; i<Ronde;i++){
easyList[i] = "";
normalList[i] = "";
hardList[i] = "";
impossibleList[i] = "";
}
Serial.println("Reset Complete");
}
void fout(){
for(int i = 0;i < 3; i++){
Led(1,255,0,0);
Led(2,255,0,0);
Led(3,255,0,0);
delay(250);
Led(1,0,0,0);
Led(2,0,0,0);
Led(3,0,0,0);
delay(250);
}
}