Advertisement
Lakamfo

Arduino RGB controller

Mar 6th, 2023 (edited)
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define potent_pin 0
  2.  
  3. int pin_red = 6;
  4. int pin_green = 10;
  5. int pin_blue = 3;
  6. int beeper = 9;
  7.  
  8. uint8_t red_bright = 255;
  9. uint8_t green_bright = 255;
  10. uint8_t blue_bright = 255;
  11. uint8_t count = 1;
  12. uint8_t rgb_state = 1;
  13.  
  14. void setup() {
  15.   Serial.begin(9600);
  16.  
  17.   pinMode(pin_red, OUTPUT);
  18.   pinMode(pin_green, OUTPUT);
  19.   pinMode(pin_blue, OUTPUT);
  20.   pinMode(beeper, OUTPUT);
  21.   pinMode(7, INPUT_PULLUP);
  22. }
  23.  
  24. bool flag = false;
  25.  
  26. void loop() {
  27.   btn();
  28.   potent();
  29.   rgb_led();
  30.   Serial.println(rgb_state);
  31.  
  32.   if (flag == true) {
  33.     analogWrite(beeper, 250);
  34.   }
  35.   else{
  36.     analogWrite(beeper, 0);
  37.   }
  38.   delay(5);
  39. }
  40.  
  41. void btn(){
  42.   bool btnState = !digitalRead(7);
  43.   if (btnState && !flag) {
  44.     flag = true;
  45.     rgb_state++;
  46.     if (rgb_state > 3){
  47.       rgb_state = 1;
  48.     }
  49.    
  50.   }
  51.   if (!btnState && flag){
  52.     flag = false;  
  53.   }
  54. }
  55.  
  56. void potent(){
  57.   count = map(analogRead(potent_pin),0,1023,0,255);
  58.   switch(rgb_state) {
  59.     case 1:
  60.       red_bright = count;
  61.     case 2:
  62.       green_bright = count;
  63.     case 3:
  64.       blue_bright = count;
  65.        
  66.   }
  67. }
  68.  
  69. void rgb_led(){
  70.   analogWrite(pin_red, red_bright);
  71.   analogWrite(pin_green, green_bright);
  72.   analogWrite(pin_blue, blue_bright);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement