lma3
Published © GPL3+

Louie's Arduino Race Game

Play against a friend in this action packed Arduino game that is simple and easy to build!

IntermediateShowcase (no instructions)838
Louie's Arduino Race Game

Things used in this project

Hardware components

Wire, Hook Up
Wire, Hook Up
×18
Resistor, 300 ohm
Resistor, 300 ohm
×8
Through Hole Resistor, 200 ohm
Through Hole Resistor, 200 ohm
×3
5 mm LED: Red
5 mm LED: Red
×6
5 mm LED: Yellow
5 mm LED: Yellow
×2
Switch Actuator, Head for spring return push-button
Switch Actuator, Head for spring return push-button
×3
Arduino UNO
Arduino UNO
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1

Story

Read more

Schematics

arduino_game_NV0Sb7OfEa.png

Code

Arduino game

C/C++
//https://pastebin.com/HffCmD2V
//https://hackaday.io/project/3477-race-to-the-led-arduino-game#menu-description
//got code from this website and tweaked it with things I wanted and revisions

const int button1 = 8;
const int button2 = 4;
const int reset = 13;

const int lvl1led1 = 11;
const int lvl2led1 = 10;
const int lvl3led1 = 9;

const int lvl1led2 = 5;
const int lvl2led2 = 6;
const int lvl3led2 = 7;

const int counterled1 = 1;
const int counterled2 = 0;


int lastbuttonstate = 0;
int lastbuttonstate2 = 0;

 
 int goal = 60;
 int state1 ; 				//didn't assign a value because will be assigned later
 int state2 ;
int stateReset ;
 
 int counter1 = 0;			//assigning 0 since the score would start at 0
 int counter2 = 0; 
 
 
 void setup() {
 
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(lvl1led1, OUTPUT);
  pinMode(lvl1led2, OUTPUT);
  pinMode(lvl2led1, OUTPUT);
  pinMode(lvl2led2, OUTPUT);
  pinMode(lvl3led1, OUTPUT);
  pinMode(lvl3led2, OUTPUT);
  pinMode(counterled1,OUTPUT);
  pinMode(counterled2, OUTPUT);			//declaring all outputs and inputs
  pinMode(reset, INPUT);
   
 }
 
 void loop() {
 
   state1 = digitalRead(button1);  //set digitalread to state1 to check if button1 is pressed
   state2 = digitalRead(button2);  //set digitalread to state2 to check if button2 is pressed
   stateReset = digitalRead(reset); //set digitalread to stateReset to check if reset is pressed
   
// used nested if statements to prevent holding down button and getting clicks

   if(state1 != lastbuttonstate) { //check if state1 is not equal to lastbuttonstate
     
     if (state1 == HIGH) { //check if state1 is high
       
       counter1 = counter1 + 1; // add 1 point to counter1
       digitalWrite(counterled1, HIGH);
       
     } else {  //if state1 isn’t high run this code
       digitalWrite(counterled1, LOW);
     } 
     
   }
   lastbuttonstate = state1; 
//this code prevents player 1 users from holding the button to get points
   
   if(state2 != lastbuttonstate2) { //check if state2 is not equal to lastbuttonstate2
     
     if(state2 == HIGH) { //check if state2 is high
       
       counter2 = counter2 + 1; //add 1 point to counter2
       digitalWrite(counterled2, HIGH);
     } else{ // if state 2 is low run this code
       
       digitalWrite(counterled2, LOW);
     }
   }
   lastbuttonstate2 = state2; 
// this code prevents player 2 users from holding the button to get points
   

//______________________________________   
   
  if(counter1 >= 20) { //if counter1 reaches a score of 10 or more turn on first light
     
     digitalWrite(lvl1led1, HIGH);   
   }
   
   if(counter2 >= 20) { //if counter2 reaches a score of 10 or more turn on first light
     
     digitalWrite(lvl1led2, HIGH); 

   }
//_______________________________________
   
  if(counter1 >= 40) { //if counter1 reaches a score of 40 or more turn on second light
     
     digitalWrite(lvl2led1, HIGH);   
   }
   
   if(counter2 >= 40) { //if counter2 reaches a score of 40 or more turn on second light
     
     digitalWrite(lvl2led2, HIGH);   
   } 
//________________________________________  
   
   
   if (counter1 >= goal) {
// if counter1 reaches 60 or more reset the opponent's lights and counter to indicate the win
     
     digitalWrite(lvl3led1, HIGH);
     
     counter2 = 0;
     digitalWrite(lvl1led2,LOW);
     digitalWrite(lvl2led2,LOW);
     digitalWrite(lvl3led2,LOW);
   } 
   
   



if (counter2 >= goal) { 
// if counter1 reaches 60 or more reset the opponent's lights and counter to indicate the win

     digitalWrite(lvl3led2, HIGH);
     counter1 = 0;
     digitalWrite(lvl1led1,LOW);
     digitalWrite(lvl2led1,LOW);
     digitalWrite(lvl3led1,LOW);
   }  

   if (stateReset == HIGH) { 
//if stateReset is equal to high reset all counters and turn off all lights
    counter1 = 0;
    counter2 = 0;
     
     digitalWrite(lvl1led1,LOW);
     digitalWrite(lvl2led1,LOW);
     digitalWrite(lvl3led1,LOW);
     digitalWrite(lvl1led2,LOW);
     digitalWrite(lvl2led2,LOW);
     digitalWrite(lvl3led2,LOW);
   } // made own reset button so void setup wouldn’t have to run again.
// resets the game instantly
   
 }

Credits

lma3
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.