// John Aguila, Student #: 300333218
#include <Servo.h> // Servo Library
/*
Pushbutton circuits that generate a logic high are connected to 5 volts
2 - Button A (Red)
12 - Button B (Green)
8 - Button C (Yellow)
7 - Button D (Blue)
LED
5 - Amber Colour LED
6 - Blue Colour LED
10 - Green Colour LED
9 - Red Colour LED
*/
//Servo Function
int servoPin = 11; // Declared the pin the SERVO is in
Servo turn; // Name of the function for SURVO
unsigned long periodOne = 1000;
unsigned long period = 2000; // value event interval to replace delay
unsigned long previousTime = 0; // value for event interval to help with delay
unsigned long currentTime = millis(); // Initializing the millis function to a said variable
//unsigned long time = 0;
void setup() {
Serial.begin(115200); // Serial.begin for serial monitor and 115200 is the speed of the print "bps"
Serial.println("This is the Serial Monitor!"); // Prints the statement inside the brackets
Serial.println("---------------------------------"); // Prints the statement inside the brackets
//Set up all the digital pins on the push buttons
pinMode(2, INPUT); // Button A (red) push button
pinMode(12, INPUT); // Button B (green) push button
pinMode(8, INPUT); // Button C (yellow) push button
pinMode(7, INPUT); // Button D (blue) push button
//set up all the digital output pins to LED Circuits
pinMode(5, OUTPUT); // Amber colour LED
pinMode(6, OUTPUT); // Blue colour LED
pinMode(10, OUTPUT); // Green Colour LED
pinMode(9, OUTPUT); // Red Colour LED
pinMode(11, OUTPUT); // SERVO
turn.attach(servoPin); // Servo pin attached to #11 + The name of the function
}
/*
void rep(){ //Brand new Delay Function
const unsigned long period = 1000; // Constant value event interval to replace delay
const unsigned long previousTime = 0; // Constant value for event interval to help with delay
unsigned long currentTime = millis(); // Assigning current time to millis function
if (currentTime - previousTime >= period) { //Conditions for repitition similar to delay
previousTime = currentTime; // Updates the time to continuesly repeat the action
}
}
*/
/*
void show(){ // Shows the time in milliseconds on the screen
while (true){ // infinite loop
Serial.println( millis ()); // Displays the time in milliseconds of when the Artduino is Turned on
}
}
*/
void loop() {
//turn off all of the LED, High turns off the LED
digitalWrite(5, HIGH); // Turns off Amber LED
digitalWrite(6, HIGH); // Turn off Blue LED
digitalWrite(10, HIGH); // Turn off Green LED
digitalWrite(9, HIGH); // Turn off Red LED
/*
//When pressing the RED button, the RED LED blinks
while (digitalRead(2)){ // 2 Is the RED button (A)
Serial.println("Red Button Pressed"); // When RED Button is pressed, Serial Monitor Prints Statement
digitalWrite(9, LOW); // turn on RED led for 200
delay(200);
//turn off the RED led and keep it off for 150
digitalWrite(9, HIGH); // turns off the RED led once button not pressed
delay(150);
}
*/
// When pressing the BLUE button, the light flashes once clicked
while (digitalRead(7)){
Serial.println("Blue Button Pressed"); // When BLUE Button is pressed, Serial Monitor Prints Statement
digitalWrite(6, LOW); // Turns on the BLUE LED
delay(200);
digitalWrite(6, HIGH); // Turns off the BLUE LED
delay(200);
}
//While pressing the GREEN button, GREEN Led Blinks
while (digitalRead(12)){ // while loop for the button and LED
Serial.println("Green Button Pressed"); // When GREEN Button is pressed, Serial Monitor Prints Statement
digitalWrite(10, LOW); // Turns on the GREEN LED
delay(200);
digitalWrite(10, HIGH); // Turns off the GREEN LED
delay(200);
}
//While pressing the YELLOW button, YELLOW light turns on
while (digitalRead(8)){
Serial.println("Yellow Button Pressed"); // When YELLOW Button is pressed, Serial Monitor Prints Statement
digitalWrite(5, LOW); // Turns on the the YELLOW LED
delay(200);
digitalWrite(5, HIGH); // Turns off the YELLOW LED
delay(200);
}
//Practice: Make the lever thing oscillate between 5 and 95 degrees
while (digitalRead(2)){
while (true){
/*
if (millis() >= previousTime + periodOne){
previousTime += periodOne;
turn.write(5);
Serial.println("1");
} */
/*
if (millis() >= previousTime + period){
previousTime += period;
turn.write(95);
Serial.println("1");
} */
//if (millis() - previousTime >= period) {
/* Event code */
//Serial.println("Ice Ice Baby");
/* Update the timing for the next time around */
//previousTime = currentTime;
//}
/*
if (millis() - previousTime >= period){
previousTime -= period;
turn.write(95);
Serial.println("2");
}*/
if (millis()/1000 - previousTime > 1) {
period = millis(); //setting "timer = millis" makes millis-timer = 0
Serial.println("1");
turn.write(5);
}
if (millis()/1000 - previousTime > 2) {
Serial.println("2");
turn.write(95);
}
}
}
} // End of loop Bracket