// ------------
// ECE 210 - Lab 6
// ------------
/*-------------
Author: Giri Venkataramanan
This code is used in ECE 210 in Practicum 6.
The code demonstrates reading analog signals and writing PWM signals
Pin A0 is used to read potentiometer signal input (10 bits)
Pin D2 (red LED) and Pin D3 (green LED) are used to write signal outputs
Pin D3 is used to indicate ready state with blinking and blend state with solid green
Scaling by a factor of 4 is required because PWM uses only 8 bits
When you press the PB, the RGB LED sequences between
Ready(Blinking Green), SetDutyR (Set RGB redness),
SetDutyG (Set RGB greenness), SetDutyB (set Blueness), BlendRGB (mix RGB)
During the SetDutyX states, the Pot is used to set the
values for the corresponding items through the brightness of each color
Pin A0 to Pot read
Pins D9-D10-D are to resistors connected to RGB of common cathode LED
Pin D2 to PR positive logic (Red LED - will always be on default)
Pin D3 to PG negative logic (Green LED)
Pin D8 to Pb pushbutton (notice lower case because PB is reserved)
-------------*/
// constants won't change. They're used here to set pin numbers:
const int VL = 4; // the number of the swich pin, VL stands for logic voltage
const int PR = 2; // the number of the LED pin, PR stands for red probe
const int PG = 3; // the number of the LED pin, PG stand for green probe
const int VP = A0; // select the input pin for the potentiometer
const int SF = 4; // set scale factor for 10 bit to 4 bit conversion
const int VR = 9; // the number of Pin connected to R of RGB LED
const int VG = 10; // the number of Pin connected to G of RGB LED
const int VB = 5; // the number of Pin connected to B of RGB LED
const int Pb = 8; // the number of Pin connected to Pb of RGB LED
const int Ready = 0;
const int SetDutyR = 1;
const int SetDutyG = 2;
const int SetDutyB = 3;
const int BlendRGB = 4;
// variables will change:
int SwitchState = 0; // give a name for the switch state variable
int PotValue = 0; // variable to store the value coming from the sensor
int DutyR = 0 ; // variable to store the value duty ratio of PWM for Red
int DutyG = 0; // variable to store the value duty ratio of PWM for Green
int DutyB = 0; // variable to store the value duty ratio of PWM for Blue
int State = Ready; // initializing State to be ready
//declares the inputs and outputs of the circuit
void setup() {
pinMode(A0, INPUT);
pinMode(VL, INPUT);
pinMode(Pb, INPUT);
pinMode(PR, OUTPUT);
pinMode(PG, OUTPUT);
pinMode(VR, OUTPUT);
pinMode(VG, OUTPUT);
pinMode(VB, OUTPUT);
}
//Defining the Ready State function
//blinks the green LED until the pushbutton is pressed
void Ready_Define() {
digitalWrite(PG,0);
digitalWrite(PR,0);
digitalWrite(VR,0);
digitalWrite(VG,0);
digitalWrite(VB,0);
delay(250);
digitalWrite(PG,1);
//if the pushbutton is pressed, change to state SetPWMR
if (digitalRead(Pb) == 0)
{
State = SetDutyR;
}
delay(100);
}
//Defining the SetDutyR state function
//SetRGB redness based on pot, until the pushbutton is pressed
void SetDutyR_Define() {
PotValue = analogRead(A0);
DutyR = PotValue/SF;
digitalWrite(PR,0);
digitalWrite(PG,1);
analogWrite(VR,DutyR);
digitalWrite(VG,0);
digitalWrite(VB,0);
delay(50);
//if the pushbutton is pressed, change to state SetPWMG
if (digitalRead(Pb) == 0)
{
State = SetDutyG;
}
delay(150);
}
//Defining the SetDutyG state function
//SetRGB greenness based on pot, until the pushbutton is pressed
void SetDutyG_Define() {
PotValue = analogRead(A0);
DutyG = PotValue/SF;
digitalWrite(PR,0);
digitalWrite(PG,1);
analogWrite(VG,DutyG);
digitalWrite(VR,0);
digitalWrite(VB,0);
delay(50);
//if the pushbutton is pressed, change to state SetPWMG
if (digitalRead(Pb) == 0)
{
State = SetDutyB;
}
delay(150);
}
//Defining the SetDutyB state function
//SetRGB greenness based on pot, until the pushbutton is pressed
void SetDutyB_Define() {
PotValue = analogRead(A0);
DutyB = PotValue/SF;
digitalWrite(PR,0);
digitalWrite(PG,1);
analogWrite(VB,DutyB);
digitalWrite(VR,0);
digitalWrite(VG,0);
delay(50);
//if the pushbutton is pressed, change to state SetPWMG
if (digitalRead(Pb) == 0)
{
State = BlendRGB;
}
delay(150);
}
//Defining the BlendRGB state function
//Green LED is on, and the RGB LED is a blend(white light), until pushbutton is pressed
void BlendRGB_Define() {
delay(50);
digitalWrite(PR,0);
digitalWrite(PG,0);
analogWrite(VR,DutyR);
analogWrite(VG,DutyG);
analogWrite(VB,DutyB);
delay(50);
//if the pushbutton is pressed, change to READY state
if (digitalRead(Pb) == 0)
{
State = Ready;
}
delay(350);
}
// your code written here keeps running until eternity
void loop() {
//if the circuit is in Ready state, go to handle_Ready_Define() function
if (State == Ready)
{
Ready_Define();
}
//if the circuit is in SetDutyR state, go to SetDutyR_Define() function
else if (State == SetDutyR)
{
SetDutyR_Define();
}
//if the circuit is in SetDutyG state, go to SetDutyG_Define() function
else if (State == SetDutyG)
{
SetDutyG_Define();
}
//if the circuit is in SetDutyB state, go to SetDutyB_Define() function
else if (State == SetDutyB)
{
SetDutyB_Define();
}
//if the circuit is in BlendRGB state, go to BlendRGB_Define() function
else if (State == BlendRGB)
{
BlendRGB_Define();
}
}