Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. #include <digitalWriteFast.h>
  2.  
  3. #define FASTADC 1
  4.  
  5. // defines for setting and clearing register bits
  6. #ifndef cbi
  7. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  8. #endif
  9. #ifndef sbi
  10. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  11. #endif
  12.  
  13. int CLK = 23; //Pin that sends impluse to TSL1401R's CLK pin.
  14. int SI = 22; //Pin that sends impluse to TSL1401R's SI pin.
  15. int Cam1Aout = A2; //Pin that receives impluse TSL1401R's analog output.
  16. int Cam2Aout = A1; //Pin that receives impluse TSL1401R's analog output.
  17. int Cam3Aout = A0; //Pin that receives impluse TSL1401R's analog output.
  18.  
  19. int delayTime = 170;
  20.  
  21. unsigned long runTime;
  22.  
  23. int pixelsArray1[128]; //Array to hold the values of the individual pixles.
  24. int pixelsArray2[128]; //Array to hold the values of the individual pixles.
  25. int pixelsArray3[128]; //Array to hold the values of the individual pixles.
  26.  
  27. void setup()
  28. {
  29. #if FASTADC
  30. // set prescale to 16
  31. sbi(ADCSRA,ADPS2) ;
  32. cbi(ADCSRA,ADPS1) ;
  33. cbi(ADCSRA,ADPS0) ;
  34. #endif
  35.  
  36. pinModeFast(CLK,OUTPUT); //Setting the CLK pin to be used for output
  37. pinModeFast(SI,OUTPUT); //Setting the SI pin to be used for output
  38.  
  39. pinMode(Cam1Aout,INPUT); //Setting the analog pin to be used for input
  40. pinMode(Cam2Aout,INPUT); //Setting the analog pin to be used for input
  41. pinMode(Cam3Aout,INPUT); //Setting the analog pin to be used for input
  42.  
  43. Serial.begin(9600); //Setting the data transfer rate
  44. //Serial.begin(19200); //Setting the data transfer rate
  45.  
  46. /*noInterrupts();
  47. CLKPR = _BV(CLKPCE); // enable change of the clock prescaler
  48. CLKPR = _BV(CLKPS0); // divide frequency by 2
  49. interrupts();*/
  50.  
  51. }
  52. /************************************************************************/
  53.  
  54. void loop()
  55. {
  56. timming();
  57. readPixels();
  58. outputPixels();
  59. programRunTime();
  60. }
  61.  
  62. /************************************************************************/
  63. /*This function calculates the runtime of the program.*/
  64. void programRunTime()
  65. {
  66. //runTime = millis();
  67. Serial.print("Runtime: ");
  68. //prints delayTime since program started
  69. Serial.println(runTime);
  70. // wait a second so as not to send massive amounts of data
  71. delay(100);
  72. exit(0);
  73. }
  74.  
  75. /************************************************************************/
  76. /*This method is called at the begining of each cycle to throw away the old image and
  77. start a new cycle. Notice that unlike the readPixels function it contains a 129th impulse.*/
  78. void timming()
  79. {
  80.  
  81. //The timing for the impluses was found through direct experimentation.
  82. //(Meaing that I played around with different delayTimes until the code worked)
  83.  
  84. digitalWriteFast(SI, HIGH);
  85. delayMicroseconds(delayTime/2);
  86. digitalWriteFast(CLK, HIGH);
  87. delayMicroseconds(delayTime/2);
  88. digitalWriteFast(SI, LOW);
  89. delayMicroseconds(delayTime/2);
  90. digitalWriteFast(CLK, LOW);
  91. delayMicroseconds(delayTime);
  92.  
  93. for(int i = 0; i < 129; i++)
  94. {
  95. digitalWriteFast(CLK, HIGH);
  96. delayMicroseconds(delayTime);
  97. digitalWriteFast(CLK, LOW);
  98. delayMicroseconds(delayTime);
  99. }
  100.  
  101. }
  102. /************************************************************************/
  103. //This method reads in the values of the pixels and stores them into pixlesArray
  104. void readPixels()
  105. {
  106. digitalWriteFast(SI, HIGH);
  107. delayMicroseconds(delayTime/2);
  108. digitalWriteFast(CLK, HIGH);
  109. delayMicroseconds(delayTime/2);
  110. digitalWriteFast(SI, LOW);
  111. delayMicroseconds(delayTime/2);
  112. digitalWriteFast(CLK, LOW);
  113. delayMicroseconds(delayTime);
  114.  
  115. for(int i = 0; i < 128; i++)
  116. {
  117. digitalWriteFast(CLK, HIGH);
  118. pixelsArray1[i]=analogRead(Cam1Aout);
  119. pixelsArray2[i]=analogRead(Cam2Aout);
  120. pixelsArray3[i]=analogRead(Cam3Aout);
  121. delayMicroseconds(delayTime);
  122. digitalWriteFast(CLK, LOW);
  123. delayMicroseconds(delayTime);
  124. }
  125.  
  126. digitalWriteFast(CLK, HIGH);
  127. delayMicroseconds(delayTime);
  128. digitalWriteFast(CLK, LOW);
  129. delayMicroseconds(delayTime);
  130.  
  131. delayMicroseconds(20);
  132.  
  133. runTime = millis();
  134. }
  135.  
  136. /************************************************************************/
  137. //This method outputs the results that have been stored in pixelsArray[].
  138. void outputPixels()
  139. {
  140. Serial.print("Camera 1: ");
  141. for(int j = 0; j < 128; j++)
  142. {
  143. //Serial.print(j);
  144. //Serial.print("=");
  145. Serial.print(pixelsArray1[j]);
  146. Serial.print(", ");
  147. //delay(75); //This delay is not necessary for functionallity, but makes result output slower so they are easier to read.
  148. }
  149. Serial.println();
  150.  
  151. Serial.print("Camera 2: ");
  152. for(int j = 0; j < 128; j++)
  153. {
  154. //Serial.print(j);
  155. //Serial.print("=");
  156. Serial.print(pixelsArray2[j]);
  157. Serial.print(", ");
  158. //delay(75); //This delay is not necessary for functionallity, but makes result output slower so they are easier to read.
  159. }
  160. Serial.println();
  161.  
  162. Serial.print("Camera 3: ");
  163. for(int j = 0; j < 128; j++)
  164. {
  165. //Serial.print(j);
  166. //Serial.print("=");
  167. Serial.print(pixelsArray3[j]);
  168. Serial.print(", ");
  169. //delay(75); //This delay is not necessary for functionallity, but makes result output slower so they are easier to read.
  170. }
  171. Serial.println();
  172. }
  173.  
  174. Camera 1: 334, 358, 401, 451, 494, 531, 553, 575, 594, 612, 624, 634, 643, 654, 663, 668, 675, 680, 684, 688, 690, 688, 676, 671, 691, 711, 724, 728, 734, 740, 748, 755, 763, 770, 776, 783, 788, 793, 799, 801, 804, 806, 807, 807, 812, 815, 815, 824, 835, 846, 865, 881, 895, 914, 931, 952, 966, 980, 986, 996, 1012, 1022, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1003, 1004, 964, 978, 944, 955, 916, 911, 878, 873, 844, 838, 807, 801, 774, 768, 743, 728, 703, 697, 668, 636, 615, 596, 568, 532, 499, 462, 425, 395, 355, 327, 302, 279, 292, 299,
  175. Camera 2: 342, 367, 395, 422, 446, 467, 487, 511, 536, 556, 578, 596, 612, 630, 647, 660, 671, 688, 700, 713, 726, 736, 742, 752, 760, 767, 771, 783, 788, 796, 801, 807, 809, 814, 814, 817, 819, 825, 824, 825, 824, 824, 822, 827, 824, 827, 825, 825, 823, 824, 822, 823, 819, 819, 816, 816, 817, 822, 824, 829, 831, 841, 847, 856, 864, 875, 882, 894, 904, 915, 920, 924, 925, 931, 927, 931, 944, 936, 945, 931, 927, 930, 940, 922, 910, 907, 908, 884, 864, 881, 863, 858, 820, 835, 814, 792, 775, 787, 755, 763, 748, 737, 723, 712, 697, 687, 670, 655, 639, 625, 608, 590, 572, 551, 534, 515, 499, 481, 465, 447, 430, 409, 390, 368, 348, 331, 335, 327,
  176. Camera 3: 393, 407, 423, 428, 447, 476, 493, 504, 523, 536, 543, 559, 572, 588, 610, 620, 628, 638, 647, 655, 659, 662, 671, 675, 684, 689, 674, 678, 691, 688, 685, 679, 675, 668, 665, 660, 655, 652, 649, 647, 647, 648, 646, 644, 637, 639, 638, 635, 639, 639, 647, 641, 639, 639, 638, 633, 632, 631, 630, 621, 617, 610, 606, 597, 600, 600, 606, 612, 623, 632, 643, 652, 666, 679, 692, 707, 728, 746, 760, 771, 778, 783, 792, 798, 803, 795, 796, 785, 769, 747, 743, 735, 719, 696, 689, 673, 671, 658, 647, 639, 632, 627, 622, 614, 602, 590, 581, 572, 563, 551, 541, 526, 515, 502, 491, 478, 467, 454, 441, 428, 414, 403, 390, 374, 361, 352, 323, 323,
  177. Runtime: 98
  178.  
  179. Camera 1: 191, 186, 193, 201, 207, 214, 216, 217, 223, 225, 227, 229, 230, 231, 233, 234, 236, 236, 237, 237, 238, 237, 236, 236, 238, 241, 242, 243, 244, 246, 248, 248, 249, 251, 251, 252, 254, 254, 255, 255, 255, 255, 255, 255, 257, 257, 258, 259, 261, 263, 266, 268, 271, 274, 277, 280, 283, 286, 287, 289, 294, 294, 298, 297, 301, 302, 307, 310, 312, 312, 312, 313, 312, 316, 322, 327, 327, 326, 327, 327, 329, 331, 330, 331, 332, 331, 327, 330, 319, 317, 313, 311, 308, 304, 299, 297, 297, 294, 287, 286, 282, 279, 276, 272, 268, 265, 262, 259, 255, 249, 247, 242, 240, 233, 231, 226, 222, 216, 211, 206, 200, 195, 189, 184, 179, 176, 173, 192,
  180. Camera 2: 179, 182, 187, 189, 194, 195, 199, 203, 207, 207, 212, 216, 218, 219, 223, 224, 227, 229, 231, 232, 235, 236, 238, 238, 240, 240, 242, 242, 244, 245, 247, 247, 248, 248, 247, 248, 248, 249, 248, 249, 249, 249, 249, 250, 251, 249, 250, 250, 251, 250, 251, 249, 250, 249, 249, 249, 249, 250, 252, 251, 253, 254, 255, 255, 259, 259, 263, 263, 267, 267, 269, 270, 271, 271, 273, 273, 274, 275, 275, 275, 275, 275, 275, 275, 275, 272, 271, 269, 268, 265, 263, 260, 259, 257, 255, 253, 252, 248, 248, 244, 242, 241, 240, 236, 235, 231, 230, 227, 225, 223, 220, 216, 216, 211, 209, 205, 204, 199, 198, 195, 192, 188, 187, 183, 179, 176, 188, 215,
  181. Camera 3: 188, 190, 192, 193, 195, 199, 201, 202, 207, 209, 211, 213, 215, 214, 220, 222, 224, 224, 225, 224, 227, 229, 230, 230, 231, 231, 231, 231, 231, 231, 231, 231, 231, 229, 230, 228, 228, 227, 227, 227, 227, 228, 227, 226, 226, 226, 225, 225, 226, 226, 227, 226, 227, 226, 226, 226, 225, 225, 225, 224, 224, 223, 222, 220, 220, 220, 223, 223, 224, 226, 227, 230, 231, 235, 238, 240, 243, 248, 249, 252, 253, 255, 255, 258, 259, 259, 257, 255, 253, 249, 248, 246, 244, 241, 236, 236, 233, 231, 231, 231, 229, 227, 228, 227, 225, 224, 221, 220, 218, 216, 215, 212, 210, 207, 208, 204, 200, 199, 196, 196, 193, 191, 190, 188, 184, 184, 162, 187,
  182. Runtime: 19
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement