/* 11/21/16 This is an adaption of the RobotGeek sketch "Contolling a Solenoid with Arduino" and the RobotGeek LCD sketch "Hello World" http://learn.robotgeek.com/demo-code/124-arduino-solenoid-tutorial-button-demo.html http://learn.robotgeek.com/getting-started/30-dev-kits/57-robotgeek-i2c-lcd-getting-started-guide.html#examples */ #include #include RobotGeekLCD lcd; // constants won't change. They're used here to set pin numbers: const int button0Pin = 2; // left flipper button const int button1Pin = 3; // right flipper button const int button2Pin = 4; // shooter flipper button const int trigger1Pin = 0; // left target trigger analog pin 0 const int trigger2Pin = 1; // center left target trigger analog pin 1 const int trigger3Pin = 2; // cener right target trigger analog pin 2 const int trigger4Pin = 3; // right target trigger analog pin 3 const int buttonResetPin = 5; // LCD reset button const int relayBellPin = 6; // bell solenoid const int relay0Pin = 7; // left flipper solenoid const int relay1Pin = 8; // right flipper solenoid const int relay2Pin = 9; // shooter solenoid const int relay3Pin = 10; // left target solenoid const int relay4Pin = 11; // center left target solenoid const int relay5Pin = 12; // center right target solenoid const int relay6Pin = 13; // right target solenoid // variables will change: int button0State = 0; int button1State = 0; // variable for reading the pushbutton status int button2State = 0; int buttonResetState = 0; int gameScore = 0; int trigger1Value = 1000; int trigger2Value = 1000; int trigger3Value = 1000; int trigger4Value = 1000; int threshold = 800; int delayTimeIn = 1000; int delayTimeOut = 100; void setup() { lcd.init(); lcd.print(" Game Score:"); // Serial.begin(9600); // initialize the pushbutton pin as an input: pinMode(button0Pin, INPUT); pinMode(button1Pin, INPUT); pinMode(button2Pin, INPUT); pinMode(buttonResetPin, INPUT); // initialize the relay pin as an output: pinMode(relay0Pin, OUTPUT); pinMode(relay1Pin, OUTPUT); pinMode(relay2Pin, OUTPUT); pinMode(relay3Pin, OUTPUT); pinMode(relay4Pin, OUTPUT); pinMode(relay5Pin, OUTPUT); pinMode(relay6Pin, OUTPUT); pinMode(relayBellPin, OUTPUT); digitalWrite(relay0Pin, LOW); digitalWrite(relay1Pin, LOW); digitalWrite(relay2Pin, LOW); digitalWrite(relay3Pin, LOW); digitalWrite(relay4Pin, LOW); digitalWrite(relay5Pin, LOW); digitalWrite(relay6Pin, LOW); digitalWrite(relayBellPin, LOW); } void loop(){ // read the state of the pushbutton values: button0State = digitalRead(button0Pin); button1State = digitalRead(button1Pin); button2State = digitalRead(button2Pin); buttonResetState = digitalRead(buttonResetPin); trigger1Value = analogRead(trigger1Pin); trigger2Value = analogRead(trigger2Pin); trigger3Value = analogRead(trigger3Pin); trigger4Value = analogRead(trigger4Pin); // Serial.println(trigger4Value); // check if the pushbutton0 is pressed. // if it is we turn on the relay/solenoid if (button0State == HIGH) { // turn relay on: digitalWrite(relay0Pin, HIGH); } // When we let go of the button, turn off the relay else if ((button0State == LOW) && (digitalRead(relay0Pin) == HIGH)) { // turn relay off digitalWrite(relay0Pin, LOW); } // check if the pushbutton1 is pressed. // if it is we turn on the relay/solenoid if (button1State == HIGH) { // turn relay on: digitalWrite(relay1Pin, HIGH); } // When we let go of the button, turn off the relay else if ((button1State == LOW) && (digitalRead(relay1Pin) == HIGH)) { // turn relay off digitalWrite(relay1Pin, LOW); } // check if the pushbutton2 is pressed. // if it is we turn on the relay/solenoid if (button2State == HIGH) { // turn relay on: digitalWrite(relay2Pin, HIGH); } // When we let go of the button, turn off the relay else if ((button2State == LOW) && (digitalRead(relay2Pin) == HIGH)) { // turn relay off digitalWrite(relay2Pin, LOW); } // check if the left target light beam is broken // if it is we turn on the relay/solenoid if (trigger1Value < threshold) { delay(delayTimeIn); trigger1Value = analogRead(trigger1Pin); // test to see if ball is really resting in the target if (trigger1Value < threshold ) { // turn relay on: digitalWrite(relay3Pin, HIGH); // ring the bell digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); delay(delayTimeOut); digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); // turn the relay off digitalWrite(relay3Pin, LOW); gameScore = gameScore + 50; lcd.setCursor(0,1); lcd.print(" "); lcd.print(gameScore); } } // check if the center left target light beam is broken // if it is we turn on the relay/solenoid if (trigger2Value < threshold) { delay(delayTimeIn); trigger2Value = analogRead(trigger2Pin); // test to see if ball is really resting in the target if (trigger2Value < threshold ) { // turn relay on: digitalWrite(relay4Pin, HIGH); // ring the bell digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); delay(delayTimeOut); digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); delay(delayTimeOut); digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); // turn the relay off digitalWrite(relay4Pin, LOW); gameScore = gameScore + 100; lcd.setCursor(0,1); lcd.print(" "); lcd.print(gameScore); } } // check if the center right target light beam is broken // if it is we turn on the relay/solenoid if (trigger3Value < threshold) { delay(delayTimeIn); trigger3Value = analogRead(trigger3Pin); // test to see if ball is really resting in the target if (trigger3Value < threshold ) { // turn relay on: digitalWrite(relay5Pin, HIGH); // ring the bell digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); delay(delayTimeOut); //turn off relay digitalWrite(relay5Pin, LOW); gameScore = gameScore + 10; lcd.setCursor(0,1); lcd.print(" "); lcd.print(gameScore); } } // check if the right target light beam is broken // if it is we turn on the relay/solenoid if (trigger4Value < threshold) { delay(delayTimeIn); trigger4Value = analogRead(trigger4Pin); // test to see if ball is really resting in the target if (trigger4Value < threshold ) { // turn relay on: digitalWrite(relay6Pin, HIGH); // ring the bell digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); delay(delayTimeOut); digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); delay(delayTimeOut); digitalWrite(relayBellPin, HIGH); delay(delayTimeOut); digitalWrite(relayBellPin, LOW); // turn off relay digitalWrite(relay6Pin, LOW); gameScore = gameScore + 100; lcd.setCursor(0,1); lcd.print(" "); lcd.print(gameScore); } } if (buttonResetState == HIGH) { lcd.setCursor(0,1); gameScore = 0; lcd.print(" 0 "); } }