Devices & Components
Arduino Starter Kit [English]
Software & Tools
Arduino IDE
Project description
Code
New Code
cpp
Fixed code
1// C++ code 2// 3#include <LiquidCrystal.h> 4 5int buttonVal = 0; // which button is being pressed 6 7int score = 0; // score 8 9LiquidCrystal lcd_2(5, 6, 7, 8, 9, 10); 10 11//led blinking 12bool blinkLed(int pin){ 13 bool state; 14 digitalWrite(pin, HIGH); 15 if (digitalRead(pin+9) == LOW){ 16 state = true; 17 } else { 18 state = false; 19 } 20 delay(200); 21 digitalWrite(pin, LOW); 22 return state; 23} 24 25//setup code, we need to initialize lcd and pins 26void setup(){ 27 lcd_2.begin(16, 2); // Set up the number of columns and rows on the LCD. 28 pinMode(2, OUTPUT); 29 pinMode(3, OUTPUT); 30 pinMode(4, OUTPUT); 31 pinMode(11, INPUT_PULLUP); 32 pinMode(12, INPUT_PULLUP); 33 pinMode(13, INPUT_PULLUP); 34} 35 36//main game loop 37void loop() 38{ 39 //menu screen 40 score = 0; 41 42 lcd_2.print("Press middle button to start."); 43 44 while (digitalRead(12) == HIGH){ 45 lcd_2.scrollDisplayLeft(); 46 delay(500); 47 } 48 49 lcd_2.clear(); 50 51 int startTime = millis(); 52 53 //actual game 54 while ((millis() - startTime) < 90000){ 55 for (int i = 2; i <= 4; i++){ 56 int randInt = rand() % 3 ; 57 lcd_2.print("Score: "); 58 lcd_2.print(score); 59 bool buttonState = blinkLed(randInt+2); 60 if (buttonState == true){ 61 score++; 62 } 63 buttonState = false; 64 //if (digital) 65 delay(100*(rand()%10)); 66 lcd_2.clear(); 67 } 68 } 69}
whack_a_button_mini
cpp
It's my first time writing long code by myself lol
1// C++ code 2// 3#include <LiquidCrystal.h> 4 5int buttonVal = 0; // which button is being pressed 6 7int score = 0; // score 8 9LiquidCrystal lcd_2(5, 6, 7, 8, 9, 10); 10 11//led blinking 12bool blinkLed(int pin){ 13 bool state; 14 digitalWrite(pin, HIGH); 15 if (digitalRead(pin+9) == HIGH){ 16 state = true; 17 } else { 18 state = false; 19 } 20 delay(200); 21 digitalWrite(pin, LOW); 22 return state; 23} 24 25//setup code, we need to initialize lcd and pins 26void setup(){ 27 lcd_2.begin(16, 2); // Set up the number of columns and rows on the LCD. 28 pinMode(2, OUTPUT); 29 pinMode(3, OUTPUT); 30 pinMode(4, OUTPUT); 31 pinMode(11, INPUT); 32 pinMode(12, INPUT); 33 pinMode(13, INPUT); 34} 35 36//main game loop 37void loop() 38{ 39 //menu screen 40 score = 0; 41 42 lcd_2.print("Press middle button to start."); 43 44 while (digitalRead(12) == LOW){ 45 lcd_2.scrollDisplayLeft(); 46 delay(500); 47 } 48 49 lcd_2.clear(); 50 51 int startTime = millis(); 52 53 //actual game 54 while ((millis() - startTime) < 90000){ 55 for (int i = 2; i <= 4; i++){ 56 int randInt = rand() % 3 ; 57 lcd_2.print("Score: "); 58 lcd_2.print(score); 59 bool buttonState = blinkLed(randInt+2); 60 if (buttonState == true){ 61 score++; 62 } 63 buttonState = false; 64 //if (digital) 65 delay(100*(rand()%10)); 66 lcd_2.clear(); 67 } 68 } 69}
Comments
Only logged in users can leave comments