Monday, March 6, 2017

My Arduino Game
     "Stop that LED!"
Description of My Game
The object of my game is to stop the light on the blue LED. To play, press down on button and watch the lights go back and forth. Release your finger off of the button when you think the LED will stop on the blue. If it lands on any other LED but the blue, your score will be subtracted 10 points. If it lands on the blue LED, your score will be added 10 more points. Keep playing to get your score up to 50. It's very hard and will take a while, but it's fun when you have nothing to do!
Example of light not on the blue LED. This means the player will have 10 points subtracted from their score.
Example of light on the blue LED. This means the player will have 10 points added to their score. 

CODE
int ledpin = 13; //starts here
int ledpin2 = 12;
int ledpin3 = 11;
int ledpin4 = 10;
int ledpin5 = 9; //variables for when button is released, stops at this pin^for all of the above
int buttonPin = 2; //see if button is pressed
int buttonState = 0; //
int counter = 0; //count button (score)
int ledpinall = (9, 10, 11, 12, 13);
int count = 0; //declared variables ^

void setup() { //runs once
  Serial.begin(9600); //set up serial library at 9600 bps
  pinMode(buttonPin, INPUT); //initialize as input

} //WHEN BUTTON IS RELEASED, TRY TO LIGHT UP BLUE LED

void loop() { //runs over and over again
    buttonState = digitalRead(buttonPin); //check to see if button is pressed here
    if (buttonState == LOW)
    {
      Serial.println("Press button, then release and try to light up the blue LED!"); //prints "Button Released" on Serial Monitor
      {
        pinMode(ledpin, INPUT); //initialize ledpin as input
        digitalWrite(ledpin, LOW); //turn led off
        }
        if (ledpin!=10) //if ledpin does not land on 10, you lose
        {
          Serial.println ("Loser!"); //print "Loser!" when you do not land on 10
        }
        if (ledpin==10) //if ledpin does land on ten, you win
        {
          Serial.println("Winner!"); //print "Winner!" when you do land on 10
        }
        if (ledpin == 13) //if led pin is 13, then stay on after button is released, same for 4 statements below
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH);  //turn ledpin 13 on    
        }
        if (ledpin2 == 12) //if ledpin is 12, stay on
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH);      //turn ledpin 12 on
        }
        if (ledpin3 == 11) //if ledpin is 11, stay on
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH);    //turn ledpin11 on    
        }
        if (ledpin4 == 10) //if ledpin is 10, stay on after button is released
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH); //turn ledpin10 on      
        }
        if (ledpin5 == 9) //if ledpin is 9, stay on after button is released
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH); //turn ledpin9 on
        }
        delay(1000); //wait
    buttonState=digitalRead(buttonPin); //check to see if button is pressed here
    if (ledpin==10 && buttonState==HIGH) //if led pin is 10 and is on, then...
    {
      count=count+10; //add 10 to the score if you land on 10 (blue LED)
      Serial.print("Score="); //writes "Score=" to player's score
      Serial.println(count);
    }
    buttonState=digitalRead(buttonPin); //check to see if button is pressed here
    if (ledpin!=10 && buttonState==HIGH) //if led pin is anything but 10 and is on, then...
    {
      count=count-10; //subtract 1 from the score if you land on anything but 10
      Serial.print("Score="); //writes "score=" to player's score
      Serial.println(count);
    }
 
 
  delay(100); //delay for 100 ms
  buttonState = digitalRead(buttonPin); //check to see if button is pressed here
  if (buttonState == HIGH)
  { //starting of if statement
    Serial.println("Release button when you think your blue LED will light up!"); //prints button pressed with ending line break
    buttonState = digitalRead(buttonPin);
    while (ledpin >9 && buttonState == HIGH) //while led pin is greater than 9, and button is on, then...
   
    {
   
       buttonState = digitalRead(buttonPin);
      pinMode (ledpin, OUTPUT); //initialize as output
      digitalWrite(ledpin, HIGH); //turn led on  
      delay(100); //wait 100 ms
      digitalWrite(ledpin, LOW); //turn led off
      ledpin=ledpin-1; //subtract one from 9 to go down line
    } //end of if statement
  {
    Serial.println("Waiting"); //prints waiting to serial monitor
 
    while (ledpin <13 && buttonState == HIGH) //while ledpin is less than 13, and button is on, then...

    {
     buttonState = digitalRead(buttonPin);
     
      pinMode (ledpin, OUTPUT); //initialize as output
      digitalWrite(ledpin, HIGH); //turn led on
      delay(100); //wait 100 ms
      digitalWrite(ledpin, LOW); //turn led off
      ledpin=ledpin+1; //add to 13
    }
  }
//delay(1000);
  }
}

URL to Video
https://www.youtube.com/watch?v=sJk36eqXYGU&feature=youtu.be



No comments:

Post a Comment