const int bulbPins[] = {22,23,24,25,26,27};
const int numBulbs = sizeof(bulbPins) / sizeof(bulbPins[0]);
int ledPins[6] = {22,23,24,25,26,27};
int buttonPins[6] = {28,29,30,31,32,33};
int startPin = A0;
int levelPin = A1;
int arraySize = 7;
int levels = 1;
int countRound = 1;
int wrongCount = 0;
int gameStart = 0;
// ประกาศตัวแปรอาร์เรย์
int ledArray[6] = {-1,-1,-1,-1,-1, -1};
int answerArray[6] = {-1,-1,-1,-1,-1, -1};
int incomplete = 0;
void randomLed(int numOfLed){
int SEED = analogRead(A5);
for (int i = 0; i < numOfLed; i++) {
ledArray[i] = random(0, 6);
Serial.print(ledArray[i]);
incomplete = 1;
}
runLed(numOfLed);
}
void runLed(int numOfLed){
for (int i = 0; i < numOfLed; i++){
digitalWrite(ledPins[ledArray[i]], HIGH);
if(levels == 1 || levels == 3 || levels == 5 || levels == 7 || levels == 9){
delay(3000);
}
else{
delay(2000);
}
digitalWrite(ledPins[ledArray[i]], LOW);
delay(200);
}
}
void setup() {
// ตั้งค่าขาเป็น OUTPUT
for (int i = 0; i < numBulbs; i++) {
pinMode(bulbPins[i], OUTPUT);
}
// ตั้งค่าขาเป็น INPUT
for (int i=0; i<6; i++){
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT);
}
pinMode(startPin, INPUT);
pinMode(levelPin, INPUT);
Serial.begin(9600);
// ตั้งค่า random seed
randomSeed(analogRead(A3));
}
void loop() {
int startButton = digitalRead(startPin);
//Serial.print(digitalRead(startPin));
//Serial.print(startButton);
int levelUp = digitalRead(levelPin);
if(levelUp == 1){
if(levels < 9){
levels = levels + 1;
Serial.println(levels);
delay(500);
}else{
levels = 1;
Serial.println(levels);
delay(500);
}
}
if (startButton == 1){
gameStart = 1;
}
if(gameStart == 1){
//Start game levels/////////////////////////////////////////////////////
if(levels == 0){
// Do nothing
}
//////////////////////////////////////////////////////////////
if(levels == 1){
if (countRound < 5){
randomLed(3);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 1;
wrongCount =0;
countRound = 0;
}
else{
levels =2;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
if(levels == 2){
if (countRound < 5){
randomLed(3);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 2;
wrongCount =0;
countRound = 0;
}
else{
levels =3;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
if(levels == 3){
if (countRound < 5){
randomLed(4);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 3;
wrongCount =0;
countRound = 0;
}
else{
levels =4;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
if(levels == 4){
if (countRound < 5){
randomLed(4);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 4;
wrongCount =0;
countRound = 0;
}
else{
levels =5;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
if(levels == 5){
if (countRound < 5){
randomLed(5);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 5;
wrongCount =0;
countRound = 0;
}
else{
levels =6;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
if(levels == 6){
if (countRound < 5){
randomLed(5);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 6;
wrongCount =0;
countRound = 0;
}
else{
levels =7;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
if(levels == 7){
if (countRound < 5){
randomLed(6);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 6;
wrongCount =0;
countRound = 0;
}
else{
levels =7;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
if(levels == 8){
if (countRound < 5){
randomLed(6);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 8;
wrongCount =0;
countRound = 0;
}
else{
levels =9;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
if(levels == 9){
if (countRound < 5){
randomLed(7);
for(int i=0; i<7; i++){
Serial.print(ledArray[i]);
}
int x=0;
while(incomplete == 1){
for(int i=8; i<14 ; i++){
if(digitalRead(i)){
answerArray[x] = i-8;
Serial.print(i-8);
x++;
delay(500);
break;
}
}
if(x==3){break;}
}
int correct=1;
for(int i=0; i<6; i++){
if(ledArray[i]!=answerArray[i]){correct=0;}
}
if(correct==1){
Serial.println("C");
}else{
Serial.println("W");
wrongCount =wrongCount+1;
}
if(wrongCount >=2){
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(countRound);
countRound++;
}else
{
if (wrongCount >=2){
levels = 9;
wrongCount =0;
countRound = 0;
}
else{
///finished levels =6;
wrongCount =0;
countRound = 0;
}
}
}
//////////////////////////////////////////////////////////////
}
//end game levels////////////////////////////////////////////////////////////
}//finish loop