Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #define REDPIN 5
  2. #define GREENPIN 6
  3. #define BLUEPIN 3
  4.  
  5. int currentR = 0;
  6. int currentG = 0;
  7. int currentB = 0;
  8. bool pulse = false;
  9. bool inProgress = false;
  10. unsigned long currentMillis = millis();
  11. bool alternating = false;;
  12.  
  13. void setup() {
  14.     pinMode(REDPIN, OUTPUT);
  15.     pinMode(GREENPIN, OUTPUT);
  16.     pinMode(BLUEPIN, OUTPUT);
  17.     Serial.begin(9600);
  18. }
  19.  
  20. void loop() {
  21.  
  22.  
  23.  
  24.     if (pulse) {
  25.         if (!alternating) {
  26.             SmoothFade(currentR, currentG, currentB, 0, 0, 0);
  27.             alternating = true;
  28.         }
  29.         else {
  30.             SmoothFade(0, 0, 0, currentR, currentG, currentB);
  31.             alternating = false;
  32.         }
  33.     }
  34.  
  35. }
  36. void serialEvent() {
  37.     if (Serial.available() == 1) {
  38.         String input = Serial.readString();
  39.  
  40.         if (input[0] == 35) {
  41.             long number = strtol(&input[1], NULL, 16);
  42.  
  43.             long r = number >> 16;
  44.             long g = number >> 8 & 0xFF;
  45.             long b = number & 0xFF;
  46.  
  47.             Serial.println("Color set to: " + input);
  48.             SmoothFade(currentR, currentG, currentB, r, g, b);
  49.  
  50.             currentR = r;
  51.             currentG = g;
  52.             currentB = b;
  53.  
  54.         }
  55.  
  56.         if (input == "pulse") {
  57.             if (pulse == false) {
  58.                 pulse = true;
  59.                 Serial.println("Pulsing!");
  60.             }
  61.             else {
  62.                 pulse = false;
  63.                 Serial.println("Pulsing NOT!");
  64.             }
  65.         }
  66.     }
  67. }
  68. void SmoothFade(int currR, int currG, int currB, int r, int g, int b) {
  69.     int curR = currR, curG = currG, curB = currB;
  70.     int stepR = curR - r, stepG = curG - g, stepB = curB - b;
  71.  
  72.     for (int i = 255; i > 0; i--)
  73.     {
  74.  
  75.         if (i % (255 / abs(stepR)) == 0 && stepR != 0) {
  76.             if (stepR < 0 && curR < 255) {
  77.                 curR++;
  78.             }
  79.             else if (curR > 0) {
  80.                 curR--;
  81.             }
  82.             analogWrite(REDPIN, curR);
  83.         }
  84.         if (i % (255 / abs(stepG)) == 0 && stepG != 0) {
  85.             if (stepG < 0 && curG < 255) {
  86.                 curG++;
  87.             }
  88.             else if (curG > 0) {
  89.                 curG--;
  90.             }
  91.             analogWrite(GREENPIN, curG);
  92.         }
  93.         if (i % (255 / abs(stepB)) == 0 && stepB != 0) {
  94.             if (stepB < 0 && curB < 255) {
  95.                 curB++;
  96.             }
  97.             else if (curB > 0) {
  98.                 curB--;
  99.             }
  100.             analogWrite(BLUEPIN, curB);
  101.         }
  102.         delay(5);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement