Advertisement
Guest User

Untitled

a guest
May 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. // Controlling an LED strip with a radial potentiometer
  2. #include <Adafruit_NeoPixel.h>
  3. #ifdef __AVR__
  4. #include <avr/power.h>
  5. #endif
  6.  
  7. #define PIN A1
  8. #define NUMPIXELS 5
  9. int potPin = A0;
  10.  
  11. int val = 0;
  12. int writeval = 0;
  13.  
  14. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  15.  
  16. //Color values for the whole strip between 0 and 255
  17. int R = 100;
  18. int redMap = 0;
  19. int G = 100;
  20. int greenMap = 0;
  21. int B = 100;
  22. int blueMap = 0;
  23.  
  24. void setup() {
  25. pixels.begin();
  26. Serial.begin(9600);
  27.  
  28. for(int i=0; i<5; i++){
  29. pixels.setPixelColor(i, pixels.Color(R,0,0));
  30. }
  31. pixels.show();
  32. }
  33.  
  34. void loop() {
  35.  
  36. val = analogRead(potPin);
  37. writeval = map(val, 0, 1023, 0, 6);
  38. Serial.println(val);
  39.  
  40. if(val<206){ //Red stays 100, green goes from 0-100, blue stays 0
  41.  
  42. greenMap = map(val, 0, 205, 0, 100);
  43.  
  44. for(int i=0; i<5; i++){
  45. pixels.setPixelColor(i, pixels.Color(R,greenMap,0));
  46. }
  47. pixels.show();
  48.  
  49. }
  50.  
  51. else if(val<411 && val>205){ //Red goes from 100-0, green stays 100, blue stays 0
  52.  
  53. redMap = map(val, 206, 410, 100, 0);
  54.  
  55. for(int i=0; i<5; i++){
  56. pixels.setPixelColor(i, pixels.Color(redMap,G,0));
  57. }
  58. pixels.show();
  59.  
  60. }
  61.  
  62. else if(val<616 && val>410){ //Red stays 0, green goes from 100-0, blue goes from 0-100
  63.  
  64. greenMap = map(val, 411, 615, 100, 0);
  65. blueMap = map(val, 411, 615, 0, 100);
  66.  
  67. for(int i=0; i<5; i++){
  68. pixels.setPixelColor(i, pixels.Color(0,greenMap,blueMap));
  69. }
  70. pixels.show();
  71.  
  72. }
  73.  
  74. else if(val<821 && val>615){ //Red goes from 0-100, green stays 0, blue stays 100
  75.  
  76. redMap = map(val, 616, 820, 0, 100);
  77.  
  78. for(int i=0; i<5; i++){
  79. pixels.setPixelColor(i, pixels.Color(redMap,0,B));
  80. }
  81. pixels.show();
  82.  
  83. }
  84.  
  85. else{
  86. for(int i=0; i<5; i++){ //Red stays 100, green stays 0, blue goes from 100-0
  87.  
  88. blueMap = map(val, 821, 1023, 100, 0);
  89.  
  90. pixels.setPixelColor(i, pixels.Color(R,0,blueMap));
  91. }
  92. pixels.show();
  93.  
  94. }
  95.  
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement