From eea64b72032c2dec577893f6c4356088efa8b7a1 Mon Sep 17 00:00:00 2001 From: Raiyan2000 <77954118+Raiyan2000@users.noreply.github.com> Date: Wed, 19 Oct 2022 16:08:56 -0400 Subject: [PATCH 1/7] Setup OLED and new var, userPoints --- Bop_it.ino | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/Bop_it.ino b/Bop_it.ino index 93413e6..03e8ab8 100644 --- a/Bop_it.ino +++ b/Bop_it.ino @@ -1,3 +1,12 @@ +#include +#include +#include + +#define SCREEN_WIDTH 128 // OLED display width, in pixels +#define SCREEN_HEIGHT 64 // OLED display height, in pixels + +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); + int Button_start_pin = 8; int speaker_output = 6; int scl_lcd = A5, sda_lcd = A4; //LCD outputs @@ -9,6 +18,8 @@ int command_num; bool game_is_on = true; int answer_time; bool correct_answer; +int temp_dial_volt; +int userPoints = 0; void setup() { // put your setup code here, to run once: @@ -25,13 +36,22 @@ void setup() { pinMode(led_dial, OUTPUT); pinMode(led_microphone, OUTPUT); + Serial.begin(115200); + //Might have to change address + if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 + Serial.println(F("SSD1306 allocation failed")); + } + delay(2000); + display.clearDisplay(); + + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(5, 10); } bool buttonInput(int responseTime) { - //Speaker beep for button - unsigned long start = millis(); digitalWrite(led_button, HIGH); @@ -148,8 +168,10 @@ void speakerOutput(int command) void loop() { // put your main code here, to run repeatedly: //Check if start button is pressed which would start the game - - + display.clearDisplay(); + display.println("Start"); + display.display(); + if(digitalRead(Button_start_pin) == HIGH) { game_is_on = true; @@ -159,14 +181,16 @@ void loop() { game_is_on = false; } + while(game_is_on) { - //choose random command - //command_num = random(0,2); + display.clearDisplay(); + display.println("Points: "); + display.display(); - //Choose one command randomly - int temp_dial_volt; - command_num = 0; + //choose random command + command_num = random(0,2); + //command_num = 0; answer_time = 1000; @@ -203,11 +227,17 @@ void loop() { game_is_on = false; //Set the timer to default value answer_time = 1000; + } else { //Reduce answer time by 5ms answer_time -= 5; + + //Update display + userPoints += 1; + + } } From d79f0104884c6bb8c1f1af0ef5c8173a29319284 Mon Sep 17 00:00:00 2001 From: Raiyan2000 <77954118+Raiyan2000@users.noreply.github.com> Date: Fri, 28 Oct 2022 16:23:10 -0400 Subject: [PATCH 2/7] Updated dial --- Bop_it.ino | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Bop_it.ino b/Bop_it.ino index 03e8ab8..994e158 100644 --- a/Bop_it.ino +++ b/Bop_it.ino @@ -66,7 +66,7 @@ bool buttonInput(int responseTime) return true; } } - digitalWrite(correct_led, LOW); + //digitalWrite(correct_led, LOW); digitalWrite(wrong_led, HIGH); delay(500); digitalWrite(wrong_led, LOW); @@ -112,20 +112,21 @@ bool dialInput(int responseTime, int temp_volt) { current_volt = analogRead(dial_cmd_input); - if (temp_volt - current_volt != 0) + if ((temp_volt - current_volt < 0.5 && temp_volt - current_volt > 0) || (temp_volt - current_volt > -0.5 && temp_volt - current_volt < 0) ) { + digitalWrite(led_dial, LOW); digitalWrite(correct_led, HIGH); - delay(500); + delay(2000); digitalWrite(correct_led, LOW); - digitalWrite(led_dial, LOW); return true; } } - digitalWrite(correct_led, LOW); + //digitalWrite(correct_led, LOW); + digitalWrite(led_dial, LOW); digitalWrite(wrong_led, HIGH); - delay(500); + delay(2000); digitalWrite(wrong_led, LOW); - digitalWrite(led_dial, LOW); + return false; } @@ -188,6 +189,8 @@ void loop() { display.println("Points: "); display.display(); + digitalWrite(correct_led, LOW); + //choose random command command_num = random(0,2); //command_num = 0; From afc0cdecf8d64bee740440141ffda0bd850b4b83 Mon Sep 17 00:00:00 2001 From: Raiyan2000 <77954118+Raiyan2000@users.noreply.github.com> Date: Fri, 28 Oct 2022 17:05:04 -0400 Subject: [PATCH 3/7] Updated dial Added two states --- Bop_it.ino | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/Bop_it.ino b/Bop_it.ino index 994e158..b3b5877 100644 --- a/Bop_it.ino +++ b/Bop_it.ino @@ -105,21 +105,53 @@ bool micInput(int responseTime){ bool dialInput(int responseTime, int temp_volt) { unsigned long start = millis(); + bool state = false; int current_volt; + temp_dial_volt = analogRead(dial_cmd_input); + + if(temp_dial_volt < 300) + { + //Low + state = false; + } + else if(temp_dial_volt > 800) + { + //High + state = true; + } + digitalWrite(led_dial, HIGH); + while (millis() - start < responseTime) { current_volt = analogRead(dial_cmd_input); - if ((temp_volt - current_volt < 0.5 && temp_volt - current_volt > 0) || (temp_volt - current_volt > -0.5 && temp_volt - current_volt < 0) ) + //high + if (state == true) { - digitalWrite(led_dial, LOW); - digitalWrite(correct_led, HIGH); - delay(2000); - digitalWrite(correct_led, LOW); - return true; + if (current_volt < 400) + { + digitalWrite(led_dial, LOW); + digitalWrite(correct_led, HIGH); + delay(2000); + digitalWrite(correct_led, LOW); + return true; + } + } + //low + else + { + if (current_volt > 700) + { + digitalWrite(led_dial, LOW); + digitalWrite(correct_led, HIGH); + delay(2000); + digitalWrite(correct_led, LOW); + return true; + } } + } //digitalWrite(correct_led, LOW); digitalWrite(led_dial, LOW); From 9af95cdfffb8978e47a1a4d9ca7a240cef0833d7 Mon Sep 17 00:00:00 2001 From: Raiyan2000 <77954118+Raiyan2000@users.noreply.github.com> Date: Mon, 31 Oct 2022 11:18:44 -0400 Subject: [PATCH 4/7] Added Oled function --- Bop_it.ino | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Bop_it.ino b/Bop_it.ino index b3b5877..e90d2c3 100644 --- a/Bop_it.ino +++ b/Bop_it.ino @@ -81,6 +81,8 @@ bool micInput(int responseTime){ unsigned int sound; unsigned int signalMax = 0; unsigned int signalMin = 1024; + + //digitalWrite(microphone_cmd_input, HIGH); // collect data for responseTime miliseconds while (millis() - start < responseTime) @@ -105,7 +107,7 @@ bool micInput(int responseTime){ bool dialInput(int responseTime, int temp_volt) { unsigned long start = millis(); - bool state = false; + bool state; int current_volt; temp_dial_volt = analogRead(dial_cmd_input); @@ -114,7 +116,7 @@ bool dialInput(int responseTime, int temp_volt) //Low state = false; } - else if(temp_dial_volt > 800) + else if(temp_dial_volt > 1000) { //High state = true; @@ -258,23 +260,37 @@ void loop() { if (correct_answer == false) { + display.clearDisplay(); + display.println("Points: "); + display.print(userPoints, DEC); + delay(1000); + //sprintf(buf, "%d", points); + //u8g.drawStr(18, 3, buf); //Call endgame function game_is_on = false; //Set the timer to default value - answer_time = 1000; + answer_time = 2000; + + userPoints = 0; } else { + //Reduce answer time by 5ms answer_time -= 5; //Update display userPoints += 1; - + display.clearDisplay(); + display.println("Points: "); + display.print(userPoints, DEC); + delay(1000); } + + } } From 2106fc2758e6d7837f71c4c250dd5b4bb0e0cdaf Mon Sep 17 00:00:00 2001 From: Raiyan2000 <77954118+Raiyan2000@users.noreply.github.com> Date: Wed, 2 Nov 2022 10:08:38 -0400 Subject: [PATCH 5/7] Update Bop_it.ino --- Bop_it.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bop_it.ino b/Bop_it.ino index e90d2c3..6d3f140 100644 --- a/Bop_it.ino +++ b/Bop_it.ino @@ -46,7 +46,7 @@ void setup() { display.setTextSize(1); display.setTextColor(WHITE); - display.setCursor(5, 10); + display.setCursor(0, 10); } From 7d6fc6023e062753f474fb0a5f10944007f10f1f Mon Sep 17 00:00:00 2001 From: Raiyan2000 <77954118+Raiyan2000@users.noreply.github.com> Date: Mon, 7 Nov 2022 14:54:39 -0500 Subject: [PATCH 6/7] Update BopIt Final addition of commands --- Bop_it.ino | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Bop_it.ino b/Bop_it.ino index 6d3f140..f2be06c 100644 --- a/Bop_it.ino +++ b/Bop_it.ino @@ -36,18 +36,7 @@ void setup() { pinMode(led_dial, OUTPUT); pinMode(led_microphone, OUTPUT); - Serial.begin(115200); - //Might have to change address - if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 - Serial.println(F("SSD1306 allocation failed")); - } - delay(2000); - display.clearDisplay(); - - display.setTextSize(1); - display.setTextColor(WHITE); - display.setCursor(0, 10); - + } bool buttonInput(int responseTime) @@ -203,7 +192,17 @@ void speakerOutput(int command) void loop() { // put your main code here, to run repeatedly: //Check if start button is pressed which would start the game + Serial.begin(115200); + //Might have to change address + if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 + Serial.println(F("SSD1306 allocation failed")); + } + delay(2000); display.clearDisplay(); + + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(2, 10); display.println("Start"); display.display(); @@ -285,7 +284,7 @@ void loop() { display.clearDisplay(); display.println("Points: "); - display.print(userPoints, DEC); + display.print(user_Points, DEC); delay(1000); } From 087e9da6f2916484cfcdbe7898960bf55c40051f Mon Sep 17 00:00:00 2001 From: Raiyan2000 <77954118+Raiyan2000@users.noreply.github.com> Date: Mon, 7 Nov 2022 17:37:26 -0500 Subject: [PATCH 7/7] Final changes to Bop it --- Bop_it.ino | 212 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 163 insertions(+), 49 deletions(-) diff --git a/Bop_it.ino b/Bop_it.ino index f2be06c..636d4cf 100644 --- a/Bop_it.ino +++ b/Bop_it.ino @@ -12,14 +12,13 @@ int speaker_output = 6; int scl_lcd = A5, sda_lcd = A4; //LCD outputs int button_cmd_input = 7, dial_cmd_input = A2, microphone_cmd_input = A3; //Command Inputs int correct_led = 10, wrong_led = 9; //LED pins for controlling right and wrong answer -int led_button = 0, led_dial = 1, led_microphone = 2; //LED pins that light up with each commands +int led_button = 12, led_dial = 11, led_microphone = 2; //LED pins that light up with each commands int command_num; bool game_is_on = true; int answer_time; bool correct_answer; -int temp_dial_volt; -int userPoints = 0; +int userPoints=0; void setup() { // put your setup code here, to run once: @@ -35,12 +34,97 @@ void setup() { pinMode(led_button, OUTPUT); pinMode(led_dial, OUTPUT); pinMode(led_microphone, OUTPUT); + +} + +bool buttonInputTest() +{ + //Speaker beep for button + if (digitalRead(button_cmd_input) == HIGH) + { + return true; + } + + return false; +} +bool dialInputTest() +{ + bool state = false; + int current_volt; + int temp_dial_volt = analogRead(dial_cmd_input); + + if(temp_dial_volt < 300) + { + //Low + state = false; + } + else if(temp_dial_volt > 800) + { + //High + state = true; + } + current_volt = analogRead(dial_cmd_input); + + //high + if (state == true) + { + if (current_volt < 400) + { + return true; + } + } + //low + else + { + if (current_volt > 700) + { + return true; + } + } + return false; +} + +bool micInputTest(){ + + unsigned int peakToPeak = 0; // peak-to-peak level + unsigned int sound; + unsigned int signalMax = 0; + unsigned int signalMin = 1024; + //led + // collect data for responseTime miliseconds + sound = analogRead(microphone_cmd_input); + + if(sound>=800){ + return true; + } + + //This is the max of the 10-bit ADC so this loop will include all readings + if (sound < 1024){ + if (sound > signalMax){ + signalMax = sound; // save just the max levels + } + else if (sound < signalMin){ + signalMin = sound; // save just the min levels + } + } + return false; } + + + + + + + + + bool buttonInput(int responseTime) { + //Speaker beep for button + bool incorrect_input[2]; unsigned long start = millis(); digitalWrite(led_button, HIGH); @@ -54,8 +138,18 @@ bool buttonInput(int responseTime) digitalWrite(led_button, LOW); return true; } + incorrect_input[0] = micInputTest(); + incorrect_input[1] = dialInputTest(); + + for(int i = 0; i < 2; i++) + { + if(incorrect_input[i] == true) + { + break; + } + } } - //digitalWrite(correct_led, LOW); + digitalWrite(correct_led, LOW); digitalWrite(wrong_led, HIGH); delay(500); digitalWrite(wrong_led, LOW); @@ -70,42 +164,55 @@ bool micInput(int responseTime){ unsigned int sound; unsigned int signalMax = 0; unsigned int signalMin = 1024; + bool incorrect_input[2]; + //led + digitalWrite(led_microphone,HIGH); - //digitalWrite(microphone_cmd_input, HIGH); - + // collect data for responseTime miliseconds while (millis() - start < responseTime) { sound = analogRead(microphone_cmd_input); - //This is the max of the 10-bit ADC so this loop will include all readings - if (sound < 1024){ - if (sound > signalMax){ - signalMax = sound; // save just the max levels - } - else if (sound < signalMin){ - signalMin = sound; // save just the min levels - } + if(sound>=800){ + digitalWrite(correct_led, HIGH); + delay(500); + digitalWrite(correct_led, LOW); + digitalWrite(led_microphone, LOW); + return true; + } + incorrect_input[0] = buttonInputTest(); + incorrect_input[1] = dialInputTest(); + + for(int i = 0; i < 2; i++) + { + if(incorrect_input[i] == true) + { + break; + } } } - peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude - double volts = (peakToPeak * 5) / 1024; // convert to volts - - return (volts>=2.0) ? true:false; + digitalWrite(correct_led, LOW); + digitalWrite(wrong_led, HIGH); + delay(500); + digitalWrite(wrong_led, LOW); + digitalWrite(led_microphone, LOW); + return false; } bool dialInput(int responseTime, int temp_volt) { unsigned long start = millis(); - bool state; + bool state = false; int current_volt; - temp_dial_volt = analogRead(dial_cmd_input); + int temp_dial_volt = analogRead(dial_cmd_input); + bool incorrect_input[2]; if(temp_dial_volt < 300) { //Low state = false; } - else if(temp_dial_volt > 1000) + else if(temp_dial_volt > 800) { //High state = true; @@ -142,7 +249,16 @@ bool dialInput(int responseTime, int temp_volt) return true; } } - + incorrect_input[0] = micInputTest(); + incorrect_input[1] = buttonInputTest(); + + for(int i = 0; i < 2; i++) + { + if(incorrect_input[i] == true) + { + break; + } + } } //digitalWrite(correct_led, LOW); digitalWrite(led_dial, LOW); @@ -190,9 +306,7 @@ void speakerOutput(int command) } void loop() { - // put your main code here, to run repeatedly: - //Check if start button is pressed which would start the game - Serial.begin(115200); + Serial.begin(115200); //Might have to change address if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 Serial.println(F("SSD1306 allocation failed")); @@ -200,10 +314,13 @@ void loop() { delay(2000); display.clearDisplay(); - display.setTextSize(1); + display.setTextSize(2); display.setTextColor(WHITE); - display.setCursor(2, 10); - display.println("Start"); + display.setCursor(0, 10); + // put your main code here, to run repeatedly: + //Check if start button is pressed which would start the game + //display.clearDisplay(); + display.print("Start"); display.display(); if(digitalRead(Button_start_pin) == HIGH) @@ -215,19 +332,21 @@ void loop() { game_is_on = false; } - while(game_is_on) { display.clearDisplay(); + display.setCursor(1, 10); display.println("Points: "); + display.print(userPoints, DEC); display.display(); - - digitalWrite(correct_led, LOW); - + //choose random command - command_num = random(0,2); - //command_num = 0; - answer_time = 1000; + command_num = random(0,3); + + //Choose one command randomly + int temp_dial_volt; + //command_num = 2; + answer_time = 5000; //if command is pressing the button @@ -248,7 +367,7 @@ void loop() { correct_answer = dialInput(answer_time, temp_dial_volt); } - else + else if(command_num==2) { //speaker beeps for microphone speakerOutput(command_num); @@ -260,36 +379,31 @@ void loop() { if (correct_answer == false) { display.clearDisplay(); + display.setCursor(1, 10); display.println("Points: "); display.print(userPoints, DEC); + display.display(); delay(1000); - //sprintf(buf, "%d", points); - //u8g.drawStr(18, 3, buf); //Call endgame function game_is_on = false; //Set the timer to default value - answer_time = 2000; - - userPoints = 0; - + answer_time = 5000; + userPoints=0; } else { - //Reduce answer time by 5ms answer_time -= 5; - - //Update display + //Update display userPoints += 1; display.clearDisplay(); + display.setCursor(1, 10); display.println("Points: "); - display.print(user_Points, DEC); + display.print(userPoints, DEC); + display.display(); delay(1000); } - - } } -