In: Computer Science
You have a pinball machine with four switches on the playboard. When a ball rolls over one of the switches that action must trigger the Arduino to add 20 points to the score being displayed on a 16x4 LCD, and actuate a relay that will trigger a bumping solenoid elsewhere on the board. The solenoid should return to home after the switch is no longer depressed by the ball.
Hint: treat the roll over switches like limit switches.
Arduino program for above problem:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define sw1 2
#define sw2 3
#define sw3 4
#define sw4 5
#define so1 6
//variable to store points of game
int points = 0;
void setup() {
//setting pinmode of switches
pinMode(sw1, INPUT);
pinMode(sw2, INPUT);
pinMode(sw3, INPUT);
pinMode(sw4, INPUT);
//seting pinmode of solenoid
pinMode(so1, OUTPUT)
//Setting the baud rate
Serial.begin(9600);
// setting column and row no.s in LCD
lcd.begin(16, 4);
//setting staring position in 16 X 4 lcd
lcd.setCursor(1, 2)
//printing initial score
lcd.print("score: 0");
}
//Lets assume we have pull up switches
//that means arduino will read high when switch is pressed
void loop() {
//if any switch is pressed by the pinball
if(digitalRead(sw1) == HIGH || digitalRead(sw2) == HIGH || digitalRead(sw3) == HIGH || digitalRead(sw4) == HIGH)
{
//increment the points by 20
points += 20;
//trigger a bumping solenoid, so1
digitalWrite(so1, HIGH);
//clear the lcd first
lcd.clear();
//show the points on lcd
lcd.print("Score: " + String(sol));
}
//if none of the switches is pressed
else{
//off the solenoid so that it comes abck to home, so1
digitalWrite(so1, HIGH);
}
}
if it helps you, do upvote as it motivates us a lot!