// ###################################################################
class RGB_LED {
public :
RGB_LED(int red_pin, int green_pin, int blue_pin);
void setRed(int duty_cycle);
void setGreen(int duty_cycle);
void setBlue(int duty_cycle);
void showRGB();
private :
int duty_cycle_RED;
int duty_cycle_GREEN;
int duty_cycle_BLUE;
int pinRED;
int pinGREEN;
int pinBLUE;
};
RGB_LED::RGB_LED(int red_pin, int green_pin, int blue_pin) {
pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
pinMode(blue_pin, OUTPUT);
pinRED = red_pin;
pinGREEN = green_pin;
pinBLUE = blue_pin;
}
void RGB_LED::setRed(int duty_cycle){
duty_cycle_RED = 255 - duty_cycle;
showRGB();
}
void RGB_LED::setGreen(int duty_cycle) {
duty_cycle_GREEN = 255 - duty_cycle;
showRGB();
}
void RGB_LED::setBlue(int duty_cycle){
duty_cycle_BLUE = 255 - duty_cycle;
showRGB();
}
void RGB_LED::showRGB() {
analogWrite(pinRED, duty_cycle_RED);
analogWrite(pinGREEN, duty_cycle_GREEN);
analogWrite(pinBLUE, duty_cycle_BLUE);
}
// ###################################################################
int SW[3] = {2, 3, 4};
int STEP = 8;
int red, green, blue = 0;
int toggleRed, toggleGreen, toggleBlue = 0;
RGB_LED led(9,10,11);
// ###################################################################
void printRGB(int red, int green, int blue) {
Serial.print("RED DutyCycle : ");
Serial.println(red);
Serial.print("GREEN DutyCycle : ");
Serial.println(green);
Serial.print("BLUE DutyCycle : ");
Serial.println(blue);
Serial.println("---------------------------------");
}
int check(int colour, int STEP) {
colour = colour + STEP;
if (colour > 255) {
colour = 0;
}
return colour;
}
// ####################################################################
void setup() {
Serial.begin(9600);
pinMode(SW[0], INPUT_PULLUP);
pinMode(SW[1], INPUT_PULLUP);
pinMode(SW[2], INPUT_PULLUP);
}
void loop() {
if ((digitalRead(SW[0]) == LOW) &&(toggleRed == 0)){
toggleRed = 1;
}
else if ((digitalRead(SW[0]) == HIGH) && (toggleRed == 1)) {
toggleRed = 0;
red = check(red, STEP);
printRGB(red, green, blue);
}
if ((digitalRead(SW[1]) == LOW) &&(toggleGreen == 0)){
toggleGreen = 1;
}
else if ((digitalRead(SW[1]) == HIGH) && (toggleGreen == 1)) {
toggleGreen = 0;
green = check(green, STEP);
printRGB(red, green, blue);
}
if ((digitalRead(SW[2]) == LOW) &&(toggleBlue == 0)){
toggleBlue = 1;
}
else if ((digitalRead(SW[2]) == HIGH) && (toggleBlue == 1)) {
toggleBlue = 0;
blue = check(blue, STEP);
printRGB(red, green, blue);
}
led.setRed(red);
led.setGreen(green);
led.setBlue(blue);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น