Advertisement
Guest User

Untitled

a guest
Apr 12th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. // LIBRARIES
  2.  
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5. #include "Adafruit_NeoPixel.h"
  6.  
  7. // DEFINITIONS
  8.  
  9. #define STARTCOLOR 0x4800FF // LED color at startup 0xff8000 is orange in hex code. pick your own here: http://www.w3schools.com/colors/colors_picker.asp
  10. #define BLACK 0x000000 // LED color BLACK
  11.  
  12. #define DATAPIN 5 // Datapin
  13. #define LEDCOUNT 227 // Number of LEDs used in your system
  14. #define SHOWDELAY 20 // Delay in micro seconds before showing
  15. #define BAUDRATE 500000 // Serial port speed
  16.  
  17. #define BRIGHTNESS 90 // Max. brightness in %
  18.  
  19. const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0xE2, 0xD5}; // Prefix at the start of the transmission
  20. char buffer[sizeof(prefix)]; // Temporary buffer for receiving the prefix data
  21.  
  22. // to calculate your prefix, the first 4 bytes will never change: const char prefix[] = {0x41, 0x64, 0x61, 0x00, this never changes.
  23. // the next byte is equal to the number of LED - 1 --> (232-1)=231. 231 transformed in HEX. 231 in hex is E7 (use google)
  24. // the last byte is equal to the XOR value of the calculated value just above and 0x55 (byte just calculated (E7) XORED with 0x55) = B2 use this link http://xor.pw/? and in input 1 put 55 and in input 2 put your HEX value.
  25.  
  26. // some precalculated values to save some time: 178 leds gives B1 and E4, 180 B3E6, 181 B4E1, 182 B5E0 232 E7B2 230 E5B0
  27.  
  28. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, DATAPIN, NEO_GRB + NEO_KHZ800);
  29. LiquidCrystal_I2C lcd(0x20,16,2); // set the LCD address to 0x20 for a 16 chars and 2 line display
  30.  
  31. int state; // Define current state
  32. #define STATE_WAITING 1 // - Waiting for prefix
  33. #define STATE_DO_PREFIX 2 // - Processing prefix
  34. #define STATE_DO_DATA 3 // - Handling incoming LED colors
  35.  
  36. int readSerial; // Read Serial data (1)
  37. int currentLED; // Needed for assigning the color to the right LED
  38. const int led_button = 11; // ON/OFF button input
  39. const int status_led = 8; // LED of button
  40. const int ch_2 = A0; // Inputs from HDMI switcher
  41. const int ch_3 = A1;
  42. const int ch_4 = A2;
  43. const int ch_5 = A3;
  44.  
  45. int curr_source = 0; // Used to store which source is currently displayed
  46. boolean strip_status = true; // Used to chose if the LED strip is ON or OFF (depending on button choice)
  47. unsigned long timeout = 0; // Timeout used to turn LED strip off if no new data has come thorugh after some time
  48.  
  49. void setup()
  50. {
  51. pinMode(led_button, INPUT);
  52. pinMode(ch_2, INPUT);
  53. pinMode(ch_3, INPUT);
  54. pinMode(ch_4, INPUT);
  55. pinMode(ch_5, INPUT);
  56. pinMode(status_led, OUTPUT);
  57. digitalWrite(led_button, HIGH);
  58. digitalWrite(status_led, HIGH);
  59.  
  60. delay (15000); // 15 seconds delay at startup to avoid strange behaviours as the PI boots up etc
  61. strip.begin(); // Init LED strand, set all black, then all to startcolor
  62.  
  63. strip.setBrightness( (255 / 100) * 25 );
  64.  
  65. setAllLEDs(BLACK, 0);
  66. setAllLEDs(STARTCOLOR, 5); // Will turn ON all LEDS with a 5ms delay between each turn ON creating a snake increasing pattern
  67.  
  68. Serial.begin(BAUDRATE); // Init serial speed
  69.  
  70. state = STATE_WAITING; // Initial state: Waiting for prefix
  71.  
  72. lcd.init(); // Initialize the lcd
  73. lcd.backlight(); // Turn ON LCD backlight
  74. lcd.print(" waiting for PI"); // Wait for PI to boot up for 5 sec (avoids the Arduino rebooting randomly)
  75. delay(5000);
  76. lcd.clear();
  77. lcd.print("**** SOURCE ****");
  78. strip.setBrightness( (255 / 100) * BRIGHTNESS ); // Set the brightness we chose above
  79. }
  80.  
  81.  
  82. void loop()
  83. {
  84. do_strip(); // Main part of the code where we look at the data incoming from the PI
  85. if (state == STATE_WAITING) // Only if we are in WAITING state we check the source inputs and our ON/OFF button.
  86. {
  87. check_source();
  88. check_button();
  89. }
  90. }
  91.  
  92. void do_strip(void)
  93. {
  94. if (strip_status == true)
  95. {
  96. switch(state)
  97. {
  98. case STATE_WAITING: // *** Waiting for prefix ***
  99. if( Serial.available()>0 )
  100. {
  101. readSerial = Serial.read(); // Read one character
  102.  
  103. if ( readSerial == prefix[0] ) // if this character is 1st prefix char
  104. { state = STATE_DO_PREFIX; } // then set state to handle prefix
  105. }
  106. break;
  107.  
  108.  
  109. case STATE_DO_PREFIX: // *** Processing Prefix ***
  110. timeout = millis();
  111. if( Serial.available() > sizeof(prefix) - 2 )
  112. {
  113. Serial.readBytes(buffer, sizeof(prefix) - 1);
  114.  
  115. for( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++)
  116. {
  117. if( buffer[Counter] == prefix[Counter+1] )
  118. {
  119. state = STATE_DO_DATA; // Received character is in prefix, continue
  120. currentLED = 0; // Set current LED to the first one
  121. }
  122. else
  123. {
  124. state = STATE_WAITING; // Crap, one of the received chars is NOT in the prefix
  125. break; // Exit, to go back to waiting for the prefix
  126. } // end if buffer
  127. } // end for Counter
  128. } // end if Serial
  129. break;
  130.  
  131.  
  132. case STATE_DO_DATA: // *** Process incoming color data ***
  133. if( Serial.available() > 2 ) // if we receive more than 2 chars
  134. {
  135. Serial.readBytes( buffer, 3 ); // Abuse buffer to temp store 3 charaters
  136. strip.setPixelColor( currentLED++, buffer[0], buffer[1], buffer[2]); // and assing to LEDs
  137. }
  138.  
  139. if( currentLED > LEDCOUNT ) // Reached the last LED? Display it!
  140. {
  141. strip.show(); // Make colors visible
  142. delayMicroseconds(SHOWDELAY); // Wait a few micro seconds
  143.  
  144. state = STATE_WAITING; // Reset to waiting ...
  145. currentLED = 0; // and go to LED one
  146.  
  147. break; // and exit ... and do it all over again
  148. }
  149. break;
  150. } // switch(state)
  151. } // if statement
  152. else
  153. {setAllLEDs(BLACK, 0);}
  154.  
  155. if(millis() > timeout + 5000) // If no new incoming data after 5seconds, turn the strip OFF.
  156. {setAllLEDs(BLACK, 0);}
  157. } // do_strip
  158.  
  159. void check_source (void)
  160. {
  161. if(digitalRead(ch_2) == HIGH)
  162. {
  163. if(curr_source != 2)
  164. {
  165. lcd.setCursor(0,1);
  166. curr_source = 2;
  167. lcd.print(" APPLE TV "); // Channel 2 of HDMI switcher LCD name
  168. }
  169. }
  170. else if(digitalRead(ch_3) == HIGH)
  171. {
  172. if(curr_source != 3)
  173. {
  174. lcd.setCursor(0,1);
  175. curr_source = 3;
  176. lcd.print(" FIRESTICK "); // Channel 3 of HDMI switcher LCD name
  177. }
  178. }
  179. else if(digitalRead(ch_4) == HIGH)
  180. {
  181. if(curr_source != 4)
  182. {
  183. lcd.setCursor(0,1);
  184. curr_source = 4;
  185. lcd.print(" OPEN "); // Channel 4 of HDMI switcher LCD name
  186. }
  187. }
  188. else if(digitalRead(ch_5) == HIGH)
  189. {
  190. if(curr_source != 5)
  191. {
  192. lcd.setCursor(0,1);
  193. curr_source = 5;
  194. lcd.print(" VGA "); // Channel 5 of HDMI switcher LCD name
  195. }
  196. }
  197. else
  198.  
  199.  
  200. {
  201. if(curr_source != 1)
  202. {
  203. lcd.setCursor(0,1);
  204. curr_source = 1;
  205. lcd.print(" A/V "); // Channel 1 of HDMI switcher LCD name
  206. }
  207. }
  208. }
  209.  
  210. void check_button (void) // ON/OFF button routine
  211. {
  212. if(digitalRead(led_button) == LOW)
  213. {
  214. if(strip_status == true)
  215. {
  216. strip_status = false;
  217. digitalWrite(status_led, LOW);
  218. }
  219. else
  220. {
  221. strip_status = true;
  222. digitalWrite(status_led, HIGH);
  223. }
  224. delay(25);
  225. while(digitalRead(led_button) == LOW)
  226. {}
  227. }
  228. }
  229.  
  230. // Sets the color of all LEDs in the strip
  231. // If 'wait'>0 then it will show a swipe from start to end
  232. void setAllLEDs(uint32_t color, int wait)
  233. {
  234. for ( int Counter=0; Counter < LEDCOUNT; Counter++ ) // For each LED
  235. {
  236. strip.setPixelColor( Counter, color ); // .. set the color
  237.  
  238. if( wait > 0 ) // if a wait time was set then
  239. {
  240. strip.show(); // Show the LED color
  241. delay(wait); // and wait before we do the next LED
  242. } // if wait
  243.  
  244. } // for Counter
  245.  
  246. strip.show(); // Show all LEDs
  247. } // setAllLEDs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement