We have to make a (basic) game for a school project using the Arduino UNO.
The core idea is:
- A LED turns on
- You have n (delayLED) amount of time to react and press the according button
- If you press the wrong button, or dont react at all, it subtracts a life. (You have a total of 5)
With the current code, it only gives the user a millisecond to react, and subtracts the life instantly.
NOTE: We are all new at programming, only had it for a week. (Which is why the idea is so basic)
(If i posted in the wrong thread or did in a wrong way, I apologize)
We have the following code, though I left out the code for other LED colors, since it will be identical:
//score
int ledPointOne = A0;
int ledPointTwo = A1;
int ledPointThree = A2;
int ledPointFour = A3;
int ledPointFive = A4;
int score = 5;
//tilfældigt nummer
int randNumber = 0;
//delay
int delayLED = 3000;
//knapper
int buttonStateRed = 0;
int buttonStateGreen = 0;
int buttonStateYellow = 0;
int buttonStateWhite = 0;
const int buttonRed = 10;
const int buttonGreen = 11;
const int buttonYellow = 12;
const int buttonWhite = 13;
//Farver
const int redLED = 4;
const int yellowLED = 5;
const int greenLED = 6;
const int whiteLED = 7;
//millis
unsigned long Starttimer;
unsigned long Endtimer;
unsigned long Present;
//lyd
#include "pitches.h"
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
// put your setup code here, to run once:
//score
pinMode(ledPointOne, OUTPUT);
pinMode(ledPointTwo, OUTPUT);
pinMode(ledPointThree, OUTPUT);
pinMode(ledPointFour, OUTPUT);
pinMode(ledPointFive, OUTPUT);
//farver
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(whiteLED, OUTPUT);
//knapper
pinMode(buttonRed, INPUT);
pinMode(buttonGreen, INPUT);
pinMode(buttonYellow, INPUT);
pinMode(buttonWhite, INPUT);
//random
Serial.begin(9600);
randomSeed(analogRead(6));
//lyd
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}
void loop() {
// put your main code here, to run repeatedly:
if(redLED == HIGH || greenLED == HIGH || yellowLED ==HIGH || whiteLED == HIGH){
//start millis
Present = millis();
//Generer tilfædligt nummer
Serial.println(randNumber);
randNumber = random(1, 5);
if(randNumber == 1) {
//Start millis timer
Starttimer = Present;
//Tænd LED
digitalWrite(redLED, HIGH);
//Læs knappens tilstand
buttonStateRed = digitalRead(buttonRed);
if(Present-Starttimer > delayLED) {
if(buttonStateRed == HIGH) {
//Gør ingenting
//sluk LED
digitalWrite(redLED, LOW);
}
else{
//træk liv fra
score = score -1;
}
}
}
}
//score system
if(score == 0)
{
digitalWrite(ledPointOne, LOW);
digitalWrite(ledPointTwo, LOW);
digitalWrite(ledPointThree, LOW);
digitalWrite(ledPointFour, LOW);
digitalWrite(ledPointFive, LOW);
//Spil melodi
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);}
delay(2000);
score=score +5;
}
if(score == 1)
{
digitalWrite(ledPointOne, HIGH);
digitalWrite(ledPointTwo, LOW);
digitalWrite(ledPointThree, LOW);
digitalWrite(ledPointFour, LOW);
digitalWrite(ledPointFive, LOW);
}
if(score == 2)
{
digitalWrite(ledPointOne, HIGH);
digitalWrite(ledPointTwo, HIGH);
digitalWrite(ledPointThree, LOW);
digitalWrite(ledPointFour, LOW);
digitalWrite(ledPointFive, LOW);
}
if(score == 3)
{
digitalWrite(ledPointOne, HIGH);
digitalWrite(ledPointTwo, HIGH);
digitalWrite(ledPointThree, HIGH);
digitalWrite(ledPointFour, LOW);
digitalWrite(ledPointFive, LOW);
}
if(score == 4)
{
digitalWrite(ledPointOne, HIGH);
digitalWrite(ledPointTwo, HIGH);
digitalWrite(ledPointThree, HIGH);
digitalWrite(ledPointFour, HIGH);
digitalWrite(ledPointFive, LOW);
}
if(score == 5)
{
digitalWrite(ledPointOne, HIGH);
digitalWrite(ledPointTwo, HIGH);
digitalWrite(ledPointThree, HIGH);
digitalWrite(ledPointFour, HIGH);
digitalWrite(ledPointFive, HIGH);
}
}