Tuesday, May 12, 2015

Controlling red, green and blue leds with push buttons and a potentiometer

In this improvement, we read the value of potentiometer using analogRead() function. We have also 3 push buttons. By using the 4 independent informations coming from potentiometer and 3 pushbuttons, we change the brightness of the leds. While doing this, we used analogWrite function. We write the potentiometer value to leds.

Berrak Şişman and İlker Çam
Our video is given below.

int ledRed = 13;
int ledGreen = 12;
int ledBlue = 11;
int potansPin = 0;

int button1Pin = 2;
int button2Pin = 3;
int button3Pin = 4;
int controlled; // 1 Red -> 2 Green -> 3 Blue -> 4 ALL

long timePASSED;

void setup() {
controlled = ledRed;
pinMode(ledRed,OUTPUT);
pinMode(ledGreen,OUTPUT);
pinMode(ledBlue,OUTPUT);

//Potans is been read by analogRead
pinMode(button1Pin,INPUT);
pinMode(button2Pin,INPUT);
pinMode(button3Pin,INPUT);
Serial.begin(9600);
timePASSED = millis();
}

// the loop function runs over and over again forever
void loop() {
  int potansValue = analogRead(potansPin);
  Serial.write(potansValue);
  int btn1State,btn2State,btn3State;
  btn1State = digitalRead(button1Pin);
  btn2State = digitalRead(button2Pin);
  btn3State = digitalRead(button3Pin);

  if(btn1State == LOW && btn2State == LOW && btn3State ==LOW){
    controlled = 4;
  } else if(btn1State == LOW){
    controlled = 1;
  } else if(btn2State == LOW){
    controlled = 2;
  } else if (btn3State == LOW){
    controlled = 3;
  }

  switch(controlled){
    case 1:
      analogWrite(ledRed,potansValue);
      break;
    case 2:
      analogWrite(ledGreen,potansValue);
      break;
    case 3:
      analogWrite(ledBlue,potansValue);
      break;
    case 4:
      analogWrite(ledRed,potansValue);
      analogWrite(ledGreen,potansValue);
      analogWrite(ledBlue,potansValue);
      break;
  }

  if((timePASSED+potansValue) < millis()){
      analogWrite(ledRed,0);
      analogWrite(ledGreen,0);
      analogWrite(ledBlue,0);
      timePASSED = millis();
  }
  delay(50);
}

No comments:

Post a Comment