#include <Servo.h>
#include <time.h>
Servo Servo1;//setting up the class for servo
int const button_a = 2, button_b = 12, button_c = 8, button_d = 7;//using int value to determine the pins
int const amber_led = 5, blue_led = 6, green_led = 10, red_led = 9;
int control = 1;//initiating the global variable for default behaviour on or off
//millis() geting current time
void geoDelay(unsigned long t){//making delay function by using millis(arduino running time)
unsigned long initial = millis();//unasigned long because it starts fro 0 and can store incredibly large number
while ((millis() - initial) <= t){//keeping the loop on (nothing inside) to keep the time on hold
}
initial = millis();
return;
}
void buttonA(){//function called for the interupt function or when button A is pressed looking from hardware behaviour
//turning off all the LEDs
digitalWrite(amber_led, HIGH);
digitalWrite(blue_led, HIGH);
digitalWrite(green_led, HIGH);
digitalWrite(red_led, HIGH);
Servo1.write(145);//chaning the servo to 145 degree
if(control == 0)//changing the value of control to turn the default behaviour off if the state is on
control++;
else {
control = 0;//chainging the value of control to turn the default behaviour on if the state is off
}
return;
}
void setup() {
//Setting default pin value to be high (led off)
digitalWrite(amber_led, HIGH);
digitalWrite(blue_led, HIGH);
digitalWrite(green_led, HIGH);
digitalWrite(red_led, HIGH);
//Setting up the pin for usage input and output (buttons and led respectively)
pinMode(button_a, INPUT);
pinMode(button_b, INPUT);
pinMode(button_c, INPUT);
pinMode(button_d, INPUT);
pinMode(amber_led, OUTPUT);
pinMode(blue_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(red_led, OUTPUT);
Servo1.attach(11);//Setting up connection of servo at pin 11
attachInterrupt(digitalPinToInterrupt(button_a), buttonA, RISING);//interupt function as a priority to the program for when button A is pressed
}
void alternateBlink (int led1, int led2, int n){//alternate function with parameter of which led and how many times it will blink
for(int i = 0; i < n; i++){
digitalWrite(led1, LOW);//turning on first choice
geoDelay(100);//giving it time of .1s
digitalWrite(led1, HIGH);//turning off the first choice
geoDelay(100);//giving it time of .1s
digitalWrite(led2, LOW);//turning on the second choice
geoDelay(100);//giving it time of .1s
digitalWrite(led2, HIGH);//turning off the second choice
}
}
void blink(int led, int n){//function to blink the led of choice and how many times it will blink
for(int i = 0; i < n; i++){//with format blink(led number, number of blinks)
digitalWrite(led, LOW);//turning on the led
geoDelay(100);//wait for .1s
digitalWrite(led, HIGH);//turning off the led
geoDelay(100);//wait for .1s
}
}
void loop() {
if(control == 0){//default behaviour by blinking the amber with green alternately 5 times then blink alternately 5 times blue and red leds
for(int i = 0; i < 5; i++){//blinking the light 5 times between amber and green
if(control == 0)//chekcing the condition if other button has been pressed or not
blink(amber_led,1);//blink once for the selected led
if(control == 0)//chekcing the condition if other button has been pressed or not
blink(green_led,1);//blink once for the selected led
if(digitalRead(button_b) == HIGH || digitalRead(button_c) == HIGH || digitalRead(button_d) == HIGH){
control++;//break the default behaviour and also breaking the loop to save resource
break;
}
}
for(int i = 0; i < 5; i++){//blinking the light 5 times between red and blue
if(control == 0)//chekcing the condition if other button has been pressed or not
blink(blue_led,1);//blink once for the selected led
if(control == 0)//chekcing the condition if other button has been pressed or not
blink(red_led,1);//blink once for the selected led
if(digitalRead(button_b) == HIGH || digitalRead(button_c) == HIGH || digitalRead(button_d) == HIGH){
control++;//break the default behaviour and also breaking the loop to save resource
break;
}
}
}
if(digitalRead(button_b) == HIGH){
while (true){
for(int i = 10; i <= 120; i++){//moving the servo slowly from 10 to 120 degree
Servo1.write(i);
geoDelay(8);//delay for smooth movement
if(digitalRead(button_b) == LOW)//break if the button is released
return;
}
for(int i = 120; i >= 10; i--){//moving the servo slowly from 120 to 10 degree
Servo1.write(i);
geoDelay(8);//delay for smooth movement
if(digitalRead(button_b) == LOW)//break if the button is released
return;
}
if(digitalRead(button_b) == LOW)//break if the button is released
return;
}
}
if(digitalRead(button_c) == HIGH){//using blink function "blink('led pin', 'how many times it blink')"
//blinking each led twice with the order green, red, blue, amber and also in reverse
if(digitalRead(button_c) == HIGH)//skip if the button is released
blink(green_led,2);//blink twice for the selected led
if(digitalRead(button_c) == HIGH)//skip if the button is released
blink(red_led,2);//blink twice for the selected led
if(digitalRead(button_c) == HIGH)//skip if the button is released
blink(blue_led,2);//blink twice for the selected led
if(digitalRead(button_c) == HIGH)//skip if the button is released
blink(amber_led,2);//blink twice for the selected led
if(digitalRead(button_c) == HIGH)//skip if the button is released
blink(blue_led,2);//blink twice for the selected led
if(digitalRead(button_c) == HIGH)//skip if the button is released
blink(red_led,2);//blink twice for the selected led
if(digitalRead(button_c) == HIGH)//skip if the button is released
blink(green_led,2);//blink twice for the selected led
}
if(digitalRead(button_d) == HIGH){
//randomly choose one of the actions below when button d is pressed
if(digitalRead(button_d) == HIGH && millis() % 3 == 0){
Servo1.write(30);//change servo angle to 30 degree
blink(green_led,2);//using blink function to blink the green led twice
}
if(digitalRead(button_d) == HIGH && millis() % 3 == 1){
Servo1.write(90);//change servo angle to 90 degree
alternateBlink(red_led, blue_led, 3);//use the alternate blink to blink the led 3 times alternately (red and blue)
}
if(digitalRead(button_d) == HIGH && millis() % 3 == 2){
Servo1.write(150);//change servo angle to 150 degree
blink(amber_led,4);//using blink function to blink the amber led 4 times
}
}
}