#include <Arduino.h>
#include <Servo.h>
/*Nakul Verma student # 12345
Midterm prep Exercise 2*/
Servo turny;
//setup a bool for exercises 2 and 3
bool check = LOW;
void setup() {
//declare all the pushbutton inputs
pinMode(2,INPUT);
pinMode(12,INPUT);
pinMode(8,INPUT);
pinMode(7,INPUT);
//declare all the LED output circuits
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
//designate pin 11 as the servo PWM pin
turny.attach(11);
}
void loop() {
//start by turning off all the LEDs
//LED circuits are active-low and will turn of with logic HIGH
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
//Exercise 1 blink all the LEDs while button A is pressed
while(digitalRead(2) == HIGH)
{
//turn on all the LEDs
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
//keep the LEDs on for 150 ms
delay(150);
//turn off all the LEDs for 180 ms
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
//keep the LEDs off for 180 ms
delay(180);
}
//Exercises 2 and 3: turn on all the LEDs with button B
//set the check boolean to logic high if button B is pressed
if (digitalRead(12) == HIGH)
{
check = HIGH;
}
//set the check boolean to logic low if button C is pressed
if (digitalRead(8) == HIGH)
{
check = LOW;
}
//toggle the LEDs on/off based on whether the boolean is logic high/low, respectively
if(check==HIGH)
{
//turn on all the LEDs
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
}
if(check==LOW)
{
//turn off all the LEDs
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
}
//exercise 4; move the servo back and forth between 45° and 135° while button D is pressed
while(digitalRead(7)){
turny.write(45);
delay(300);
turny.write(135);
delay(300);
}
}