Advertisement
Guest User

Untitled

a guest
May 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. int colourValue = 0;
  2.  
  3. int redPin = 9;
  4. int grnPin = 10;
  5. int bluPin = 11;
  6.  
  7. int redVal = 0;
  8. int grnVal = 0;
  9. int bluVal = 0;
  10.  
  11. const int numReadings = 10;
  12. const int faderSampleRate = 10;
  13.  
  14. int readings[numReadings];
  15. int readIndex = 0;
  16. int total = 0;
  17. int inputPin = A3;
  18.  
  19. void setup()
  20. {
  21. pinMode(redPin, OUTPUT);
  22. pinMode(grnPin, OUTPUT);
  23. pinMode(bluPin, OUTPUT);
  24. Serial.begin(9600);
  25.  
  26. for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  27. readings[thisReading] = 0;
  28. }
  29. }
  30.  
  31.  
  32. void loop() {
  33. colourValue = map(constrain(analogRead(inputPin), 750, 1024), 750, 1024, 0, 1024);
  34. total = total - readings[readIndex];
  35. readings[readIndex] = colourValue;
  36. total = total + readings[readIndex];
  37. readIndex = readIndex + 1;
  38. if (readIndex >= numReadings) readIndex = 0;
  39. colourValue = total / numReadings;
  40. outputColour();
  41. delay(25);
  42. }
  43.  
  44. void outputColour()
  45. {
  46. if (colourValue < 341) // Lowest third of the potentiometer's range (0-340)
  47. {
  48. colourValue = (colourValue * 3) / 4; // Normalize to 0-255
  49.  
  50. redVal = 256 - colourValue; // Red from full to off
  51. grnVal = colourValue; // Green from off to full
  52. bluVal = 1; // Blue off
  53. }
  54. else if (colourValue < 682) // Middle third of potentiometer's range (341-681)
  55. {
  56. colourValue = ( (colourValue-341) * 3) / 4; // Normalize to 0-255
  57.  
  58. redVal = 1; // Red off
  59. grnVal = 256 - colourValue; // Green from full to off
  60. bluVal = colourValue; // Blue from off to full
  61. }
  62. else // Upper third of potentiometer"s range (682-1023)
  63. {
  64. colourValue = ( (colourValue-683) * 3) / 4; // Normalize to 0-255
  65.  
  66. redVal = colourValue; // Red from off to full
  67. grnVal = 1; // Green off
  68. bluVal = 256 - colourValue; // Blue from full to off
  69. }
  70. analogWrite(redPin, redVal); // Write values to LED pins
  71. analogWrite(grnPin, grnVal);
  72. analogWrite(bluPin, bluVal);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement