lab 4

Building a Controllable RGB LED Nightlight

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 BUTTON1 = 2; //The Button is connected to pin 2
const int BUTTON2 = 3;
const int BUTTON3 = 4;
const int BUTTON4 = 5;
boolean lastButton1 = LOW;
boolean lastButton2 = LOW;
boolean lastButton3 = LOW;
boolean lastButton4 = LOW;//Last Button State
boolean currentButton1 = LOW;
boolean currentButton2 = LOW;
boolean currentButton3 = LOW;
boolean currentButton4 = LOW;//Current Button State
int ledMode = 0;//Cycle between LED states
int bnk = 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 (BUTTON1, INPUT);
  pinMode (BUTTON2, INPUT);
  pinMode (BUTTON3, INPUT);
  pinMode (BUTTON4, 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, int button)
{
  boolean current = digitalRead(button); //Read the button state
  if (last != current) //if it's different...
  {
    delay(10); //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 mode1)
{
  switch (mode1)
  {
    case 1:
      analogWrite(RLED, 0);
      analogWrite(GLED, 127);
      analogWrite(BLED, 127);
      break;
    case 2:
      analogWrite(RLED, 255);
      analogWrite(GLED, 80);
      analogWrite(BLED, 0);
      break;
    case 3:
      analogWrite(RLED, 255);
      analogWrite(GLED, 240);
      analogWrite(BLED, 235);
      break;
    case 4:
      analogWrite(RLED, 127);
      analogWrite(GLED, 0);
      analogWrite(BLED, 127);
      break;
    //case 0:

    default:
      digitalWrite(RLED, LOW);
      digitalWrite(GLED, LOW);
      digitalWrite(BLED, LOW);
      break;
  }


}





void loop()
{
  currentButton1 = debounce(lastButton1, BUTTON1);
  currentButton2 = debounce(lastButton2, BUTTON2);
  currentButton3 = debounce(lastButton3, BUTTON3);
  currentButton4 = debounce(lastButton4, BUTTON4); //read debounced state
  if (lastButton1 == LOW && currentButton1 == HIGH) //if it was pressed...
  {
    if (ledMode != 1)
      ledMode = 1;
    else
      ledMode = 0;
  }
  lastButton1 = currentButton1; //reset button value
  //if you've cycled through the different options,
  //reset the counter to 0

  setMode(ledMode); //change the LED state



  if (lastButton2 == LOW && currentButton2 == HIGH) //if it was pressed...
  {
    if (ledMode != 2)
      ledMode = 2;
    else
      ledMode = 0;
  }
  lastButton2 = currentButton2; //reset button value

  if (lastButton3 == LOW && currentButton3 == HIGH) //if it was pressed...
  {
    if (ledMode != 3)
      ledMode = 3;
    else
      ledMode = 0;
  }
  lastButton3 = currentButton3; //reset button value


  if (lastButton4 == LOW && currentButton4 == HIGH) //if it was pressed...
  {
    if (ledMode != 0)
      bnk++;
    else
      bnk = 0;
  }
   //reset button value

  while (bnk == 1)
  {
   
    setMode(ledMode);
    delay(50);
    setMode(0);
    delay(50);
    if (ledMode == 0)
      bnk = 0;
        break;
  }
  while (bnk == 2)
  {
   
    setMode(ledMode);
    delay(500);
        if (ledMode == 0)
    bnk = 0;
    setMode(0);
    delay(500);
    if (ledMode == 0)
    bnk = 0;
    break;
  }
 
if (bnk == 3)bnk = 0;
  lastButton4 = currentButton4;

}


No Pic

Comments

Popular posts from this blog

Homework 6

Another Day 23

Homework4