Advertisement
Guest User

wchill

a guest
Oct 22nd, 2010
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.60 KB | None | 0 0
  1. // POV code 3.01 BETA BlinkM version
  2. // Completely untested on real hardware
  3. // CHANGELOG:
  4. // 3.01 BETA Oops, forgot to include some of the code! Fixed
  5. // Little more cleanup
  6. // Going to add serial support, probably in 3.10 or higher.
  7. // 3.00 BETA Now with support for saving up to 16 RGB color mixes
  8. // Support for up to 16 custom characters
  9. // Status LED added to design
  10. // 2.10 Cleaned up some more
  11. // 2.00 Ultra fix, POV should work properly, lot of bad code replaced
  12. // Also added BlinkM extra options and POV timing options
  13. // 1.20 Cleanup, still pretty crappy code in my opinion
  14. // 1.10 Fixed critical POV display bug
  15. // 1.00 Initial crappy release
  16.  
  17. // TODO:
  18. // More cleanup
  19. // Incorporate serial programming methods
  20. // Test the code out on the real thing!
  21.  
  22. /*
  23. You need:
  24.  ATMega328 chip
  25.  16MHz crystal oscillator
  26.  8 LEDs
  27.  8-switch DIP switch
  28.  8 100-ohm resistors (for 3.2V forward voltage LED)
  29.  3 push buttons
  30.  3-way switch (on-on)
  31.  10k-ohm resistor (plus optional button for reset)
  32.  TPIC6A595 or comparable shift register
  33.  BlinkM
  34.  
  35.  Connect DIP switch to digital pins 5-8/Analog In 0-3 and ground
  36.  Connect pushbuttons to digital pins 2-4 and ground
  37.  Connect 3-way switch like this:
  38.  5V - SYS - GND
  39.  
  40.  Connect TPIC6A595 registers:
  41.  dataPin (12) connects to SERIN
  42.  clockPin (11) connects to SRCK
  43.  latchPin (10) connects to RCK
  44.  
  45.  BlinkM should be connected to Analog In 4/5, 5V and GND.
  46.  
  47.  Refer to wiring diagram for wiring details.
  48.  
  49.  */
  50.  
  51. // Let's include the libraries we'll need
  52. // This includes switch debouncing, EEPROM
  53. // read/write access, I2C library, and
  54. // specialized BlinkM functions
  55. #include <Bounce.h>
  56. #include <EEPROM.h>
  57. #include <Wire.h>
  58. #include <BlinkM_funcs.h>
  59.  
  60. // Character set - A-Z, a-z, 0-9, some symbols
  61. const byte POVA[] ={
  62.   254, 25, 25, 254
  63. };
  64. const byte POVB[] ={
  65.   255, 153, 153, 118
  66. };
  67. const byte POVC[] ={
  68.   126, 129, 129, 66
  69. };
  70. const byte POVD[] ={
  71.   255, 129, 129, 126
  72. };
  73. const byte POVE[] ={
  74.   255, 137, 137, 137
  75. };
  76. const byte POVF[] ={
  77.   255, 9, 9, 9
  78. };
  79. const byte POVG[] ={
  80.   126, 129, 145, 241
  81. };
  82. const byte POVH[] ={
  83.   255, 24, 24, 255
  84. };
  85. const byte POVI[] ={
  86.   129, 255, 255, 129
  87. };
  88. const byte POVJ[] ={
  89.   24, 129, 129, 127
  90. };
  91. const byte POVK[] ={
  92.   255, 24, 24, 231
  93. };
  94. const byte POVL[] ={
  95.   255, 128, 128, 128
  96. };
  97. const byte POVM[] ={
  98.   255, 2, 12, 2, 255
  99. };
  100. const byte POVN[] ={
  101.   255, 6, 24, 96, 255
  102. };
  103. const byte POVO[] ={
  104.   126, 129, 129, 126
  105. };
  106. const byte POVP[] ={
  107.   255, 9, 9, 6
  108. };
  109. const byte POVQ[] ={
  110.   126, 129, 129, 255
  111. };
  112. const byte POVR[] ={
  113.   255, 9, 25, 230
  114. };
  115. const byte POVS[] ={
  116.   134, 137, 137, 131
  117. };
  118. const byte POVT[] ={
  119.   1, 255, 255, 1
  120. };
  121. const byte POVU[] ={
  122.   127, 128, 128, 127
  123. };
  124. const byte POVV[] ={
  125.   63, 192, 192, 63
  126. };
  127. const byte POVW[] ={
  128.   127, 128, 112, 128, 127
  129. };
  130. const byte POVX[] ={
  131.   231, 24, 24, 231
  132. };
  133. const byte POVY[] ={
  134.   15, 240, 240, 15
  135. };
  136. const byte POVZ[] ={
  137.   225, 145, 137, 135
  138. };
  139. const byte POVAL[] ={
  140.   64, 168, 168, 112
  141. };
  142. const byte POVBL[] ={
  143.   248, 160, 160, 64
  144. };
  145. const byte POVCL[] ={
  146.   112, 136, 136, 80
  147. };
  148. const byte POVDL[] ={
  149.   64,160,160,120
  150. };
  151. const byte POVEL[] ={
  152.   112,168,168,176
  153. };
  154. const byte POVFL[] ={
  155.   32,240,40,40
  156. };
  157. const byte POVGL[] ={
  158.   144,168,168,120
  159. };
  160. const byte POVHL[] ={
  161.   248,32,32,192
  162. };
  163. const byte POVIL[] ={
  164.   232,232
  165. };
  166. const byte POVJL[] ={
  167.   64,128,128,104
  168. };
  169. const byte POVKL[] ={
  170.   248,64,96,144
  171. };
  172. const byte POVLL[] ={
  173.   248,128
  174. };
  175. const byte POVML[] ={
  176.   240,8,48,8,240
  177. };
  178. const byte POVNL[] ={
  179.   248,8,8,240    
  180. };
  181. const byte POVOL[] ={
  182.   112,136,136,112
  183. };
  184. const byte POVPL[] ={
  185.   248,40,40,16  
  186. };
  187. const byte POVQL[] ={
  188.   16,40,40,248
  189. };
  190. const byte POVRL[] ={
  191.   248,16,8,8
  192. };
  193. const byte POVSL[] ={
  194.   144,168,168,72
  195. };
  196. const byte POVTL[] ={
  197.   16,120,144
  198. };
  199. const byte POVUL[] ={
  200.   120,128,128,248
  201. };
  202. const byte POVVL[] ={
  203.   120,192,120
  204. };
  205. const byte POVWL[] ={
  206.   120,128,96,128,120
  207. };
  208. const byte POVXL[] ={
  209.   216,32,216
  210. };
  211. const byte POVYL[] ={
  212.   152,160,120
  213. };
  214. const byte POVZL[] ={
  215.   136,200,168,152,136
  216. };
  217. const byte POV0[] ={
  218.   126,225,153,135,126
  219. };
  220. const byte POV1[] ={
  221.   132,130,255,128
  222. };
  223. const byte POV2[] ={
  224.   194,161,145,142
  225. };
  226. const byte POV3[] ={
  227.   137,137,137,118
  228. };
  229. const byte POV4[] ={
  230.   24,20,18,255
  231. };
  232. const byte POV5[] ={
  233.   79,137,137,113
  234. };
  235. const byte POV6[] ={
  236.   126,145,145,98
  237. };
  238. const byte POV7[] ={
  239.   193,49,9,7
  240. };
  241. const byte POV8[] ={
  242.   118,137,137,118
  243. };
  244. const byte POV9[] ={
  245.   78,145,145,126
  246. };
  247. const byte POVSP[] ={
  248.   0
  249. };
  250. const byte POVcolon[] ={
  251.   102, 102
  252. };
  253. const byte POVsemicolon[] ={
  254.   70, 54
  255. };
  256. const byte POVquestion[] ={
  257.   2, 177, 9, 6
  258. };
  259. const byte POVdash[] ={
  260.   24, 24, 24, 24
  261. };
  262. const byte POVperiod[] ={
  263.   192, 192
  264. };
  265. const byte POVexclamation[] ={
  266.   223, 223
  267. };
  268.  
  269. byte *POVC0;
  270. byte *POVC1;
  271. byte *POVC2;
  272. byte *POVC3;
  273. byte *POVC4;
  274. byte *POVC5;
  275. byte *POVC6;
  276. byte *POVC7;
  277. byte *POVC8;
  278. byte *POVC9;
  279. byte *POVC10;
  280. byte *POVC11;
  281. byte *POVC12;
  282. byte *POVC13;
  283. byte *POVC14;
  284. byte *POVC15;
  285.  
  286. // There's space for more characters in the future, up to 256.
  287. // Will eventually add support for custom characters.
  288. const char charTable[256] ={
  289.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.?!:;-                "
  290.     // dash is charTable[67], custom characters are 68-83 for total of 16 custom characters
  291. };
  292.  
  293. // Program default text before you input a message
  294. // Max 64 characters
  295. // A message is required.
  296. const char defaultText[] = {
  297.   "POV Wand 1.0"
  298. };
  299.  
  300.  
  301. // Define constants here
  302. #define DATAPIN 12
  303. #define CLOCKPIN 11
  304. #define LATCHPIN 10
  305.  
  306. // Note: 0x00 means general call in I2C
  307. // It will call ALL devices on the I2C bus
  308. #define BLINKM_ADDR 0x00
  309.  
  310. // Connect pushbuttons to pins 2,3,4
  311. // No pullup/pulldown resistor
  312.  
  313. byte textPOV[384]; // Max 64 characters
  314. byte charPOV[64];
  315. byte POVlength = 0;
  316. int POVBlength = 0;
  317. byte RGBState = 0;
  318. byte RGB[] = {
  319.   255,255,255
  320. };
  321. byte columnDelay = 2; // set delay between column flashes in ms
  322. byte wordDelay = 8;  // set delay between words in ms
  323.  
  324. Bounce button2 = Bounce( 2,5 );
  325. Bounce button3 = Bounce( 3,5 );
  326. Bounce button4 = Bounce( 4,5 );
  327.  
  328. void setup() {
  329.   byte temppov[] = {1,2,3,4,5};
  330.   // For debugging purposes
  331.   Serial.begin(9600);
  332.   Serial.println("Setting up");
  333.  
  334.   // We set shift register pins to output for SPI communication
  335.   pinMode(DATAPIN, OUTPUT);
  336.   pinMode(CLOCKPIN, OUTPUT);
  337.   pinMode(LATCHPIN, OUTPUT);
  338.  
  339.   // Set pins to input state and activate pullup resistors
  340.   // This will allow us to read the DIP switch/pushbuttons
  341.   for (int i = 2; i < 9; i++) {
  342.     pinMode(i,INPUT);
  343.     digitalWrite(i,HIGH);
  344.   }
  345.   for (int i = 14; i < 18; i++) {
  346.     pinMode(i,INPUT);
  347.     digitalWrite(i,HIGH);
  348.   }
  349.  
  350. writeTextToEEPROM(temppov,5);
  351.  
  352.   // Attempt to read saved POV text from EEPROM
  353.   byte temp = readTextFromEEPROM();
  354.  
  355.   // If the EEPROM is empty, load the default text
  356.   if (temp == 0) {
  357.     temp = loadDefaultText();
  358.   }
  359.   POVlength = temp;
  360.   // Update button states
  361.   button2.update();
  362.   button3.update();
  363.   button4.update();
  364.  
  365.   // Initialize BlinkM
  366.   BlinkM_begin();
  367.   BlinkM_stopScript(0x00);
  368.   BlinkM_fadeToRGB(0x00,0,0,0);
  369.   for(int i = 0; i < 8; i++){
  370.   writeToRegister(0);
  371.   }
  372.  
  373.   // Set up custom character arrays
  374.   // We read the custom character's size and allocate
  375.   // the needed memory using malloc
  376.   // We then store the character data into the arrays
  377.   POVC0 = (byte *)malloc(readCharSize(1));
  378.   POVC1 = (byte *)malloc(readCharSize(2));
  379.   POVC2 = (byte *)malloc(readCharSize(3));
  380.   POVC3 = (byte *)malloc(readCharSize(4));
  381.   POVC4 = (byte *)malloc(readCharSize(5));
  382.   POVC5 = (byte *)malloc(readCharSize(6));
  383.   POVC6 = (byte *)malloc(readCharSize(7));
  384.   POVC7 = (byte *)malloc(readCharSize(8));
  385.   POVC8 = (byte *)malloc(readCharSize(9));
  386.   POVC9 = (byte *)malloc(readCharSize(10));
  387.   POVC10 = (byte *)malloc(readCharSize(11));
  388.   POVC11 = (byte *)malloc(readCharSize(12));
  389.   POVC12 = (byte *)malloc(readCharSize(13));
  390.   POVC13 = (byte *)malloc(readCharSize(14));
  391.   POVC14 = (byte *)malloc(readCharSize(15));
  392.   byte *POVC0 = readCharData(1, POVC0);
  393.   byte *POVC1 = readCharData(2, POVC1);
  394.   byte *POVC2 = readCharData(3, POVC2);
  395.   byte *POVC3 = readCharData(4, POVC3);
  396.   byte *POVC4 = readCharData(5, POVC4);
  397.   byte *POVC5 = readCharData(6, POVC5);
  398.   byte *POVC6 = readCharData(7, POVC6);
  399.   byte *POVC7 = readCharData(8, POVC7);
  400.   byte *POVC8 = readCharData(9, POVC8);
  401.   byte *POVC9 = readCharData(10, POVC9);
  402.   byte *POVC10 = readCharData(11, POVC10);
  403.   byte *POVC11 = readCharData(12, POVC11);
  404.   byte *POVC12 = readCharData(13, POVC12);
  405.   byte *POVC13 = readCharData(14, POVC13);
  406.   byte *POVC14 = readCharData(15, POVC14);
  407.   byte *POVC15 = readCharData(16, POVC15);
  408.  
  409.   delay(100);
  410.   for(int i = 0; i < 256; i++) {
  411.     Serial.println(i,DEC);
  412.     writeToRegister(i);
  413.     delay(100);
  414.   }
  415. }
  416.  
  417. void loop()
  418. {
  419. /*  // Is a pushbutton pressed?
  420. button2.update();
  421. button3.update();
  422. button4.update();
  423.   // Switch to programming mode
  424.   if (button2.read() == LOW)
  425.     programText();
  426.  
  427.   // Switch to BlinkM flashlight
  428.   if (button3.read() == LOW)
  429.     BlinkM();
  430.  
  431.   // Adjust delay speed
  432.   if (button4.read() == LOW)
  433.     delaySpeed();
  434. */
  435.   Serial.println("Printing word");
  436.   // If not, run POV
  437.   //printWord(textPOV);
  438.   writeToRegister(0);
  439.   delay(5);
  440.   //delay(wordDelay); //Wait a little bit between each display of the word
  441. }
  442.  
  443. // Start BlinkM flashlight
  444. void BlinkM() {
  445.   Serial.println("BlinkM mode");
  446.   // Initialize variables
  447.   unsigned long colordata = 0;
  448.  
  449.   // Turn off all the POV LEDs
  450.   writeToRegister(0);
  451.  
  452.   // Set up BlinkM speed
  453.   BlinkMSetSpeed();
  454.  
  455.   // Let's set up the BlinkM's color
  456.   // Set script to default - no color
  457.   byte scriptnum = 0;
  458.   BlinkM_playScript(BLINKM_ADDR,scriptnum,0,0 );
  459.  
  460.   // Read color data from EEPROM
  461.   colordata = readColorFromEEPROM(DIPInput() & B1111);
  462.   RGB[0] = colordata & 16711680;
  463.   RGB[1] = colordata & 65280;
  464.   RGB[2] = colordata & 255;
  465.   // Send it a 3 byte RGB color value
  466.   BlinkM_fadeToRGB(BLINKM_ADDR,RGB[0],RGB[1],RGB[2]);
  467.  
  468.   // Wait for user to release the mode switch button
  469.   while (button3.read() == LOW){
  470.     button3.update();
  471.   }
  472.  
  473.   // Main BlinkM loop
  474.   // Executes until mode switch button is pressed
  475.   // It will then return to the main POV loop
  476.   do {
  477.     // Update button states
  478.     button2.update();
  479.     button3.update();
  480.     button4.update();
  481.  
  482.     // Are any buttons pressed?
  483.     // Program button
  484.     if (button2.read() == LOW)
  485.       programRGB();
  486.  
  487.     // Light script switch button
  488.     if (button4.read() == LOW) {
  489.       BlinkMSetSpeed();
  490.       BlinkM_playScript(BLINKM_ADDR,scriptnum,0,0);
  491.       scriptnum++;
  492.       if (scriptnum > 18)
  493.         scriptnum = 0;
  494.     }
  495.  
  496.     // Wait for button to be released
  497.     while (button2.read() == LOW || button3.read() == LOW || button4.read() == LOW) {
  498.       button2.update();
  499.       button3.update();
  500.       button4.update();
  501.     }
  502.   }
  503.   // Keep looping until mode switch button is pressed
  504.   while (button3.read() == HIGH);
  505.  
  506.   // Let's turn off the BlinkM
  507.   BlinkM_stopScript(BLINKM_ADDR);
  508.   BlinkM_changeRGB(0,0,0,false);
  509.  
  510.   // Return to POV code
  511.   return;
  512. }
  513.  
  514. // Program new text for POV
  515. // If mode switch button is pressed, read DIP switches and change text accordingly
  516. // Press programming button again to exit
  517. void programText() {
  518.  
  519.   // Initialize our variables
  520.   byte num = 0;
  521.   byte temp = 0;
  522.   POVlength = 0;
  523.   POVBlength = 0;
  524.  
  525.   // Clear the POV LEDs
  526.   writeToRegister(0);
  527.  
  528.   // Let's clear the button state
  529.   while (button2.read() == LOW){
  530.     button2.update();
  531.   }
  532.  
  533.   if (DIPInput() & B11110000 == 16) {
  534.     byte temp2 = 0;
  535.     byte temp3[8];
  536.     num = DIPInput() & B1111 + 1;
  537.     do {
  538.       button3.update();
  539.       if (button3.read() == LOW) {
  540.         temp = DIPInput();
  541.  
  542.         // Show which character was saved
  543.         writeToRegister(temp);
  544.         temp3[temp2] = temp;
  545.         temp2++;
  546.       }
  547.     }
  548.     while (button4.read() == HIGH && temp2 < 8);
  549.     writeCharData(num,temp3);
  550.     return;
  551.   }
  552.  
  553.   // wait for button press, then read DIP switch and save data
  554.   do {
  555.     button3.update();
  556.     if (button3.read() == LOW) {
  557.       temp = DIPInput();
  558.  
  559.       // Does the input exceed character database limits?
  560.       // If so, fix it to a space
  561.       if (temp > sizeof(charTable))
  562.         temp = 0;
  563.  
  564.       // Lookup the character
  565.       charPOV[POVlength] = charTable[temp];
  566.       charLookup(charTable[temp]);
  567.       POVlength++;
  568.       // Show which character was saved
  569.       writeToRegister(temp);
  570.     }
  571.     // Update the button state
  572.     button2.update();
  573.   }  
  574.  
  575.   // Keep going until text length is 64 or user exits mode
  576.   while (button2.read() == HIGH && POVlength < 64);
  577.  
  578.   // Write the data to EEPROM
  579.   writeTextToEEPROM(charPOV,POVlength);
  580.   return;
  581. }
  582.  
  583. // Display the POV text
  584. void printWord(byte wordVar[]) {
  585.   for(int i = 0; i < sizeof(wordVar); i++) {
  586.     //writeToRegister(wordVar[i]);
  587.     delay(5);
  588.     //delay(columnDelay);
  589.     Serial.println("printWord running");
  590.   }
  591. }
  592.  
  593. // Write POV data to EEPROM
  594. void writeTextToEEPROM(byte wordVar[], byte length) {
  595.  
  596.   // Save delay information
  597.   EEPROM.write(0,wordDelay);
  598.   EEPROM.write(1,columnDelay);
  599.   length += 1;
  600.   for(int i = 1; i < length; i++) {
  601.     EEPROM.write(i+2,wordVar[i]);
  602.   }
  603.  
  604.   // Save length information
  605.   EEPROM.write(2,length);
  606. }
  607.  
  608. // Read saved text from EEPROM
  609. byte readTextFromEEPROM() {
  610.  
  611.   // Initialize variables
  612.   byte temp = 0;
  613.   byte length = 0;
  614.   byte size = 0;
  615.   POVlength = 0;
  616.   POVBlength = 0;
  617.  
  618.   // If length = 0, we'll load default text
  619.   if (EEPROM.read(2) == 0)
  620.     return 0;
  621.  
  622.   // Load delay/length information
  623.   wordDelay = EEPROM.read(0);
  624.   columnDelay = EEPROM.read(1);
  625.   length = EEPROM.read(2);
  626.  
  627.   // Read and save data
  628.   for (int i = 1; i < length; i++) {
  629.     // Do we have enough space? If not, increase the size of textPOV
  630.     if (sizeof(textPOV) < POVBlength + 9) {
  631.       realloc(textPOV, sizeof(textPOV) + 9);
  632.     }
  633.     temp = EEPROM.read(i+2);
  634.     charPOV[POVlength] = charTable[temp];
  635.     charLookup(charTable[temp]);
  636.     POVlength++;
  637.   }
  638.   return length;
  639. }
  640.  
  641. // Get custom character size from EEPROM
  642. byte readCharSize(byte num) {
  643.   return EEPROM.read(175 + num);
  644. }
  645.  
  646. // Read custom character data from EEPROM
  647. byte *readCharData(byte num, byte cdata[]) {
  648.   byte temp = readCharSize(num);
  649.   for (int i = 0; i < temp; i++){
  650.     cdata[i] = EEPROM.read(176 + 8 * num + i);
  651.   }
  652.   return cdata;
  653. }
  654.  
  655. // Stores custom character data to EEPROM
  656. void writeCharData(byte num, byte cdata[]) {
  657.   EEPROM.write(175 + num, sizeof(cdata));
  658.   for (int i = 0; i < sizeof(cdata); i++) {
  659.     EEPROM.write(176 + 8 * num + i, cdata[i]);
  660.   }
  661. }
  662.  
  663. // Uses SPI to clock in data, then latches to set LED on/off
  664. void writeToRegister(byte data) {
  665.   Serial.println("writing");
  666.   digitalWrite(LATCHPIN, LOW);
  667.   shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, data);
  668.   digitalWrite(LATCHPIN, HIGH);
  669. }
  670.  
  671. // If EEPROM is empty, load default text
  672. byte loadDefaultText() {
  673.  
  674.   // Initialize variables
  675.   byte temp = 0;
  676.   POVlength = 0;
  677.   POVBlength = 0;
  678.  
  679.   // Let's copy defaultText into the POV buffer
  680.   realloc(charPOV, sizeof(defaultText));
  681.   memcpy(charPOV, defaultText, sizeof(defaultText));
  682.   POVlength = sizeof(charPOV);
  683.  
  684.   // Convert the POV buffer into the binary LED states
  685.   for(int i = 0; i < POVlength; i++) {
  686.     if (sizeof(textPOV) < POVBlength + 6) {
  687.       realloc(textPOV, sizeof(textPOV) + 6);
  688.     }
  689.     charLookup(charPOV[i]);
  690.   }
  691. }
  692.  
  693. // Change BlinkM's RGB color
  694. void programRGB() {
  695.   do {
  696.     // Update the button
  697.     button3.update();
  698.  
  699.     // To completely change the color, you need to press the pushbutton 3 times
  700.     // and adjust DIP switches 3 times
  701.     // Wait for button press, if pressed then save + update color information
  702.     if (button3.read() == LOW) {
  703.       RGB[RGBState] = DIPInput();
  704.       RGBState++;
  705.       if (RGBState >= 3) {
  706.         RGBState = 0;
  707.         writeColorToEEPROM(RGB[0],RGB[1],RGB[2]);
  708.       }
  709.       BlinkM_changeRGB(RGB[0],RGB[1],RGB[2],true);
  710.     }
  711.  
  712.     // Update button state
  713.     button2.update();
  714.   }  
  715.  
  716.   // Keep looping until programming button pressed
  717.   while (button2.read() == HIGH);
  718.   return;
  719. }
  720.  
  721. // Reads the DIP switch for 1 byte input
  722. byte DIPInput() {
  723.  
  724.   // Initialize variables
  725.   byte readDIP = 0;
  726.  
  727.   // Every read, if on, add 1
  728.   // Then bitshift left by 1
  729.   for (int i = 5;i < 9; i++) {
  730.     if (digitalRead(i) == LOW)
  731.       readDIP += 1;
  732.     readDIP = readDIP << 1;
  733.   }
  734.   for (int i = 14; i < 18; i++) {
  735.     if (digitalRead(i) == LOW)
  736.       readDIP += 1;
  737.     readDIP = readDIP << 1;
  738.   }
  739.  
  740.   return readDIP;
  741. }
  742.  
  743. // Set POV delay speed
  744. void delaySpeed() {
  745.  
  746.   // Clear POV LEDs
  747.   writeToRegister(0);
  748.  
  749.   // Wait for user to release button
  750.   while (button4.read() == LOW){
  751.     button4.update();
  752.   }
  753.  
  754.   // Get input from switches and save
  755.   byte temp = DIPInput();
  756.   wordDelay = temp >> 4;
  757.   columnDelay = temp & B00001111;
  758.  
  759.   // Save delay information to EEPROM
  760.   EEPROM.write(0,wordDelay);
  761.   EEPROM.write(1,columnDelay);
  762. }
  763.  
  764. // This sets fade speed and time adjustment on the BlinkM
  765. void BlinkMSetSpeed() {
  766.   // initialize our variables
  767.   byte dip = 0;
  768.   byte fade = 0;
  769.   byte time = 0;
  770.  
  771.   // Read information from DIP switches
  772.   dip = DIPInput();
  773.  
  774.   // Map our 4 bit inputs (0-127) to 8 bit values (0-255)
  775.   fade = map((dip & B11110000) << 3, 0, 15, 0, 255);
  776.   time = map((dip & B00001111) << 1, 0, 15, 0, 255);
  777.  
  778.   // Adjust fade speed and time adjustment
  779.   BlinkM_setFadeSpeed(BLINKM_ADDR, fade);
  780.   BlinkM_setTimeAdj(BLINKM_ADDR, time);
  781. }
  782.  
  783. void BlinkM_changeRGB(byte red, byte blue, byte green, boolean fadeon) {
  784.   if (fadeon == true) {
  785.     BlinkM_fadeToRGB(BLINKM_ADDR,red,blue,green);
  786.   }
  787.   else {
  788.     BlinkM_setRGB(BLINKM_ADDR,red,blue,green);
  789.   }
  790. }
  791.  
  792. unsigned long readColorFromEEPROM(byte num) {
  793.   if (EEPROM.read(127)==0) {
  794.     EEPROM.write(128,255);
  795.     EEPROM.write(129,255);
  796.     EEPROM.write(130,255);
  797.     EEPROM.write(127,1);
  798.   }
  799.   if (num == 0) return EEPROM.read(127);
  800.   if (EEPROM.read(127) < num) return (EEPROM.read(127)<<24)+16777216;
  801.   return (EEPROM.read(127)<<24)|(EEPROM.read(125 + 3 * num)<<16)|(EEPROM.read(126 + 3 * num)<<8)|EEPROM.read(127 + 3 * num);
  802. }
  803.  
  804. void writeColorToEEPROM(byte red, byte blue, byte green) {
  805.   byte temp = EEPROM.read(127);
  806.   if (temp == 16) temp--;
  807.   for (int i = 15; i > 0; i--) {
  808.     EEPROM.write(128 + 3 * i, EEPROM.read(125 + 3 * i));
  809.     EEPROM.write(129 + 3 * i, EEPROM.read(126 + 3 * i));
  810.     EEPROM.write(130 + 3 * i, EEPROM.read(127 + 3 * i));
  811.   }
  812.   EEPROM.write(128,red);
  813.   EEPROM.write(129,blue);
  814.   EEPROM.write(130,green);
  815.   temp++;
  816.   EEPROM.write(127,temp);
  817. }
  818.  
  819. // Blink the status LED on pin 13
  820. void blinkLED(byte blinkDelay) {
  821.   // The first time the function is called, blinkStatus will be set to 0
  822.   static unsigned long blinkStatus;
  823.   if (millis() > abs(blinkStatus) + blinkDelay) {
  824.     if (blinkStatus < 0) {
  825.       digitalWrite(13,LOW);
  826.       blinkStatus = millis();
  827.     }
  828.     else {
  829.       digitalWrite(13,HIGH);
  830.       blinkStatus = -millis();
  831.     }
  832.   }
  833. }
  834.  
  835. // Simply lookup the character and return the LED flash placement data
  836. void charLookup(char inputchar) {
  837.   switch (inputchar) {
  838.   case 'A':
  839.     for (int i=0; i < sizeof(POVA); i++) {
  840.       textPOV[POVBlength] = POVA[i];
  841.       POVBlength++;
  842.     }
  843.     break;
  844.   case 'B':
  845.     for (int i=0; i < sizeof(POVB); i++) {
  846.       textPOV[POVBlength] = POVB[i];
  847.       POVBlength++;
  848.     }
  849.     break;
  850.   case 'C':
  851.     for (int i=0; i < sizeof(POVC); i++) {
  852.       textPOV[POVBlength] = POVC[i];
  853.       POVBlength++;
  854.     }
  855.     break;
  856.   case 'D':
  857.     for (int i=0; i < sizeof(POVD); i++) {
  858.       textPOV[POVBlength] = POVD[i];
  859.       POVBlength++;
  860.     }
  861.     break;
  862.   case 'E':
  863.     for (int i=0; i < sizeof(POVE); i++) {
  864.       textPOV[POVBlength] = POVE[i];
  865.       POVBlength++;
  866.     }
  867.     break;
  868.   case 'F':
  869.     for (int i=0; i < sizeof(POVF); i++) {
  870.       textPOV[POVBlength] = POVF[i];
  871.       POVBlength++;
  872.     }
  873.     break;
  874.   case 'G':
  875.     for (int i=0; i < sizeof(POVG); i++) {
  876.       textPOV[POVBlength] = POVG[i];
  877.       POVBlength++;
  878.     }
  879.     break;
  880.   case 'H':
  881.     for (int i=0; i < sizeof(POVH); i++) {
  882.       textPOV[POVBlength] = POVH[i];
  883.       POVBlength++;
  884.     }
  885.     break;
  886.   case 'I':
  887.     for (int i=0; i < sizeof(POVI); i++) {
  888.       textPOV[POVBlength] = POVI[i];
  889.       POVBlength++;
  890.     }
  891.     break;
  892.   case 'J':
  893.     for (int i=0; i < sizeof(POVJ); i++) {
  894.       textPOV[POVBlength] = POVJ[i];
  895.       POVBlength++;
  896.     }
  897.     break;
  898.   case 'K':
  899.     for (int i=0; i < sizeof(POVK); i++) {
  900.       textPOV[POVBlength] = POVK[i];
  901.       POVBlength++;
  902.     }
  903.     break;
  904.   case 'L':
  905.     for (int i=0; i < sizeof(POVL); i++) {
  906.       textPOV[POVBlength] = POVL[i];
  907.       POVBlength++;
  908.     }
  909.     break;
  910.   case 'M':
  911.     for (int i=0; i < sizeof(POVM); i++) {
  912.       textPOV[POVBlength] = POVM[i];
  913.       POVBlength++;
  914.     }
  915.     break;
  916.   case 'N':
  917.     for (int i=0; i < sizeof(POVN); i++) {
  918.       textPOV[POVBlength] = POVN[i];
  919.       POVBlength++;
  920.     }
  921.     break;
  922.   case 'O':
  923.     for (int i=0; i < sizeof(POVO); i++) {
  924.       textPOV[POVBlength] = POVO[i];
  925.       POVBlength++;
  926.     }
  927.     break;
  928.   case 'P':
  929.     for (int i=0; i < sizeof(POVP); i++) {
  930.       textPOV[POVBlength] = POVP[i];
  931.       POVBlength++;
  932.     }
  933.     break;
  934.   case 'Q':
  935.     for (int i=0; i < sizeof(POVQ); i++) {
  936.       textPOV[POVBlength] = POVQ[i];
  937.       POVBlength++;
  938.     }
  939.     break;
  940.   case 'R':
  941.     for (int i=0; i < sizeof(POVR); i++) {
  942.       textPOV[POVBlength] = POVR[i];
  943.       POVBlength++;
  944.     }
  945.     break;
  946.   case 'S':
  947.     for (int i=0; i < sizeof(POVS); i++) {
  948.       textPOV[POVBlength] = POVS[i];
  949.       POVBlength++;
  950.     }
  951.     break;
  952.   case 'T':
  953.     for (int i=0; i < sizeof(POVT); i++) {
  954.       textPOV[POVBlength] = POVT[i];
  955.       POVBlength++;
  956.     }
  957.     break;
  958.   case 'U':
  959.     for (int i=0; i < sizeof(POVU); i++) {
  960.       textPOV[POVBlength] = POVU[i];
  961.       POVBlength++;
  962.     }
  963.     break;
  964.   case 'V':
  965.     for (int i=0; i < sizeof(POVV); i++) {
  966.       textPOV[POVBlength] = POVV[i];
  967.       POVBlength++;
  968.     }
  969.     break;
  970.   case 'W':
  971.     for (int i=0; i < sizeof(POVW); i++) {
  972.       textPOV[POVBlength] = POVW[i];
  973.       POVBlength++;
  974.     }
  975.     break;
  976.   case 'X':
  977.     for (int i=0; i < sizeof(POVX); i++) {
  978.       textPOV[POVBlength] = POVX[i];
  979.       POVBlength++;
  980.     }
  981.     break;
  982.   case 'Y':
  983.     for (int i=0; i < sizeof(POVY); i++) {
  984.       textPOV[POVBlength] = POVY[i];
  985.       POVBlength++;
  986.     }
  987.     break;
  988.   case 'Z':
  989.     for (int i=0; i < sizeof(POVZ); i++) {
  990.       textPOV[POVBlength] = POVZ[i];
  991.       POVBlength++;
  992.     }
  993.     break;
  994.   case 'a':
  995.     for (int i=0; i < sizeof(POVAL); i++) {
  996.       textPOV[POVBlength] = POVAL[i];
  997.       POVBlength++;
  998.     }
  999.     break;
  1000.   case 'b':
  1001.     for (int i=0; i < sizeof(POVBL); i++) {
  1002.       textPOV[POVBlength] = POVBL[i];
  1003.       POVBlength++;
  1004.     }
  1005.     break;
  1006.   case 'c':
  1007.     for (int i=0; i < sizeof(POVCL); i++) {
  1008.       textPOV[POVBlength] = POVCL[i];
  1009.       POVBlength++;
  1010.     }
  1011.     break;
  1012.   case 'd':
  1013.     for (int i=0; i < sizeof(POVDL); i++) {
  1014.       textPOV[POVBlength] = POVDL[i];
  1015.       POVBlength++;
  1016.     }
  1017.     break;
  1018.   case 'e':
  1019.     for (int i=0; i < sizeof(POVEL); i++) {
  1020.       textPOV[POVBlength] = POVEL[i];
  1021.       POVBlength++;
  1022.     }
  1023.     break;
  1024.   case 'f':
  1025.     for (int i=0; i < sizeof(POVFL); i++) {
  1026.       textPOV[POVBlength] = POVFL[i];
  1027.       POVBlength++;
  1028.     }
  1029.     break;
  1030.   case 'g':
  1031.     for (int i=0; i < sizeof(POVGL); i++) {
  1032.       textPOV[POVBlength] = POVGL[i];
  1033.       POVBlength++;
  1034.     }
  1035.     break;
  1036.   case 'h':
  1037.     for (int i=0; i < sizeof(POVHL); i++) {
  1038.       textPOV[POVBlength] = POVHL[i];
  1039.       POVBlength++;
  1040.     }
  1041.     break;
  1042.   case 'i':
  1043.     for (int i=0; i < sizeof(POVIL); i++) {
  1044.       textPOV[POVBlength] = POVIL[i];
  1045.       POVBlength++;
  1046.     }
  1047.     break;
  1048.   case 'j':
  1049.     for (int i=0; i < sizeof(POVJL); i++) {
  1050.       textPOV[POVBlength] = POVJL[i];
  1051.       POVBlength++;
  1052.     }
  1053.     break;
  1054.   case 'k':
  1055.     for (int i=0; i < sizeof(POVKL); i++) {
  1056.       textPOV[POVBlength] = POVKL[i];
  1057.       POVBlength++;
  1058.     }
  1059.     break;
  1060.   case 'l':
  1061.     for (int i=0; i < sizeof(POVLL); i++) {
  1062.       textPOV[POVBlength] = POVLL[i];
  1063.       POVBlength++;
  1064.     }
  1065.     break;
  1066.   case 'm':
  1067.     for (int i=0; i < sizeof(POVML); i++) {
  1068.       textPOV[POVBlength] = POVML[i];
  1069.       POVBlength++;
  1070.     }
  1071.     break;
  1072.   case 'n':
  1073.     for (int i=0; i < sizeof(POVNL); i++) {
  1074.       textPOV[POVBlength] = POVNL[i];
  1075.       POVBlength++;
  1076.     }
  1077.     break;
  1078.   case 'o':
  1079.     for (int i=0; i < sizeof(POVOL); i++) {
  1080.       textPOV[POVBlength] = POVOL[i];
  1081.       POVBlength++;
  1082.     }
  1083.     break;
  1084.   case 'p':
  1085.     for (int i=0; i < sizeof(POVPL); i++) {
  1086.       textPOV[POVBlength] = POVPL[i];
  1087.       POVBlength++;
  1088.     }
  1089.     break;
  1090.   case 'q':
  1091.     for (int i=0; i < sizeof(POVQL); i++) {
  1092.       textPOV[POVBlength] = POVQL[i];
  1093.       POVBlength++;
  1094.     }
  1095.     break;
  1096.   case 'r':
  1097.     for (int i=0; i < sizeof(POVRL); i++) {
  1098.       textPOV[POVBlength] = POVRL[i];
  1099.       POVBlength++;
  1100.     }
  1101.     break;
  1102.   case 's':
  1103.     for (int i=0; i < sizeof(POVSL); i++) {
  1104.       textPOV[POVBlength] = POVSL[i];
  1105.       POVBlength++;
  1106.     }
  1107.     break;
  1108.   case 't':
  1109.     for (int i=0; i < sizeof(POVTL); i++) {
  1110.       textPOV[POVBlength] = POVTL[i];
  1111.       POVBlength++;
  1112.     }
  1113.     break;
  1114.   case 'u':
  1115.     for (int i=0; i < sizeof(POVUL); i++) {
  1116.       textPOV[POVBlength] = POVUL[i];
  1117.       POVBlength++;
  1118.     }
  1119.     break;
  1120.   case 'v':
  1121.     for (int i=0; i < sizeof(POVVL); i++) {
  1122.       textPOV[POVBlength] = POVVL[i];
  1123.       POVBlength++;
  1124.     }
  1125.     break;
  1126.   case 'w':
  1127.     for (int i=0; i < sizeof(POVWL); i++) {
  1128.       textPOV[POVBlength] = POVWL[i];
  1129.       POVBlength++;
  1130.     }
  1131.     break;
  1132.   case 'x':
  1133.     for (int i=0; i < sizeof(POVXL); i++) {
  1134.       textPOV[POVBlength] = POVXL[i];
  1135.       POVBlength++;
  1136.     }
  1137.     break;
  1138.   case 'y':
  1139.     for (int i=0; i < sizeof(POVYL); i++) {
  1140.       textPOV[POVBlength] = POVYL[i];
  1141.       POVBlength++;
  1142.     }
  1143.     break;
  1144.   case 'z':
  1145.     for (int i=0; i < sizeof(POVZL); i++) {
  1146.       textPOV[POVBlength] = POVZL[i];
  1147.       POVBlength++;
  1148.     }
  1149.     break;
  1150.   case '0':
  1151.     for (int i=0; i < sizeof(POV0); i++) {
  1152.       textPOV[POVBlength] = POV0[i];
  1153.       POVBlength++;
  1154.     }
  1155.     break;
  1156.   case '1':
  1157.     for (int i=0; i < sizeof(POV1); i++) {
  1158.       textPOV[POVBlength] = POV1[i];
  1159.       POVBlength++;
  1160.     }
  1161.     break;
  1162.   case '2':
  1163.     for (int i=0; i < sizeof(POV2); i++) {
  1164.       textPOV[POVBlength] = POV2[i];
  1165.       POVBlength++;
  1166.     }
  1167.     break;
  1168.   case '3':
  1169.     for (int i=0; i < sizeof(POV3); i++) {
  1170.       textPOV[POVBlength] = POV3[i];
  1171.       POVBlength++;
  1172.     }
  1173.     break;
  1174.   case '4':
  1175.     for (int i=0; i < sizeof(POV4); i++) {
  1176.       textPOV[POVBlength] = POV4[i];
  1177.       POVBlength++;
  1178.     }
  1179.     break;
  1180.   case '5':
  1181.     for (int i=0; i < sizeof(POV5); i++) {
  1182.       textPOV[POVBlength] = POV5[i];
  1183.       POVBlength++;
  1184.     }
  1185.     break;
  1186.   case '6':
  1187.     for (int i=0; i < sizeof(POV6); i++) {
  1188.       textPOV[POVBlength] = POV6[i];
  1189.       POVBlength++;
  1190.     }
  1191.     break;
  1192.   case '7':
  1193.     for (int i=0; i < sizeof(POV7); i++) {
  1194.       textPOV[POVBlength] = POV7[i];
  1195.       POVBlength++;
  1196.     }
  1197.     break;
  1198.   case '8':
  1199.     for (int i=0; i < sizeof(POV8); i++) {
  1200.       textPOV[POVBlength] = POV8[i];
  1201.       POVBlength++;
  1202.     }
  1203.     break;
  1204.   case '9':
  1205.     for (int i=0; i < sizeof(POV9); i++) {
  1206.       textPOV[POVBlength] = POV9[i];
  1207.       POVBlength++;
  1208.     }
  1209.     break;
  1210.   case '.':
  1211.     for (int i=0; i < sizeof(POVperiod); i++) {
  1212.       textPOV[POVBlength] = POVperiod[i];
  1213.       POVBlength++;
  1214.     }
  1215.     break;
  1216.   case '?':
  1217.     for (int i=0; i < sizeof(POVquestion); i++) {
  1218.       textPOV[POVBlength] = POVquestion[i];
  1219.       POVBlength++;
  1220.     }
  1221.     break;
  1222.   case '!':
  1223.     for (int i=0; i < sizeof(POVexclamation); i++) {
  1224.       textPOV[POVBlength] = POVexclamation[i];
  1225.       POVBlength++;
  1226.     }
  1227.     break;
  1228.   case ':':
  1229.     for (int i=0; i < sizeof(POVcolon); i++) {
  1230.       textPOV[POVBlength] = POVcolon[i];
  1231.       POVBlength++;
  1232.     }
  1233.     break;
  1234.   case ';':
  1235.     for (int i=0; i < sizeof(POVsemicolon); i++) {
  1236.       textPOV[POVBlength] = POVsemicolon[i];
  1237.       POVBlength++;
  1238.     }
  1239.     break;
  1240.   case '-':
  1241.     for (int i=0; i < sizeof(POVdash); i++) {
  1242.       textPOV[POVBlength] = POVdash[i];
  1243.       POVBlength++;
  1244.     }
  1245.     break;
  1246.   case 1:
  1247.     for (int i=0; i < sizeof(POVC0); i++) {
  1248.       textPOV[POVBlength] = POVC0[i];
  1249.       POVBlength++;
  1250.     }
  1251.     break;
  1252.   case 2:
  1253.     for (int i=0; i < sizeof(POVC1); i++) {
  1254.       textPOV[POVBlength] = POVC1[i];
  1255.       POVBlength++;
  1256.     }
  1257.     break;
  1258.   case 3:
  1259.     for (int i=0; i < sizeof(POVC2); i++) {
  1260.       textPOV[POVBlength] = POVC2[i];
  1261.       POVBlength++;
  1262.     }
  1263.     break;
  1264.   case 4:
  1265.     for (int i=0; i < sizeof(POVC3); i++) {
  1266.       textPOV[POVBlength] = POVC3[i];
  1267.       POVBlength++;
  1268.     }
  1269.     break;
  1270.   case 5:
  1271.     for (int i=0; i < sizeof(POVC4); i++) {
  1272.       textPOV[POVBlength] = POVC4[i];
  1273.       POVBlength++;
  1274.     }
  1275.     break;
  1276.   case 6:
  1277.     for (int i=0; i < sizeof(POVC5); i++) {
  1278.       textPOV[POVBlength] = POVC5[i];
  1279.       POVBlength++;
  1280.     }
  1281.     break;
  1282.   case 7:
  1283.     for (int i=0; i < sizeof(POVC6); i++) {
  1284.       textPOV[POVBlength] = POVC6[i];
  1285.       POVBlength++;
  1286.     }
  1287.     break;
  1288.   case 8:
  1289.     for (int i=0; i < sizeof(POVC7); i++) {
  1290.       textPOV[POVBlength] = POVC7[i];
  1291.       POVBlength++;
  1292.     }
  1293.     break;
  1294.   case 9:
  1295.     for (int i=0; i < sizeof(POVC8); i++) {
  1296.       textPOV[POVBlength] = POVC8[i];
  1297.       POVBlength++;
  1298.     }
  1299.     break;
  1300.   case 10:
  1301.     for (int i=0; i < sizeof(POVC9); i++) {
  1302.       textPOV[POVBlength] = POVC9[i];
  1303.       POVBlength++;
  1304.     }
  1305.     break;
  1306.   case 11:
  1307.     for (int i=0; i < sizeof(POVC10); i++) {
  1308.       textPOV[POVBlength] = POVC10[i];
  1309.       POVBlength++;
  1310.     }
  1311.     break;
  1312.   case 12:
  1313.     for (int i=0; i < sizeof(POVC11); i++) {
  1314.       textPOV[POVBlength] = POVC11[i];
  1315.       POVBlength++;
  1316.     }
  1317.     break;
  1318.   case 13:
  1319.     for (int i=0; i < sizeof(POVC12); i++) {
  1320.       textPOV[POVBlength] = POVC12[i];
  1321.       POVBlength++;
  1322.     }
  1323.     break;
  1324.   case 14:
  1325.     for (int i=0; i < sizeof(POVC13); i++) {
  1326.       textPOV[POVBlength] = POVC13[i];
  1327.       POVBlength++;
  1328.     }
  1329.     break;
  1330.   case 15:
  1331.     for (int i=0; i < sizeof(POVC14); i++) {
  1332.       textPOV[POVBlength] = POVC14[i];
  1333.       POVBlength++;
  1334.     }
  1335.     break;
  1336.   case 16:
  1337.     for (int i=0; i < sizeof(POVC15); i++) {
  1338.       textPOV[POVBlength] = POVC15[i];
  1339.       POVBlength++;
  1340.     }
  1341.     break;
  1342.   default:
  1343.     for (int i=0; i < sizeof(POVSP); i++) {
  1344.       textPOV[POVBlength] = POVSP[i];
  1345.       POVBlength++;
  1346.     }
  1347.   }
  1348.   textPOV[POVBlength] = 0;
  1349.   POVBlength++;
  1350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement