/* 10/8/16 This is an adaption of the RobotGeek sketch "Contolling a Solenoid with Arduino" and the RobotGeek LCD sketch "Hello World" Controlling a Solenoid with Arduino Products Used in this demo: - http://www.robotgeek.com/solenoids - http://www.robotgeek.com/robotgeek-geekduino-sensor-kit - http://www.robotgeek.com/robotGeek-pushbutton - http://www.robotgeek.com/robotgeek-relay */ #include #include #include RobotGeekLCD lcd; Servo spinServo; int spinServoPin = 3; // constants won't change. They're used here to set pin numbers: const int button1Pin = 1; // the number of the pushbutton1 pin const int button2Pin = 2; // the number of the pushbutton2 pin const int button3Pin = 4; const int button4Pin = 5; const int button5Pin = 6; const int buttonResetPin = 7; const int relay1Pin = 8; // the number of the Relay pin const int relay2Pin = 9; const int relay3Pin = 10; const int relay4Pin = 11; const int relay5Pin = 12; // variables will change: int button1State = 0; // variable for reading the pushbutton status int button2State = 0; int button3State = 0; int button4State = 0; int button5State = 0; int buttonResetState = 0; int gameScore = 0; void setup() { spinServo.attach(spinServoPin); spinServo.write(0); lcd.init(); lcd.print(" Game Score:"); // initialize the pushbutton pin as an input: pinMode(button1Pin, INPUT); pinMode(button2Pin, INPUT); pinMode(button3Pin, INPUT); pinMode(button4Pin, INPUT); pinMode(button5Pin, INPUT); pinMode(buttonResetPin, INPUT); // initialize the relay pin as an output: pinMode(relay1Pin, OUTPUT); pinMode(relay2Pin, OUTPUT); pinMode(relay3Pin, OUTPUT); pinMode(relay4Pin, OUTPUT); pinMode(relay5Pin, OUTPUT); } void loop(){ // read the state of the pushbutton values: button1State = digitalRead(button1Pin); button2State = digitalRead(button2Pin); button3State = digitalRead(button3Pin); button4State = digitalRead(button4Pin); button5State = digitalRead(button5Pin); buttonResetState = digitalRead(buttonResetPin); // 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); gameScore = gameScore + 10; lcd.setCursor(0,1); lcd.print(" "); lcd.print(gameScore); } // check if the pushbutton3 is pressed. // if it is we turn on the relay/solenoid if (button3State == HIGH) { // turn relay on: digitalWrite(relay3Pin, HIGH); } // When we let go of the button, turn off the relay else if ((button3State == LOW) && (digitalRead(relay3Pin) == HIGH)) { // turn relay off digitalWrite(relay3Pin, LOW); gameScore = gameScore + 10; lcd.setCursor(0,1); lcd.print(" "); lcd.print(gameScore); } // check if the pushbutton4 is pressed. // if it is we turn on the relay/solenoid if (button4State == HIGH) { // turn relay on: digitalWrite(relay4Pin, HIGH); } // When we let go of the button, turn off the relay else if ((button4State == LOW) && (digitalRead(relay4Pin) == HIGH)) { // turn relay off digitalWrite(relay4Pin, LOW); gameScore = gameScore + 50; lcd.setCursor(0,1); lcd.print(" "); lcd.print(gameScore); } // check if the pushbutton5 is pressed. // if it is we turn on the relay/solenoid if (button5State == HIGH) { // turn relay on: digitalWrite(relay5Pin, HIGH); } // When we let go of the button, turn off the relay else if ((button5State == LOW) && (digitalRead(relay5Pin) == HIGH)) { // turn relay off digitalWrite(relay5Pin, 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 "); } }