Advertisement
Guest User

policeLight-NeoPixel

a guest
Apr 3rd, 2014
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. /*******************************
  2. * Arduino Police Lights Strobe Example
  3. * James C4S / www.cmiyc.com
  4. *
  5. * How to make lights strobe like police lights using millis() (no delay)
  6. *
  7. * http://www.cmiyc.com/tutorials/arduino-example-police-lights-with-millis
  8. *******************************/
  9.  
  10. #include <Adafruit_NeoPixel.h>
  11.  
  12. #define PIN 6
  13. Adafruit_NeoPixel strip = Adafruit_NeoPixel(40, PIN);
  14.  
  15. // English for which LED to Strobe
  16. #define RED 0x0
  17. #define BLUE 0x1
  18.  
  19. // Variable to track which LED is on
  20. byte whichLED = RED;
  21.  
  22. // State variables for the LEDs
  23. byte Red_State = LOW;
  24. byte Blue_State = LOW;
  25.  
  26. // Some delay values to change flashing behavior
  27. unsigned long switchDelay = 250;
  28. unsigned long strobeDelay = 50;
  29.  
  30. // Seed the initial wait for the strobe effect
  31. unsigned long strobeWait = strobeDelay;
  32.  
  33. // Variable to see when we should swtich LEDs
  34. unsigned long waitUntilSwitch = switchDelay; // seed initial wait
  35.  
  36. int numberOfLED = 40;
  37. int block1Count = 20;
  38. int block2Count = 20;
  39.  
  40. int block1[] = {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27,32,33,34,35};
  41. int block2[] = {4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31,36,37,38,39};
  42.  
  43. uint32_t red = strip.Color(255, 0, 0);
  44. uint32_t blue = strip.Color(0, 0, 255);
  45. uint32_t white = strip.Color(255, 255, 255);
  46. uint32_t black = strip.Color(0, 0, 0);
  47.  
  48.  
  49. void setup() {
  50. strip.begin();
  51. strip.setBrightness(50);
  52. strip.show(); // Initialize all pixels to 'off'
  53. }
  54.  
  55. void loop() {
  56. if (Red_State == LOW) {
  57. wipe(block1Count, block1, black) ;
  58. } else {
  59. wipe(block1Count, block1, red) ;
  60. }
  61. if (Blue_State == LOW) {
  62. wipe(block2Count, block2, black) ;
  63. } else {
  64. wipe(block2Count, block2, blue) ;
  65. }
  66. strip.show();
  67.  
  68. // Toggle back and forth between the two LEDs
  69. if ((long)(millis() - waitUntilSwitch)>=0) {
  70. // time is up!
  71. Red_State = LOW;
  72. Blue_State = LOW;
  73. whichLED = !whichLED; // toggle LED to strobe
  74. waitUntilSwitch += switchDelay;
  75. }
  76.  
  77. // Create the stobing effect
  78. if ((long)(millis() - strobeWait)>=0) {
  79. if (whichLED == RED)
  80. Red_State = !Red_State;
  81. if (whichLED == BLUE)
  82. Blue_State = !Blue_State;
  83. strobeWait += strobeDelay;
  84. }
  85. }
  86.  
  87. void wipe(int myCount, int myBlock[], uint32_t myColor) {
  88. for (int led = 0; led < myCount ; led++) {
  89. strip.setPixelColor(myBlock[led], myColor);
  90. }
  91. }
  92.  
  93. void wipeAll(int myCount, uint32_t myColor) {
  94. for (int led = 0; led < myCount ; led++) {
  95. strip.setPixelColor(led, myColor);
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement