//Medya Majidipanah 300394363
#include <Servo.h>
#include <Arduino.h>
Servo bubble; // the servo motor name
//declaring my constants(led pins and buttons)
const int button_a = 2, button_d = 7, button_c = 8, button_b = 12;
const int amber_led = 5, blue_led = 6, red_led = 9, green_led = 10;
//declaring variables
long randLed; // this is for our randomn function
//this function is for making the led pins blink without writing the whole process constantly
//for this function we can put the pin number and the amount of time we want it to blink
//the blinker() will make the selected led to blink the selected amount of time (led, rep)
void blinker(int LED, int reps){
for (int i = 0; i <= reps; i++)
{
digitalWrite(LED,LOW); // sets the digital pin LED off
delay(150); // waits for 150 ms
digitalWrite(LED,HIGH); // sets the digit pin LED on
delay(150); // waits for 150 ms
}
}
void setup() {
// put your setup code here, to run once:
// setting the digital pins as output for the LEDs
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// setting the servo motor that it is connected to digital pin 11
bubble.attach(11);
// setting the digital pins as input for the push buttons
pinMode(2, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(12, INPUT);
//since we don't have a connected digita pin 0
//the analog noise will make the randomseed() to generate diff seed numbers
// randomseed()will then start shuffling
randomSeed(analogRead(0));
}
void loop() {
// put your main code here, to run repeatedly:
// in our void loop before all of our while loops we put this digitalwrite() for
// all the LED pins so the are turned off if a button is not pressed
//and the motor servo is at 0 degrees position
digitalWrite(amber_led,HIGH);
digitalWrite(blue_led,HIGH);
digitalWrite(red_led,HIGH);
digitalWrite(green_led,HIGH);
bubble.write(0);
//This while loop is for when Button A is pressed
//button A = yellow button
//BACK AND FORTH LED TRACER
//for creating this task , we need the blinker() function
//the blinker() will make the selected led to blink the selected amount of time (led, rep)
// since we have our blinker function , we don't have to write digital write repetedly
// or the delay function , because it's already in our blinker function
while(digitalRead(2))
{
blinker(green_led,2); //the blinker function is gonna make the green led blink twice
blinker(red_led,2);//the blinker function is gonna make the red led blink twice
blinker(blue_led,2);//the blinker function is gonna make the blue led blink twice
blinker(amber_led,2);//the blinker function is gonna make the amber led blink twice
//here one round is done and the reverse starts but we dont put the
//amber/orange led cuz it already blinked and if we write it twice it gonna blink 4 times
blinker(blue_led,2);//the blinker function is gonna make the blue led blink twice
blinker(red_led,2);//the blinker function is gonna make the red led blink twice
//here if we write blinker function for the green LED , then it's going to blink twice
// and then twice again when the loop restarts , so we don't write it here
}
//This while loop is for when Button B is pressed
//button B = green button
//RANDOM LED BLINKER
//for creating this task , we need the blinker() function and we also use if statements
//the blinker() will make the selected led to blink the selected amount of time (led, rep)
//we don't need to include delay()in our if statements,
//since the blinker function has the delay() written in.
while(digitalRead(12))
{
//picke a random number between 5 to 10 (which is the range for the led digital pins)
randLed = random(5,11);
//after the random number/digtial pin has been picked our blinker function
//is set to work based on that number
//but since we only have 4 digital pin connected , so incase it will generate a number that
//does not have a pin connected to , for it to not cause a delay , we need to create an if
//statement , so it will only blink for a selected digital pins
if (randLed == 5) // if the random number is 5 the amber led will blink twice
{
blinker(randLed,2);
//delay(150);
}
else if (randLed == 6) // if the random number is 6 the blue led will blink twice
{
blinker(randLed,2);
//delay(150);
}
else if (randLed == 9) // if the random number is 9 the red led will blink twice
{
blinker(randLed,2);
//delay(150);
}
else if (randLed == 10) // if the random number is 10 the green led will blink twice
{
blinker(randLed,2);
//delay(150);
}
}
//This while loop is for when Button C is pressed
//button C = red button
//SERVO OSCILLATOR
//for creating this task , we need two for loops make one to rotate the servo forward ,
//and one to rotate it backward .
//we use bubble.write() and delay() inside each for loop
// the for loop will also makes the servo to momentarily stop when it moves one degree
//inside the for loop the bubble.write() is that for the selected degree, delay()wait 8 ms
//so when it moves one degree it will wait for 8 ms
while(digitalRead(8))
{
// this for loop will rotate the servo from 20 degrees to 120 degrees and moves forward
for (int j = 20; j <=120; j++) // the j++ is so it will add one degree to the previous position
{
bubble.write(j); // at this position
delay(8); //wait for 8 millisecond then move to next position
}
// this for loop will rotate the servo from 120 degrees to 20 degrees and moves backward
for(int j = 120; j <= 20; j--) // the j-- is so it will deduct one degree from the previous position
{
bubble.write(j); // at this position
delay(8); //wait for 8 millisecond then move to next position
}
}
//This while loop is for when Button D is pressed
//button D = blue button
//SYNCHRONIZED DANCE
//for creating this task , we need the blinker(), bubble.write() and the delay() function
//the blinker() will make the selected led to blink the selected amount of time (led, rep)
//the bubble.write() will rotate the servo motor to the selected posiotion in degrees , (degree)
//the delay() will wait for a selected amount of time in millisecond (time), which we
//only need to use before our buuble.write() function , since the blinker function has the delay() written in.
while(digitalRead(7))
{
bubble.write(30); //the bubble.write() will move the motr servo to 30 degrees
blinker(blue_led,2); //the blinker function is gonna make the blue led blink twice
delay(150); //wait for 150 ms then start the next function
bubble.write(90); //the bubble.write() will move the motr servo to 90 degrees
blinker(green_led,3); //the blinker function is gonna make the green led blink three times
delay(150); //wait for 150 ms then start the next function
bubble.write(120); //the bubble.write() will move the motr servo to 120 degrees
blinker(amber_led,1); //the blinker function is gonna make the amber led blink once
blinker(red_led,1); //the blinker function is gonna make the red led blink once
blinker(amber_led,1); //the blinker function is gonna make the amber led blink once
blinker(red_led,1); // the blinker function is gonna make the red led blink once
delay(150); //wait for 150 ms then start the next function
}
}