Guest User

airsoft target

a guest
May 30th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel
  4.  
  5. // define some values used by the panel and buttons
  6. int lcd_key = 0;
  7. int adc_key_in = 0;
  8.  
  9. #define btnRIGHT 0
  10. #define btnUP 1
  11. #define btnDOWN 2
  12. #define btnLEFT 3
  13. #define btnSELECT 4
  14. #define btnNONE 5
  15.  
  16. int read_LCD_buttons(){ // read the buttons
  17. adc_key_in = analogRead(0); // read the value from the sensor
  18.  
  19. // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  20. // we add approx 50 to those values and check to see if we are close
  21. // We make this the 1st option for speed reasons since it will be the most likely result
  22.  
  23. if (adc_key_in > 1000) return btnNONE;
  24.  
  25. // For V1.0 comment the other threshold and use the one below:
  26. if (adc_key_in < 50) return btnRIGHT;
  27. if (adc_key_in < 195) return btnUP;
  28. if (adc_key_in < 380) return btnDOWN;
  29. if (adc_key_in < 555) return btnLEFT;
  30. if (adc_key_in < 790) return btnSELECT;
  31.  
  32. return btnNONE; // when all others fail, return this.
  33. }
  34.  
  35. const boolean inputMode = HIGH;// high for buttons LOW for piezo
  36.  
  37. const int numPorts=2;
  38. int led[numPorts]={31,33};
  39. int ins[numPorts]={A8,A9};
  40. int goButton=btnSELECT;
  41. int rotarySwitch[3]={22,24,26};
  42.  
  43. int theDelay=1;
  44.  
  45. unsigned long time1;
  46. unsigned long time2;
  47. float interval1;
  48.  
  49. void setup()
  50. {
  51. pinMode(goButton,INPUT);
  52. digitalWrite(goButton,HIGH);
  53.  
  54. for (int i=0; i<numPorts; i++)
  55. {
  56. pinMode(led[i], OUTPUT);
  57. pinMode(ins[i],INPUT);
  58. //if (inputMode==HIGH) digitalWrite(ins[i],HIGH); // activate internal pullup;
  59. }
  60.  
  61. for (int i=0; i<3; i++)
  62. {
  63. pinMode(rotarySwitch[i],INPUT);
  64. digitalWrite(rotarySwitch[i],HIGH);//internal pullup
  65. }
  66.  
  67.  
  68. Serial.begin(9600);
  69. lcd.print(0x0C); delay(5);
  70.  
  71. doPost();
  72.  
  73. lcd.print(0x0C); delay(5);
  74. lcd.print(0xFA); //define custom character 2
  75. //Now send the eight data bytes
  76. lcd.print(B00000100);
  77. lcd.print(B00000100);
  78. lcd.print(B00011111);
  79. lcd.print(B00000100);
  80. lcd.print(B00000100);
  81. lcd.print(B00000100);
  82. lcd.print(B00000000);
  83. lcd.print(B00011111);
  84. //Serial1.write(0x02); //Display the new custom character 2
  85. lcd.print(0x11);
  86. lcd.print(0x0C); delay(5);
  87. // 0000000000111111
  88. // 0123456789012345
  89. lcd.println("Select a mode ");
  90. lcd.println("then press GO ");
  91. }
  92.  
  93. void loop()
  94. {
  95. //Serial1.write(0x12);
  96. //delay(1000);
  97. lcd.print(0x0C); delay(5);
  98. // 0000000000111111
  99. // 0123456789012345
  100. lcd.println("Select a mode ");
  101. lcd.println("press RUN ");
  102.  
  103. while ((PINC & B00010000));
  104.  
  105.  
  106. //doRapidFire();
  107.  
  108. if (digitalRead(rotarySwitch[0])==LOW) doQuickDraw();
  109. else if (digitalRead(rotarySwitch[1])==LOW) doTimedMode();
  110. else if (digitalRead(rotarySwitch[2])==LOW) doRapidFire();
  111.  
  112. //doQuickDraw();
  113. //doTimedMode();
  114. //doRapidFire();
  115. }
  116.  
  117. void doTimedMode()
  118. {
  119. randomSeed(millis());
  120.  
  121. int currentPort=random(4);
  122. int newPort=random(4);
  123. int maxRounds=32;
  124. int hitCounter=0;
  125. time1=millis();
  126. interval1=0;
  127.  
  128. lcd.print(0x0C); delay(5);
  129. // 0000000000111111
  130. lcd.println("Timed mode");
  131. delay(3000);
  132. lcd.print(0x0C); delay(5);
  133. lcd.print(0xDF); // C note
  134. lcd.println("GO");
  135. while (interval1 < 10000) // 10 seconds
  136. {
  137. //time1=millis();
  138. digitalWrite(led[currentPort],HIGH);
  139. switch (currentPort)
  140. {
  141. case 0:
  142. while (!(PINC & B00000001));
  143. break;
  144. case 1:
  145. while (!(PINC & B00000010));
  146. break;
  147. case 2:
  148. while (!(PINC & B00000100));
  149. break;
  150. case 3:
  151. while (!(PINC & B00001000));
  152. break;
  153. }
  154. hitCounter++;
  155. //delay(1000);
  156. time2=millis();
  157. interval1=(time2-time1);
  158. digitalWrite(led[currentPort],LOW);
  159. lcd.print(0x0C); delay(5);
  160. // 0000000000111111
  161. lcd.print("Hits: ");
  162. lcd.println(hitCounter,DEC);
  163. newPort=random(4);
  164. while (newPort==currentPort) newPort=random(4);
  165. currentPort=newPort;
  166. }
  167. lcd.print(0x0C); delay(5);
  168. lcd.print(hitCounter,DEC);
  169. lcd.println(" Hits");
  170. lcd.print(0x0D);
  171. lcd.println("in 10Secs");
  172. delay(5000);
  173. }
  174. void doRapidFire()
  175. {
  176. randomSeed(millis());
  177.  
  178. int currentPort=random(4);
  179. int newPort=random(4);
  180. int maxRounds=32;
  181. time1=millis();
  182.  
  183. lcd.print(0x0C); delay(5);
  184. // 0000000000111111
  185. lcd.println("Rapid Fire mode");
  186.  
  187. delay(2000);
  188.  
  189. lcd.print(0xDF); // C note
  190.  
  191.  
  192. for (int i=0; i<maxRounds; i++)
  193. {
  194. lcd.print(0x0C); delay(10);
  195. // 0000000000111111
  196. lcd.println("Rapid Fire mode");
  197. lcd.print("Round ");
  198. lcd.print(i+1,DEC);
  199. lcd.print(" of ");
  200. lcd.print(maxRounds,DEC);
  201. lcd.println("");
  202. digitalWrite(led[currentPort],HIGH);
  203. switch (currentPort)
  204. {
  205. case 0:
  206. while (!(PINC & B00000001));
  207. break;
  208. case 1:
  209. while (!(PINC & B00000010));
  210. break;
  211. case 2:
  212. while (!(PINC & B00000100));
  213. break;
  214. case 3:
  215. while (!(PINC & B00001000));
  216. break;
  217. }
  218. //delay(1000);
  219. //interval1=interval1+0.00001;
  220. digitalWrite(led[currentPort],LOW);
  221. newPort=random(4);
  222. while (newPort==currentPort) newPort=random(4);
  223.  
  224. //newPort=3;
  225.  
  226. currentPort=newPort;
  227. }
  228.  
  229. time2=millis();
  230. interval1=(time2-time1);
  231. interval1=interval1/1000;
  232. lcd.print(0x0C); delay(5);
  233. lcd.println("Rapid Time:"); lcd.print(0x0D);
  234. lcd.print(interval1,4);
  235. lcd.println(" secs");
  236.  
  237. delay(5000);
  238. }
  239.  
  240.  
  241. void doQuickDraw()
  242. {
  243. randomSeed(millis());
  244.  
  245. int firstTime=true;
  246.  
  247. int currentPort=random(4);
  248. int maxRounds=8;
  249.  
  250. lcd.print(0x0C); delay(5);
  251. lcd.println("Quickdraw mode ");
  252. delay(2000);
  253.  
  254. for (int i=0; i<maxRounds; i++)
  255. {
  256. lcd.print(0x0C); delay(5);
  257. lcd.println("Quickdraw mode ");
  258. lcd.print("Round ");
  259. lcd.print(i+1,DEC);
  260. lcd.print(" of ");
  261. lcd.print(maxRounds,DEC);
  262. lcd.println("");
  263. delay(random(3000)+1000);
  264. if (firstTime==true) { lcd.print(0xDF); firstTime=false; }// C note
  265.  
  266. time1=millis();
  267. digitalWrite(led[currentPort],HIGH);
  268. switch (currentPort)
  269. {
  270. case 0:
  271. while (!(PINC & B00000001));
  272. break;
  273. case 1:
  274. while (!(PINC & B00000010));
  275. break;
  276. case 2:
  277. while (!(PINC & B00000100));
  278. break;
  279. case 3:
  280. while (!(PINC & B00001000));
  281. break;
  282. }
  283. //delay(1000);
  284. time2=millis();
  285. interval1=(time2-time1);
  286. interval1=interval1/1000;
  287. digitalWrite(led[currentPort],LOW);
  288. lcd.print(0x0C); delay(5);
  289. lcd.println("Quickdraw Time: ");
  290. lcd.print(interval1,3);
  291. lcd.println(" seconds");
  292. delay(2000);
  293. currentPort=random(4);
  294. }
  295. delay(5000);
  296. }
  297.  
  298. void doPost()
  299. {
  300. lcd.print(0xD2); // 1/16 note
  301. lcd.print(0xE3);//E
  302. //delay(100);
  303. lcd.print(0xDC);//A
  304. //delay(100);
  305. lcd.print(0xDE);//B
  306. for (int lop=0; lop<20; lop++)
  307. {
  308. theDelay+=10;
  309. if (theDelay>500) theDelay=1;
  310.  
  311. for (int i=0; i<numPorts; i++)
  312. {
  313. //if (digitalRead(ins[i])==HIGH)
  314. {
  315. digitalWrite(led[i], HIGH); // turn the LED on (HIGH is the voltage level)
  316. }
  317. }
  318. delay(theDelay); // wait for a second
  319. for (int i=0; i<numPorts; i++)
  320. {
  321. digitalWrite(led[i], LOW); // turn the LED off by making the voltage LOW
  322. }
  323. delay(theDelay); // wait for a second
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment