Advertisement
skizziks_53

Mega LEDrunner v1.0

Feb 5th, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.71 KB | None | 0 0
  1. /*
  2.    2/5/2018
  3.    Note: this is a file from online, trying to help someone on reddit/arduino
  4.    it's a Mega that is supposed to control some RGB led strings using mosfets
  5.    10k pot on pin A0
  6.    N-O switch on pin A2
  7. */
  8.  
  9. // Init the Pins used for PWM
  10. const int redPin = 9;
  11. const int greenPin = 10;
  12. const int bluePin = 11;
  13.  
  14. // Init the Pins used for 10K pots
  15. const int PotPin = A0;
  16. // You had your input button connected to pin #1, but you were using serial messages....
  17. // If you want to use serial messaging, then you cannot connect anything to pins 0 and 1.
  18. const int PotSwitch = A2; // Button input is moved to A2. Also input pin mode is changed to INPUT_PULLUP.
  19.  
  20. // Init our Vars
  21. int currentColorValueRed;
  22. int currentColorValueGreen;
  23. int currentColorValueBlue;
  24. int currentColorValueRed1;
  25. int currentColorValueGreen1;
  26. int currentColorValueBlue1;
  27.  
  28. int button_press_count = 0; // Changed to use 0 = off, 1 = red adjust, 2 = green adjust, 3 = blue adjust, 4 = random
  29.  
  30. int buttonState = 0;
  31.  
  32. // Below are the variables used for debouncing the button input.
  33. int buttonDebounceTime = 500; // This is the time in milliseconds to debounce the button.
  34. bool buttonInputEnabled = true; // This is a variable to block button input while it is debouncing.
  35. unsigned long buttonPress_beginTime = 0;
  36. unsigned long buttonPress_currentTime = 0;
  37.  
  38. // Below are the variables used for running the mode #4 feature.
  39. int randomColor_cycleTime = 3000; // This is the time in milliseconds to cycle the random colors
  40. unsigned long randomColor_cycle_beginTime = 0;
  41. unsigned long randomColor_cycle_currentTime = 0;
  42.  
  43.  
  44.  
  45. // function prototypes---------
  46. void buttonPressed(); // -- this is for debouncing the button input.
  47. void writeToColorPins();
  48. void generate_random_colors();
  49. void generate_random_red_color();
  50. void generate_random_green_color();
  51. void generate_random_blue_color();
  52.  
  53.  
  54. void setup()
  55. {
  56.   Serial.begin(9600);
  57.   Serial.println("(serial speed test message)");
  58.   pinMode(redPin, OUTPUT);
  59.   pinMode(greenPin, OUTPUT);
  60.   pinMode(bluePin, OUTPUT);
  61.   pinMode(PotSwitch, INPUT_PULLUP);
  62.  
  63. }
  64.  
  65.  
  66. void loop()
  67. {
  68.   if (buttonInputEnabled == true) { // The button is checked only if it is not de-bouncing
  69.     buttonState = digitalRead(PotSwitch);
  70.     if (buttonState == LOW) { // Check for a LOW state here, since that is the "button pressed" state.
  71.       buttonPressed();
  72.       button_press_count++;
  73.       // Changed to use 0 = off, 1 = red adjust, 2 = green adjust, 3 = blue adjust, 4 = random
  74.       if (button_press_count >= 5) {
  75.         button_press_count = 0;
  76.       }
  77.       // The states that only require one change can be performed here:
  78.       // Also this way, the analogRead(PotPin) is only performed once, when
  79.       Serial.print("button_press_count = ");
  80.       Serial.println(button_press_count);
  81.       switch (button_press_count) {
  82.         case 0:
  83.           Serial.println("(turning LEDs off)");
  84.           currentColorValueRed = 0;
  85.           currentColorValueGreen = 0;
  86.           currentColorValueBlue = 0;
  87.           break;
  88.         case 1:
  89.           generate_random_red_color();
  90.           break;
  91.         case 2:
  92.           generate_random_green_color();
  93.           break;
  94.         case 3:
  95.           generate_random_blue_color();
  96.           break;
  97.         case 4:
  98.           generate_random_colors();
  99.           randomColor_cycle_beginTime = millis();
  100.           break;
  101.         default:
  102.           // nothing here
  103.           break;
  104.       }
  105.       writeToColorPins();
  106.       //buttonPressed();
  107.     }
  108.   }
  109.  
  110.  
  111.   // The section below is for automatically repeating the random color generation for mode #4.
  112.   if (button_press_count == 4) {
  113.     randomColor_cycle_currentTime = millis();
  114.     if (randomColor_cycle_currentTime > randomColor_cycle_beginTime) {
  115.       if (randomColor_cycle_currentTime >= (randomColor_cycle_beginTime + randomColor_cycleTime)) {
  116.         Serial.println("(auto-changing random colors)");
  117.         generate_random_colors();
  118.         writeToColorPins();
  119.         // reset the start time to the current time so that the cycle will repeat:
  120.         randomColor_cycle_beginTime = millis();
  121.       }
  122.     }
  123.     else {
  124.       randomColor_cycle_beginTime = millis(); // millis() rollover condition check
  125.     }
  126.   }
  127.  
  128.  
  129.   // The section below is for debouncing the button input.
  130.   if (buttonInputEnabled == false) {
  131.     buttonPress_currentTime = millis();
  132.     if (buttonPress_currentTime > buttonPress_beginTime) {
  133.       if (buttonPress_currentTime >= (buttonPress_beginTime + buttonDebounceTime)) {
  134.         buttonInputEnabled = true;
  135.         Serial.println("(button re-enabled)");
  136.       }
  137.     }
  138.     else {
  139.       buttonPress_beginTime = millis(); // millis() rollover condition check
  140.     }
  141.   }
  142.  
  143. }// end of main loop()
  144.  
  145.  
  146.  
  147. void buttonPressed() {
  148.   Serial.println("buttonPressed()");
  149.   buttonInputEnabled = false;
  150.   buttonPress_beginTime = millis();
  151. }
  152.  
  153.  
  154. void generate_random_colors() {
  155.   Serial.println("generateRandomColors()");
  156.   generate_random_red_color();
  157.   generate_random_green_color();
  158.   generate_random_blue_color();
  159. }
  160.  
  161. void generate_random_red_color() {
  162.   currentColorValueRed = random(0, 255);
  163.   Serial.print("random red = ");
  164.   Serial.println(currentColorValueRed);
  165. }
  166.  
  167. void generate_random_green_color() {
  168.   currentColorValueGreen = random(0, 255);
  169.   Serial.print("random green = ");
  170.   Serial.println(currentColorValueGreen);
  171. }
  172.  
  173. void generate_random_blue_color() {
  174.   currentColorValueBlue = random(0, 255);
  175.   Serial.print("random blue = ");
  176.   Serial.println(currentColorValueBlue);
  177. }
  178.  
  179.  
  180. void writeToColorPins() {
  181.   Serial.println("writeToColorPins()");
  182.   analogWrite(redPin, currentColorValueRed);
  183.   analogWrite(bluePin, currentColorValueBlue);
  184.   analogWrite(greenPin, currentColorValueGreen);
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement