/*
Nathan Lodder 300 420 912
ENGR 1190 Final
All LEDs are attached to the 5V pin on their positive leg so that by default the have current running through them and will shine while the 5V pin on the other side is on logic 'low',
driving <5V into the circuit creating an electrical potential.
Amber LED attached to pin 5
Blue LED attached to pin 6
Red LED attached to pin 9
Green LED attached to pin 10
Servo input line is attached to pin 11
Pushbuttons that when pressed generate a logic 'high' or '1' will be assigned as inputs to the following pins
button A (green) is connected to pin 2
button B (red) is connected to pin 12
button C (blue) is connected to pin 8
button D (yellow) is connected to pin 7
*/
#include <Servo.h>
//assigning servo1 the qualities of a Servo device
Servo servo1;
//Initiate an integer variable 'servo' and assigning it a value of '11' so that i can type servo instead of 11 every time I refer to the servo pin, this is for ease while coding
int servo = 11;
//Initiate 4 int variables that correspond to the 4 differently coloured LEDs, and assign them the value of the arduino pin that they are respectively connected to
int ALED = 5;
int BLED = 6;
int RLED = 9;
int GLED = 10;
//Initiate 4 int variables that correspond to the 4 push buttons, and assign them the value of the arduino pin that they are respectively connected to
int button_a = 2;
int button_b = 12;
int button_c = 8;
int button_d = 7;
/*
unsigned long variables are 64 bit integer variables that are >= 0 they are used for millis(); because millis(); starts a timer at the beginning of a program and
counts, in milliseconds, for up to around 50 days after the program starts. This means that we need a long (64 bit for example) to store large time values
*/
//declare an usigned long variable: 'current_time' to store updated values of the current time since the program started running in milliseconds.
unsigned long current_time;
//VARIABLES FOR PUSHBUTTON A ---------------------------------------------------------------------------------------------------------------------------------------------------
//initiate an unsigned long variable: 'button_a_start' to store the time when the button was last checked for pressing
unsigned long button_a_start;
//initiate a volatile boolean variable: a_status and assign it a 'false' value. It must be volatile because it is used in the main function and in an Interrupt Service Routine.
//'Volatile' notifies the compiler that this boolean may be changed by things other than the program itself, in this case that is an external input of a push button
volatile boolean a_status = false;
//initiate a constant unsigned long variable: button_a_check to store the time (ms) to wait before checking if a button has been pressed
const unsigned long button_a_check = 500;
//VARIABLES FOR Amber LED -----------------------------------------------------------------------------------------------------------------------------------------------------
//inititae an unsigned long variable: 'amber_start' to store the time when the amber LED's status was last changed
unsigned long amber_start;
//initiate a constant unsigned long variable: amber_check to store the time (ms) to wait before changing LED status
const unsigned long amber_check = 1000;
//VARIABLES FOR Blue LED -----------------------------------------------------------------------------------------------------------------------------------------------------
//VARIABLES FOR Red LED -----------------------------------------------------------------------------------------------------------------------------------------------------
//VARIABLES FOR Green LED -----------------------------------------------------------------------------------------------------------------------------------------------------
//VARIABLES FOR Servo -----------------------------------------------------------------------------------------------------------------------------------------------------
//inititae an unsigned long variable: 'servo_start' to store the time when the servo last moved
unsigned long servo_start;
//initiate a const unsigned long variable: servo_check to store the time (in ms) that will elapse between each servo movement
const unsigned long servo_check = 1500;
//VARIABLES FOR PUSHBUTTON B -----------------------------------------------------------------------------------------------------------------------------------------------------
const unsigned long button_b_check = 1000;
unsigned long button_b_start;
int rand_LED;
//VARIABLES FOR PUSHBUTTON C ---------------------------------------------------------------------------------------------------------------------------------------------------
const unsigned long button_c_check = 1000;
unsigned long button_c_start;
//VARIABLES FOR PUSHBUTTON D ---------------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
// put your setup code here, to run once:
//designate the uno pins connected to all LED's as outputs
pinMode(ALED, OUTPUT);
pinMode(BLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(RLED, OUTPUT);
servo1.attach(11);
//designate the uno pins connected to all pushbutton's as inputs
pinMode(button_a, INPUT);
attachInterrupt(digitalPinToInterrupt(button_a), ISR_button_a, RISING);
pinMode(button_b, INPUT);
pinMode(button_c, INPUT);
pinMode(button_d, INPUT);
//Set the baud rate of the serial data to 9600 bits/second which is the standard for the arduino
Serial.begin(9600);
/*
set all the pins connected to the LED's to a logic high, this will cause them to push 5V into
the circuit that has 5V being pushed from the other end as well. The potential difference across
each LED circuit will be 5V - 5V = 0V, and the LED's will not light up.
*/
digitalWrite(ALED, HIGH);
digitalWrite(BLED, HIGH);
digitalWrite(RLED, HIGH);
digitalWrite(GLED, HIGH);
//assign the value of the current time since the program begun to the variable 'button_s_start'
button_a_start = millis();
}
//initiate a function void of a return value: button_a_fn to manage button_a checks and the results of it's status
void button_a_fn(){
if(a_status){
digitalWrite(ALED, !digitalRead(ALED));
a_status=false;
}
}//curly for button_a_fn
// put your main code here, to run repeatedly:
void loop() {
//assign 'current_time' the value of millis()-the time since the program started to run
current_time = millis();
button_a_fn();
}//main curly
void ISR_button_a(){
a_status=true;
}