#define B1_PIN 13
#define B2_PIN 12
#include <Servo.h>
Servo arm;
float pos = 0.0;
float step = 1.0;
void setup()
{
pinMode(A1, INPUT_PULLUP); // Set the A1 pin to a pushbutton in pullup mode
pinMode(A2, INPUT_PULLUP); // Set the A1 pin to a pushbutton in pullup mode
arm.attach(2); // Attache the arm to the pin 2
arm.write(pos); // Initialize the arm's position to 0 (leftmost)
}
void loop()
{
if (!digitalRead(A1)) // Check for the yellow button input
{
if (pos>0) // Check that the position won't go lower than 0°
{
arm.write(pos); // Set the arm's position to "pos" value
pos-=step; // Decrement "pos" of "step" value
delay(5); // Wait 5ms for the arm to reach the position
}
}
if (!digitalRead(A2)) // Check for the blue button input
{
if (pos<180) // Check that the position won't go higher than 180°
{
arm.write(pos); // Set the arm's position to "pos" value
pos+=step; // Increment "pos" of "step" value
delay(5); // Wait 5ms for the arm to reach the position
}
}
}
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
pinMode(B1_PIN, INPUT_PULLUP);
pinMode(B2_PIN, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("You arrive home and");
lcd.setCursor(0, 1);
lcd.print("see the door ajar");
lcd.setCursor(0, 3);
lcd.print("Something is amiss");
delay(2000);
lcd.init();
lcd.setCursor(0, 0);
lcd.print("Do you:");
lcd.setCursor(0, 1);
lcd.print("1, Enter the house?");
lcd.setCursor(0, 2);
lcd.print("2, Call for help");
lcd.setCursor(0, 3);
lcd.print("Press button 1 or 2");
int waitingForInput = true;
int userInput = 0;
while (waitingForInput)
{
int b1State = digitalRead(B1_PIN);
int b2State = digitalRead(B2_PIN);
if (b1State == LOW)
{
waitingForInput = false;
userInput = 1;
}
if (b2State == LOW)
{
waitingForInput = false;
userInput = 2;
}
}
lcd.init();
if (userInput == 1)
{
lcd.setCursor(0, 0);
lcd.print("You went inside and");
lcd.setCursor(0, 1);
lcd.print("were eaten by a lion");
lcd.setCursor(0, 3);
lcd.print("Game Over !!!!");
}
else
{
lcd.setCursor(0, 0);
lcd.print("You call 000 and");
lcd.setCursor(0, 1);
lcd.print("speak to the police");
lcd.setCursor(0, 3);
lcd.print("They say to wait...");
delay(2000);
lcd.init();
lcd.setCursor(0, 0);
lcd.print("SCREAM!! Do you:");
lcd.setCursor(0, 1);
lcd.print("1, Wait for police?");
lcd.setCursor(0, 2);
lcd.print("2, Investigate scream");
lcd.setCursor(0, 3);
lcd.print("Press button 1 or 2");
waitingForInput = true;
userInput = 0;
while (waitingForInput)
{
int b1State = digitalRead(B1_PIN);
int b2State = digitalRead(B2_PIN);
if (b1State == LOW)
{
waitingForInput = false;
userInput = 1;
}
if (b2State == LOW)
{
waitingForInput = false;
userInput = 2;
}
}
}
lcd.init();
if (userInput == 1)
{
lcd.setCursor(0, 0);
lcd.print("You waited for police");
lcd.setCursor(0, 1);
lcd.print("You are safe, but a");
lcd.setCursor(0, 2);
lcd.print("lion ate your hamster");
lcd.setCursor(0, 3);
lcd.print("Game Over !!!!");
}
else
{
lcd.setCursor(0, 0);
lcd.print("You investigated and");
lcd.setCursor(0, 1);
lcd.print("everything is ok");
lcd.setCursor(0, 3);
lcd.print("Kerin ate you");
}
}
void loop() {
}