Day 4
For homework i had to create an RGB LED that changes color depending on what button is pressed here is my code
const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int BUTTON=2; //The Button is connected to pin 2
const int XBUTTON=3;
const int YBUTTON=5;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
boolean XlastButton=LOW;
boolean XcurrentButton=LOW;
boolean YlastButton=LOW;
boolean YcurrentButton=LOW;
int ledMode = 0; //Cycle between LED states
int XledMode=0;
int YledMode=0;
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (BUTTON, INPUT); //Set button as input (not required)
pinMode (XBUTTON, INPUT);
pinMode (YBUTTON, INPUT);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
boolean Xdebounce(boolean Xlast)
{
boolean Xcurrent=digitalRead(XBUTTON);
if ( Xlast!= Xcurrent)
{
delay(5);
Xcurrent=digitalRead(XBUTTON);
}
return Xcurrent;
}
boolean Ydebounce(boolean Ylast)
{
boolean Ycurrent = digitalRead(YBUTTON); //Read the button state
if (Ylast != Ycurrent) //if it's different...
{
delay(5); //wait 5ms
Ycurrent = digitalRead(YBUTTON); //read it again
}
return Ycurrent; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
//RED
switch (mode)
{
case 1:
digitalWrite(RLED, HIGH);
break;
default:
digitalWrite(RLED, LOW);
break;
}
}
void XsetMode(int Xmode)
{
switch (Xmode)
{
case 1:
digitalWrite(GLED, HIGH);
break;
default:
digitalWrite(GLED, LOW);
break;
}
}
void YsetMode(int Ymode)
{
//BLUE
switch (Ymode)
{
case 1:
digitalWrite(BLED, HIGH);
break;
default:
digitalWrite(BLED, LOW);
break;
}
}
void loop()
{
currentButton = debounce(lastButton); //read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton = currentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 2) ledMode = 0;
setMode(ledMode); //change the LED state
XcurrentButton = Xdebounce(XlastButton); //read debounced state
if (XlastButton == LOW && XcurrentButton == HIGH) //if it was pressed...
{
XledMode++; //increment the LED value
}
XlastButton = XcurrentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (XledMode == 2) XledMode = 0;
XsetMode(XledMode); //change the LED state
YcurrentButton = Ydebounce(YlastButton); //read debounced state
if (YlastButton == LOW && YcurrentButton == HIGH) //if it was pressed...
{
YledMode++; //increment the LED value
}
YlastButton = YcurrentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (YledMode == 2) YledMode = 0;
YsetMode(YledMode); //change the LED state
for (int i=100; i<=400; i=i+100)
{
digitalWrite(RLED, HIGH);
delay(i);
digitalWrite(RLED, LOW);
delay(i);
}
for (int j=400; j<=700; j=j+100)
{
digitalWrite(GLED, HIGH);
delay(j);
digitalWrite(GLED, LOW);
delay(j);
}
for (int k=700; k<=1000; k=k+100)
{
digitalWrite(BLED, HIGH);
delay(k);
digitalWrite(BLED, LOW);
delay(k);
}
Here's a link that shows a video of the light in action
const int BLED=9; //Blue LED on Pin 9
const int GLED=10; //Green LED on Pin 10
const int RLED=11; //Red LED on Pin 11
const int BUTTON=2; //The Button is connected to pin 2
const int XBUTTON=3;
const int YBUTTON=5;
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
boolean XlastButton=LOW;
boolean XcurrentButton=LOW;
boolean YlastButton=LOW;
boolean YcurrentButton=LOW;
int ledMode = 0; //Cycle between LED states
int XledMode=0;
int YledMode=0;
void setup()
{
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (BUTTON, INPUT); //Set button as input (not required)
pinMode (XBUTTON, INPUT);
pinMode (YBUTTON, INPUT);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
boolean Xdebounce(boolean Xlast)
{
boolean Xcurrent=digitalRead(XBUTTON);
if ( Xlast!= Xcurrent)
{
delay(5);
Xcurrent=digitalRead(XBUTTON);
}
return Xcurrent;
}
boolean Ydebounce(boolean Ylast)
{
boolean Ycurrent = digitalRead(YBUTTON); //Read the button state
if (Ylast != Ycurrent) //if it's different...
{
delay(5); //wait 5ms
Ycurrent = digitalRead(YBUTTON); //read it again
}
return Ycurrent; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{
//RED
switch (mode)
{
case 1:
digitalWrite(RLED, HIGH);
break;
default:
digitalWrite(RLED, LOW);
break;
}
}
void XsetMode(int Xmode)
{
switch (Xmode)
{
case 1:
digitalWrite(GLED, HIGH);
break;
default:
digitalWrite(GLED, LOW);
break;
}
}
void YsetMode(int Ymode)
{
//BLUE
switch (Ymode)
{
case 1:
digitalWrite(BLED, HIGH);
break;
default:
digitalWrite(BLED, LOW);
break;
}
}
void loop()
{
currentButton = debounce(lastButton); //read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton = currentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (ledMode == 2) ledMode = 0;
setMode(ledMode); //change the LED state
XcurrentButton = Xdebounce(XlastButton); //read debounced state
if (XlastButton == LOW && XcurrentButton == HIGH) //if it was pressed...
{
XledMode++; //increment the LED value
}
XlastButton = XcurrentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (XledMode == 2) XledMode = 0;
XsetMode(XledMode); //change the LED state
YcurrentButton = Ydebounce(YlastButton); //read debounced state
if (YlastButton == LOW && YcurrentButton == HIGH) //if it was pressed...
{
YledMode++; //increment the LED value
}
YlastButton = YcurrentButton; //reset button value
//if you've cycled through the different options,
//reset the counter to 0
if (YledMode == 2) YledMode = 0;
YsetMode(YledMode); //change the LED state
for (int i=100; i<=400; i=i+100)
{
digitalWrite(RLED, HIGH);
delay(i);
digitalWrite(RLED, LOW);
delay(i);
}
for (int j=400; j<=700; j=j+100)
{
digitalWrite(GLED, HIGH);
delay(j);
digitalWrite(GLED, LOW);
delay(j);
}
for (int k=700; k<=1000; k=k+100)
{
digitalWrite(BLED, HIGH);
delay(k);
digitalWrite(BLED, LOW);
delay(k);
}
Here's a link that shows a video of the light in action
Comments
Post a Comment