int pinA = 2; //this is used for displaying the numbers
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int counterpin = 9; //pullup resistor wire sensor
int redled = 10;
int buzzer = 11;
int winpin = 12; //pullup resistor win sensor
int greenled = 13;
int count; //declaring the variables
int buttonstate;
int dt = 300; //these are the delay variables
int cd = 300;
int ed = 400;
int wind = 300;
int counttone = 800; //these are the buzzer tones
int endtone = 1500;
int wintone = 300;
void setup() {
// put your setup code here, to run once:
pinMode(pinA, OUTPUT); //sets the initial number and count to 0
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode (counterpin, INPUT);
pinMode(redled,OUTPUT);
pinMode (buzzer, OUTPUT);
pinMode (winpin, INPUT);
pinMode (greenled,OUTPUT);
count = 0;
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate = digitalRead(counterpin);
if (buttonstate == 0) { //when the pullup resistor sees a drop in voltage the wires are touching
count ++;
tone(buzzer, counttone);
delay (cd);
noTone (buzzer);
}
Serial.print ( " the button state is "); //this displays some of the variables inside the serial moitor
Serial.print (buttonstate);
Serial.print (" the count is ");
Serial.println (count);
if (digitalRead(winpin) == 0){ //sets the loop for winning the game
digitalWrite (greenled,HIGH);
tone(buzzer, wintone);
delay (wind);
noTone(buzzer);
tone(buzzer, wintone);
delay(wind);
noTone(buzzer);
delay (wind);
tone(buzzer, wintone);
delay(wind);
noTone(buzzer);
digitalWrite (greenled, LOW);
count = 0; //resets the number and count to 0
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
}
if (count ==1){ //uses the variables to display numbers to the 7-digit display
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
delay(dt);
}
if (count ==2){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
delay(dt);
}
if (count == 3){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
delay (dt);
}
if (count == 4){
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
delay(dt);
}
if (count == 5){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
delay(dt);
}
if (count == 6){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
delay(dt);
}
if (count == 7 ){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
delay(dt);
}
if (count == 8 ){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
delay(dt);
}
if (count == 9){
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
delay(dt);
}
if(count == 10){ //the loop for losing
count = 0;
digitalWrite (redled, HIGH);
tone (buzzer, endtone);
delay(ed);
noTone (buzzer);
tone (buzzer, endtone);
delay(ed);
noTone (buzzer);
tone (buzzer, endtone);
delay(ed);
noTone (buzzer);
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
digitalWrite(redled,LOW);
}
delay (100);
}
Comments