int pinPot = A0;
int pinLed1 = 6, pinLed2 = 9, pinLed3 = 10, pinLed4 = 11;
int pwmPot, pwmLed1, pwmLed2, pwmLed3, pwmLed4;
int state;
void setup() {
Serial.begin(9600);
pinMode(pinLed1, OUTPUT);
pinMode(pinLed2, OUTPUT);
pinMode(pinLed3, OUTPUT);
pinMode(pinLed4, OUTPUT);
}
void loop() {
analogWrite(pinLed1, pwmLed1);
analogWrite(pinLed2, pwmLed2);
analogWrite(pinLed3, pwmLed3);
analogWrite(pinLed4, pwmLed4);
Serial.println("PWM LED : ");
Serial.print(pwmLed1);Serial.print("|");
Serial.print(pwmLed2);Serial.print("|");
Serial.print(pwmLed3);Serial.print("|");
Serial.println(pwmLed4);
//PROGRAM SWITCH CASE MENGATUR TERANG LED
pwmPot = analogRead(pinPot);
switch(state){
case 0: //Kondisi Awal
pwmLed1 = pwmPot;
if(pwmLed1 >= 255) {
pwmLed1 = 255;
state = 1;
}
break;
case 1: //Kondisi Kedua
pwmLed2 = pwmPot - 255;
if(pwmLed2 <= 0){
pwmLed2 = 0;
state = 0;
}
else if(pwmLed2 >= 255){
pwmLed2 = 255;
state = 2;
}
break;
case 2:
pwmLed3 = pwmPot - 510;
if(pwmLed3 <= 0){
pwmLed3 = 0;
state = 1;
}
else if(pwmLed3 >= 255){
pwmLed3 = 255;
state = 3;
}
break;
case 3:
pwmLed4 = pwmPot - 765;
if(pwmLed4 <= 0){
pwmLed4 = 0;
state = 2;
}
else if(pwmLed4 >= 255 || pwmPot >= 1023){
pwmLed4 = 255;
}
}
}