Day_4

Comments: The programming homework was pretty straight forward, well at least I think it is anyway. What's killer was the arduino homework, since I have to implement the debounce, which I have yet to figure out. I managed to change the color of the RGB to the desired color, but haven't managed to get the color to change on the button pushes. I will probably ask David to help me figure out how to debounce the button to get the RGB to work.
2. Give the value of a after the following set of statements is executed.
int a = 750; ...
if (a>0) (which it is)
 if (a >= 1000) (It's not)
a = 0;
else if (a < 500) a *= 2;(It's not)
 else a *= 10; [a*10 =a], since a =750, [a=750*10]
else a += 3; (not true)
answer 7,500

1)
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
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
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)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
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
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{

  switch(mode)
  {
case 1:/*(TEAL)*/
 digitalWrite(RLED, 0);
 digitalWrite(GLED, 255);
 digitalWrite(BLED, 255);
 break;
 case 2:/*(Orange)*/
 digitalWrite(RLED, 255);
 digitalWrite(GLED, 128);
 digitalWrite(BLED, 0);
 break;
 case 3:/*(WHITE)*/
 digitalWrite(RLED, HIGH);
 digitalWrite(GLED, HIGH);
 digitalWrite(BLED, HIGH);
 break;
 case 4:
 analogWrite(RLED, 127);
 analogWrite(GLED, 0);
 analogWrite(BLED, 127);
 break;
 case 0:
  digitalWrite(RLED, LOW);
 digitalWrite(GLED, LOW);
 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 == 5) ledMode = 0;
 setMode(ledMode); //change the LED state
}

2)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
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
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)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
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
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.
*/
void setMode(int mode)
{

  switch(mode)
  {
case 1:
 digitalWrite(RLED, HIGH);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, LOW);
 break;
 case 2:
 digitalWrite(RLED, LOW);
 digitalWrite(GLED, HIGH);
 digitalWrite(BLED, LOW);
 break;
 case 3:
 digitalWrite(RLED, LOW);
 digitalWrite(GLED, LOW);
 digitalWrite(BLED, HIGH);
 break;
 case 4:
 analogWrite(RLED, 127);
 analogWrite(GLED, 0);
 analogWrite(BLED, 127);
 break;
 case 0:
  digitalWrite(RLED, LOW);
 digitalWrite(GLED, LOW);
 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 == 5) ledMode = 0;
 setMode(ledMode); //change the LED state
}



3)const int BLED=9; //BLED LED on Pin 9
const int GLED=10; //GLED LED on Pin 10
const int RLED=11; //RLED LED on Pin 11
const int BUTTON1=2; //The BUTTON1 is connected to pin 2
const int BUTTON2=3;
const int BUTTON3=4;
boolean lastBUTTON1 = LOW; //Last BUTTON1 State
boolean currentBUTTON1 = LOW; //Current BUTTON1 State
boolean lastBUTTON2 = LOW;
boolean currentBUTTON2 = LOW;
boolean lastBUTTON3 = LOW;
boolean currentBUTTON13 = LOW;
int ledMode = 0; //Cycle between LED states
void setup()
{
 pinMode (BLED, OUTPUT); //Set BLED LED as Output
 pinMode (GLED, OUTPUT); //Set GLED LED as Output
 pinMode (RLED, OUTPUT); //Set RLED LED as Output
 pinMode (BUTTON1, INPUT); //Set BUTTON1 as input (not requiRLED)
 pinMode (BUTTON2, INPUT); //Set BUTTON1 as input (not requiRLED)
 pinMode (BUTTON3, INPUT); //Set BUTTON1 as input (not requiRLED)
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce(boolean last)
{
 boolean current = digitalRead(BUTTON1); //Read the button state
 if (last != current) //if it's different...
 {
 delay(5); //wait 5ms
 current = digitalRead(BUTTON1); //read it again
 }
 return current; //return the current value
}
boolean debounce2(boolean last)
{
 boolean current2 = digitalRead(BUTTON2); //Read the button state
 if (last != current2) //if it's different...
 {
 delay(15); //wait 5ms
 current2 = digitalRead(BUTTON2); //read it again
 }
 return current2; //return the current value
}
/*
* LED Mode Selection Pass a number for the LED state and set it accordingly.*/

void loop()
{
  if(digitalRead(BUTTON1) == HIGH)
  {

digitalWrite(BLED,1);
}
else
{
digitalWrite(BLED,0);
}
if(digitalRead(BUTTON2) == HIGH){
digitalWrite(RLED,1);
}
else
{
digitalWrite(RLED,0);
}
if(digitalRead(BUTTON3) == HIGH){
digitalWrite(GLED,1);
}
else
{
digitalWrite(GLED,0);
}
 }


Comments

Popular posts from this blog

Day_13

Day 18 WH