/* This is the code for the Maker Camp Pachinko Game * To play the game, two players take turns launching their * Pachinko balls and whichever player makes it to 150 first wins. * The values of each ball hole can be changed along with the * winning score amount. This code borrows from the "HelloWorld" * and "Melody" tutorials on arduino.cc */ #include //IR pins (you can change these depending on which digital pins you use) #define IR1 10 #define IR2 7 #define IR3 9 #define IR4 11 #define IRL 8 #define LEDPIN A0 #define speakerPin 6 int P1 = 0; //player one score int P2 = 0; //player two score bool P1state = true; //player turn int IR1State = 0, IR1LastState=0; int IR2State = 0, IR2LastState=0; int IR3State = 0, IR3LastState=0; int IR4State = 0, IR4LastState=0; int IRLState = 0, IRLLastState=0; // initialize the library with the numbers of the interface pins LiquidCrystal lcd(13, 12, 5, 4, 3, 2); //this is the melody that will play when someone wins int length = 15; // the number of notes char notes[] = "ccggaagffeeddc "; // a space represents a rest int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; int tempo = 100; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.print(" PACHINKO "); lcd.setCursor(0, 1); //set up P1's score on display lcd.print("P1:" + P1); lcd.setCursor(8, 1); //set up P2's score on display lcd.print("P2:" +P2); pinMode(IR1, INPUT); digitalWrite(IR1, HIGH); pinMode(IR2, INPUT); digitalWrite(IR2, HIGH); pinMode(IR3, INPUT); digitalWrite(IR3, HIGH); pinMode(IR4, INPUT); digitalWrite(IR4, HIGH); pinMode(IRL, INPUT); digitalWrite(IRL, HIGH); pinMode(speakerPin, OUTPUT); pinMode(LEDPIN, OUTPUT); digitalWrite(LEDPIN, LOW); Serial.begin(9600); } void loop() { IR1State = digitalRead(IR1); IR2State = digitalRead(IR2); IR3State = digitalRead(IR3); IR4State = digitalRead(IR4); IRLState = digitalRead(IRL); if (IR1State && !IR1LastState) { Serial.println("IR1 Unbroken"); // Serial.println(IR1State); } if (!IR1State && IR1LastState) { Serial.println("IR1 Broken"); digitalWrite(speakerPin, HIGH); digitalWrite(LEDPIN, HIGH); delay(100); digitalWrite(speakerPin, LOW); digitalWrite(LEDPIN, LOW); if(P1state == true){ P1 = P1 + 10; Serial.print("P1 score is "); Serial.print(P1); Serial.println(); P1state = false; }else{ P2 = P2 + 10; Serial.print("P2 score is "); Serial.print(P2); Serial.println(); P1state = true; } } IR1LastState = IR1State; if (IR2State && !IR2LastState) { Serial.println("IR2 Unbroken"); } if (!IR2State && IR2LastState) { Serial.println("IR2 Broken"); digitalWrite(speakerPin, HIGH); digitalWrite(LEDPIN, HIGH); delay(100); digitalWrite(speakerPin, LOW); digitalWrite(LEDPIN, LOW); if(P1state == true){ P1 = P1 + 10; Serial.print("P1 score is "); Serial.print(P1); Serial.println(); P1state = false; }else{ P2 = P2 + 10; Serial.print("P2 score is "); Serial.print(P2); Serial.println(); P1state = true; } } IR2LastState = IR2State; if (IR3State && !IR3LastState) { Serial.println("IR3 Unbroken"); } if (!IR3State && IR3LastState) { Serial.println("IR3 Broken"); digitalWrite(speakerPin, HIGH); digitalWrite(LEDPIN, HIGH); delay(100); digitalWrite(speakerPin, LOW); digitalWrite(LEDPIN, LOW); if(P1state == true){ P1 = P1 + 20; Serial.print("P1 score is "); Serial.print(P1); Serial.println(); P1state = false; }else{ P2 = P2 + 20; Serial.print("P2 score is "); Serial.print(P2); Serial.println(); P1state = true; } } IR3LastState = IR3State; if (IR4State && !IR4LastState) { Serial.println("IR4 Unbroken"); } if (!IR4State && IR4LastState) { Serial.println("IR4 Broken"); digitalWrite(speakerPin, HIGH); digitalWrite(LEDPIN, HIGH); delay(100); digitalWrite(speakerPin, LOW); digitalWrite(LEDPIN, LOW); if(P1state == true){ P1 = P1 + 30; Serial.print("P1 score is "); Serial.print(P1); Serial.println(); P1state = false; }else{ P2 = P2 + 30; Serial.print("P2 score is "); Serial.print(P2); Serial.println(); P1state = true; } } IR4LastState = IR4State; if (!IRLState && IRLLastState) { Serial.println("IRL Broken"); if(P1state == true){ P1 = P1 + 0; Serial.print("P1 score is "); Serial.print(P1); Serial.println(); P1state = false; }else{ P2 = P2 + 0; Serial.print("P2 score is "); Serial.print(P2); Serial.println(); P1state = true; } } IRLLastState = IRLState; lcd.setCursor(4, 1); lcd.print(P1); lcd.setCursor(12, 1); lcd.print(P2); if(P1 >= 150){ lcd.setCursor(3, 0); lcd.print("GAME OVER"); digitalWrite(LEDPIN, HIGH); lcd.setCursor(12, 1); lcd.print(":( "); for (int i = 0; i < length; i++) { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } } if(P2 >= 150){ lcd.setCursor(3, 0); lcd.print("GAME OVER"); digitalWrite(LEDPIN, HIGH); lcd.setCursor(4, 1); lcd.print(":( "); for (int i = 0; i < length; i++) { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } } }