Advertisement
Guest User

Code for remote control

a guest
Jun 21st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.77 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. //Vores eget
  5. //Display
  6. #include <Arduino.h>
  7. #include <U8g2lib.h>
  8.  
  9. #ifdef U8X8_HAVE_HW_SPI
  10. #include <SPI.h>
  11. #endif
  12. #ifdef U8X8_HAVE_HW_I2C
  13. #include <Wire.h>
  14. #endif
  15. U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
  16.  
  17. //Button
  18. const int buttonPin = D7; // the number of the pushbutton pin
  19. int buttonState = 0; // variable for reading the pushbutton status
  20.  
  21. //Potentiometer
  22. int potPin = A0; // select the input pin for the potentiometer
  23. int potVal = 0; // variable to store the value coming from the sensor
  24.  
  25. //Buzzer
  26. const int buzzer = D6;
  27.  
  28. //Egne variables etc
  29. int state = 1; //test
  30. int timeP = 0;
  31. int checkRun = 0;
  32. int runTime = 0;
  33. int antiSpamReset = 0;
  34. int resetSwitchCounter = 0;
  35. int buttonStatePrevious = 0;
  36. int buttonStateAlt = 0;
  37.  
  38. int button_1_ready = 1; //Test values
  39. int button_2_ready = 1;
  40. int button_3_ready = 1;
  41. int sentry_1_ready = 1;
  42. int sentry_2_ready = 1;
  43. int sentry_3_ready = 1;
  44.  
  45. int menuState = 1;
  46. int gamemode = 0;
  47.  
  48. int buttonNo = 0;
  49. int diff = 0;
  50. int numberOfThings = 0;
  51.  
  52. int MPV = 0;
  53. int hugeNumber = 0;
  54. int timeStarted = 0;
  55. int buzzerLimiter = 0;
  56.  
  57. //GAME VARIABLES
  58. int ingameTime = 0;
  59. int timesCaught = 0;
  60.  
  61. //TURRET CONTROL VARIABLES
  62. int nowControlling = 0;
  63. int sentryCommand = 0;
  64. int controlLimiter = 0;
  65. int oldDirection = 10;
  66. int newDirection = 11;
  67.  
  68. //Gamemode 1
  69. int but1Pressed = 0;
  70. int but2Pressed = 0;
  71. int but3Pressed = 0;
  72. int buttonsPressed = 0;
  73.  
  74. //Gamemode 2
  75. int takeOuts = 0;
  76.  
  77. //Gamemode 2/3
  78. int but1CheckRun = 0;
  79. int but2CheckRun = 0;
  80. int but3CheckRun = 0;
  81.  
  82. int but1StartTime = 0;
  83. int but2StartTime = 0;
  84. int but3StartTime = 0;
  85.  
  86. int endgameCheck = 0;
  87.  
  88. // Update these with values suitable for your network.
  89.  
  90. const char* ssid = "DSB Free Wifi";
  91. const char* password = "skridspader";
  92.  
  93. // for the in-class exercise, we'll use the public broker, but for the actual course project:
  94. // set-up a private mqtt broker at cloudmqtt.com (see Canvas for a howto video)
  95. //and uncomment / edit the following: [ you also need to change the 'client.connect' part of the code below in the code ]
  96.  
  97. //const char *mqtt_server = "mXX.cloudmqtt.com";
  98. //const int mqtt_port = 17323;
  99. const char *mqtt_server = "m21.cloudmqtt.com";
  100. const int mqtt_port = 14939;
  101. const char *mqtt_user = "testuser";
  102. const char *mqtt_pass = "hejhejhej";
  103.  
  104.  
  105. //see the actual function below - this is just a placeholder..
  106. void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length);
  107.  
  108. //Let's define some objects:
  109. WiFiClient espClient;
  110.  
  111. //and the mqtt client:
  112. PubSubClient client(mqtt_server,mqtt_port,callback,espClient);
  113.  
  114.  
  115. //THIS IS THE CORE OF THE 'SUBSCRIBE'
  116. //everytime something happens on the topics we've subscribed [down below in the code]; this function will be fired.
  117.  
  118. void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length) {
  119.  
  120. // it has the 'topic' and the message 'payload'
  121.  
  122. //let's convert the topic name [the first array] to a string:
  123. String topic;
  124. topic=String(byteArraytopic);
  125. Serial.print("Message arrived [");
  126. Serial.print(topic);
  127. Serial.print("] ");
  128.  
  129. // let's convert the incoming message [which arrives as an array of characters] to a string:
  130. String payload;
  131. for (int i = 0; i < length; i++) {
  132. //Serial.print((char)payload[i]);
  133. payload+=(char)byteArrayPayload[i];
  134. }
  135.  
  136.  
  137. //HER ER DER KODE SOM AKTIVERES VED NYT PÅ TOPICS YOOO
  138. if (String(topic) == "groupsix/readyCheckBut1"){
  139. button_1_ready = 1;
  140. }
  141. if (String(topic) == "groupsix/readyCheckBut2"){
  142. button_2_ready = 1;
  143. }
  144. if (String(topic) == "groupsix/readyCheckBut3"){
  145. button_3_ready = 1;
  146. }
  147. if (String(topic) == "groupsix/readyCheckSen1"){
  148. sentry_1_ready = 1;
  149. }
  150. if (String(topic) == "groupsix/readyCheckSen2"){
  151. sentry_2_ready = 1;
  152. }
  153. if (String(topic) == "groupsix/readyCheckSen3"){
  154. sentry_3_ready = 1;
  155. }
  156. if (String(topic) == "groupsix/caught"){
  157. timesCaught=timesCaught+1;
  158. }
  159. if (String(topic) == "groupsix/b1p"){
  160. but1Pressed = 1;
  161. }
  162. if (String(topic) == "groupsix/b2p"){
  163. but2Pressed = 1;
  164. }
  165. if (String(topic) == "groupsix/b3p"){
  166. but3Pressed = 1;
  167. }
  168.  
  169. }
  170. // Setup the wifi connection...
  171. void setup_wifi() {
  172.  
  173. delay(10);
  174. // We start by connecting to a WiFi network
  175. Serial.println();
  176. Serial.print("Connecting to ");
  177. Serial.println(ssid);
  178.  
  179. WiFi.begin(ssid, password);
  180.  
  181. while (WiFi.status() != WL_CONNECTED) {
  182. delay(500);
  183. Serial.print(".");
  184. }
  185. Serial.println("");
  186. Serial.println("WiFi connected");
  187. Serial.println("IP address: ");
  188. Serial.println(WiFi.localIP());
  189. }
  190.  
  191.  
  192. // A function to connect to the mqtt server, and reconnect if the connection breaks, wifi breaks, server problem etc..
  193.  
  194. void reconnect() {
  195. // Loop until we're reconnected
  196. while (!client.connected()) {
  197. Serial.print("Attempting MQTT connection...");
  198. // Attempt to connect:
  199. // if you are using cloudmqtt, uncomment the following line, and comment the one below...
  200. if (client.connect("groupsix","testuser","hejhejhej")){ // make sure 'myclientID' is something unique, e.g. 'groupXX'
  201. //if (client.connect("myclientID")) { // make sure 'myclientID' is something unique, e.g. 'groupXX'
  202.  
  203. Serial.println("connected");
  204. // Once connected, publish an announcement...
  205. client.publish("groupsix/welcomeMessage", "hello world");
  206. // ... and resubscribe to all messages coming to the 'OLED' topic...
  207.  
  208. client.subscribe("groupsix/#");
  209. client.subscribe("groupsix/controller_online");
  210. client.subscribe("groupsix/turretBegin");
  211. client.subscribe("groupsix/reset"); //til slukke gamemode
  212. client.subscribe("groupsix/diff");
  213. client.subscribe("groupsix/nowControlling");
  214. client.subscribe("groupsix/turretDirections");
  215.  
  216. } else {
  217. Serial.print("failed, rc=");
  218. Serial.print(client.state());
  219. Serial.println(" try again in 5 seconds");
  220. // Wait 5 seconds before retrying
  221. delay(5000);
  222. }
  223. }
  224. }
  225.  
  226. void setup() {
  227.  
  228. //NOTE: if the following line does not work - try changing the BUILTIN_LED to LED_BUILTIN
  229. //if still does not work - attach a real LED to e.g. pin D5..
  230. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  231.  
  232. Serial.begin(115200);
  233. setup_wifi(); // self-explanatory.. check the function to see what it does..
  234. client.setServer(mqtt_server, mqtt_port); // connect to the mqtt server at the specified port [defined at the top]
  235.  
  236. // set the 'callback' function, which is defined above.
  237. // We tell our client to fire the 'callback' function everytime there is a new message on the subscribed topics
  238. client.setCallback(callback);
  239.  
  240. //Button
  241. pinMode(buttonPin, INPUT);
  242.  
  243. //Buzzer
  244. pinMode(buzzer, OUTPUT);
  245.  
  246. //Display
  247. u8g2.begin();
  248. }
  249.  
  250.  
  251. void loop() {
  252.  
  253. //if we're not connected to the mqtt broker:
  254. if (!client.connected()) {
  255. reconnect();
  256. }
  257. client.loop();
  258.  
  259. //Time
  260. timeP = millis();
  261.  
  262. //Button
  263. buttonState = digitalRead(buttonPin);
  264. buttonStateAlt= digitalRead(buttonPin);
  265.  
  266. //Potentiometer
  267. potVal = analogRead(potPin);
  268.  
  269. //Makes sure everything isnt spammed when the button is kept down (going through menus when trying to reset and spamming the broker). Only sends output if the buttonstate is currently LOW, and previously HIGH.
  270. if (buttonStateAlt==HIGH){
  271. buttonState=LOW;
  272. }
  273. if (buttonStateAlt==LOW){
  274. if (buttonStatePrevious==HIGH){
  275. buttonState=HIGH;
  276. }
  277. else{
  278. buttonState=LOW;
  279. }
  280. }
  281. buttonStatePrevious=buttonStateAlt;
  282.  
  283. //Actual reset
  284. if (buttonStateAlt==LOW){
  285. resetSwitchCounter=timeP;
  286. }
  287. if (timeP-resetSwitchCounter>3000 and state!=1 and state != 2){ //jeg vil gerne have man ikke kan køre initialiseringen over
  288. buttonStatePrevious=LOW; //Makes sure we dont have a click of "HIGH" when the system has been reset.
  289. u8g2.clearBuffer();
  290. u8g2.setFont(u8g2_font_ncenB14_tr);
  291. u8g2.drawStr(30,30,"System");
  292. u8g2.drawStr(35,55,"Reset");
  293. u8g2.sendBuffer();
  294. state=6;
  295. }
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302. //Splash state
  303. if (state==1){
  304. u8g2.clearBuffer();
  305. u8g2.setFont(u8g2_font_ncenB14_tr);
  306. u8g2.drawStr(0,35,"Agent Escape");
  307. u8g2.setFont(u8g2_font_chroma48medium8_8r);
  308. u8g2.drawStr(22,47,"Artuino Int");
  309. u8g2.sendBuffer();
  310. if (checkRun==0){
  311. runTime = millis();
  312. checkRun = 1;
  313. client.publish("groupsix/controller_online", "ready"); //Laver en splash screen og tjekker om alt er connected. Hvis ikke går den til en connection screen (som ikke ses hvis alt er tilgængeligt)
  314. }//Systemet fungerer så de gør opmærksomme på sig selv når de tænder, og når de får en "ready" fra controlleren. Derfor ved vi altid om de er på, med meget få resourcer.
  315. if (timeP-runTime>1000){
  316. checkRun = 0;
  317. state = 2;
  318. u8g2.clearBuffer();
  319. u8g2.setFont(u8g2_font_ncenB14_tr);
  320. }
  321. }
  322.  
  323. //Connecting to others state
  324. if (state == 2){
  325. if(button_1_ready == 1 and button_2_ready == 1 and button_3_ready == 1 and sentry_1_ready == 1 and sentry_2_ready == 1 and sentry_3_ready == 1){
  326. state=3;
  327. }
  328. else{
  329. if(button_1_ready == 1){ //Viser dem på skærmen alt som de kommer online.
  330. u8g2.drawStr(0,20,"But1");
  331. }
  332. if(button_2_ready == 1){
  333. u8g2.drawStr(0,40,"But2");
  334. }
  335. if(button_3_ready == 1){
  336. u8g2.drawStr(0,60,"But3");
  337. }
  338. if(sentry_1_ready == 1){
  339. u8g2.drawStr(70,20,"Sen1");
  340. }
  341. if(sentry_2_ready == 1){
  342. u8g2.drawStr(70,40,"Sen2");
  343. }
  344. if(sentry_3_ready == 1){
  345. u8g2.drawStr(70,60,"Sen3");
  346. }
  347. delay(10);
  348. u8g2.sendBuffer();
  349. }
  350. }
  351.  
  352. //Menu state
  353. if (state == 3){
  354. antiSpamReset = 0;
  355. //Menu substate 1 (gamemode)
  356. if (menuState == 1){
  357. u8g2.clearBuffer();
  358. u8g2.setFont(u8g2_font_chroma48medium8_8r);
  359. u8g2.drawStr(5,7,"Select Gamemode");
  360. u8g2.drawStr(20,22,"Jail escape");
  361. u8g2.drawStr(20,52,"Agent COOP");
  362. u8g2.drawStr(20,37,"Take out");
  363. if (potVal>840){
  364. u8g2.drawStr(7,22,"o");
  365. if (buttonState==HIGH){
  366. gamemode=1;
  367. menuState=2;
  368. MPV=900;
  369. buttonState=LOW;
  370. }
  371. }
  372. if (potVal<840 and potVal>700){
  373. u8g2.drawStr(7,37,"o");
  374. if (buttonState==HIGH){
  375. gamemode=2;
  376. menuState=2;
  377. MPV=840;
  378. buttonState=LOW;
  379. }
  380. }
  381. if (potVal<700){
  382. u8g2.drawStr(7,52,"o");
  383. if (buttonState==HIGH){
  384. gamemode=3;
  385. hugeNumber=1000;
  386. menuState=2;
  387. MPV=700;
  388. buttonState=LOW;
  389. }
  390. }
  391. u8g2.sendBuffer();
  392.  
  393. }
  394.  
  395. //Menu substate 2 (number of things)
  396. if (menuState == 2){
  397. u8g2.clearBuffer();
  398. u8g2.setFont(u8g2_font_chroma48medium8_8r);
  399. if (gamemode == 1){
  400. u8g2.drawStr(5,7,"No of buttons");
  401. u8g2.drawStr(20,22,"1 button");
  402. u8g2.drawStr(20,37,"2 buttons");
  403. u8g2.drawStr(20,52,"3 buttons");
  404. }
  405. if (gamemode == 2){
  406. u8g2.drawStr(5,7,"No of sentries");
  407. u8g2.drawStr(20,22,"1 sentry");
  408. u8g2.drawStr(20,37,"2 sentries");
  409. u8g2.drawStr(20,52,"3 sentries");
  410. }
  411. if (gamemode == 3){
  412. u8g2.drawStr(5,7,"No of players");
  413. u8g2.drawStr(20,22,"2 players");
  414. u8g2.drawStr(20,37,"3 players");
  415. }
  416. if (potVal<1000 and potVal>MPV-140){
  417. u8g2.drawStr(7,22,"o");
  418. if (buttonState==HIGH){
  419. client.publish("groupsix/numberOfThings", "1");
  420. numberOfThings=1;
  421. MPV=MPV-140;
  422. menuState=3;
  423. buttonState=LOW;
  424. }
  425. }
  426. if (potVal<MPV-140 and potVal>MPV-280-hugeNumber){
  427. u8g2.drawStr(7,37,"o");
  428. if (buttonState==HIGH){
  429. client.publish("groupsix/numberOfThings", "2");
  430. numberOfThings=2;
  431. MPV=MPV-280;
  432. menuState=3;
  433. buttonState=LOW;
  434. }
  435. }
  436. if (potVal+hugeNumber<MPV-280-hugeNumber){
  437. u8g2.drawStr(7,52,"o");
  438. if (buttonState==HIGH){
  439. client.publish("groupsix/numberOfThings", "3");
  440. numberOfThings=3;
  441. MPV=MPV-420;
  442. menuState=3;
  443. buttonState=LOW;
  444. }
  445. }
  446. u8g2.sendBuffer();
  447. }
  448.  
  449. //Menu substate 3 (difficulty)
  450. if (menuState == 3){
  451. u8g2.clearBuffer();
  452. u8g2.setFont(u8g2_font_chroma48medium8_8r);
  453. u8g2.drawStr(5,7,"Difficulty");
  454. u8g2.drawStr(20,22,"Normal");
  455. u8g2.drawStr(20,37,"Hard");
  456. u8g2.drawStr(20,52,"INSANE");
  457.  
  458. if (potVal<1000 and potVal>MPV-140){
  459. u8g2.drawStr(7,22,"o");
  460. if (buttonState==HIGH){
  461. client.publish("groupsix/diff", "1");
  462. diff=1;
  463. buttonState=LOW;
  464. menuState=4;
  465. }
  466. }
  467. if (potVal<MPV-140 and potVal>MPV-280){
  468. u8g2.drawStr(7,37,"o");
  469. if (buttonState==HIGH){
  470. client.publish("groupsix/diff", "2");
  471. diff=2;
  472. buttonState=LOW;
  473. menuState=4;
  474. }
  475. }
  476. if (potVal<MPV-280){
  477. u8g2.drawStr(7,52,"o");
  478. if (buttonState==HIGH){
  479. client.publish("groupsix/diff", "3");
  480. diff=3;
  481. buttonState=LOW;
  482. menuState=4;
  483. }
  484. }
  485. u8g2.sendBuffer();
  486.  
  487. }
  488. //READY? State
  489. if (menuState == 4){
  490. u8g2.clearBuffer();
  491. u8g2.setFont(u8g2_font_ncenB14_tr);
  492. u8g2.drawStr(32,20,"PRESS");
  493. u8g2.drawStr(35,40,"WHEN");
  494. u8g2.drawStr(30,60,"READY");
  495. u8g2.sendBuffer();
  496.  
  497. if (buttonState == HIGH){
  498. state = 4;
  499. timeStarted=timeP;
  500. takeOuts = 0 ;
  501. buttonsPressed = 0 ; //Reset så man ikke kan snyde og trykke før tid
  502. resetSwitchCounter = 0;
  503. checkRun = 0;
  504. runTime = 0;
  505. buttonNo = 0;
  506. oldDirection = 10;
  507. newDirection = 11;
  508. but1Pressed = 0;
  509. but2Pressed = 0;
  510. but3Pressed = 0;
  511. but1CheckRun = 0;
  512. but2CheckRun = 0;
  513. but3CheckRun = 0;
  514. but1StartTime = 0;
  515. but2StartTime = 0;
  516. but3StartTime = 0;
  517. endgameCheck = 0;
  518.  
  519. if (gamemode==2){
  520. if (numberOfThings==1){
  521. client.publish("groupsix/turretBegin", "1");
  522. }
  523. else if (numberOfThings==2){
  524. client.publish("groupsix/turretBegin", "2");
  525. }
  526. else if (numberOfThings==3){
  527. client.publish("groupsix/turretBegin", "3");
  528. }
  529. }
  530. else{
  531. client.publish("groupsix/turretBegin", "3");
  532. }
  533.  
  534.  
  535. client.publish("groupsix/turretBegin", "3"); //fix - den skal ændre sige efter omd e skal døøøøø
  536. buttonState = LOW;
  537. }
  538. }
  539. }
  540.  
  541. if (state == 4){
  542. //Screen information
  543. //Overskrift
  544. u8g2.clearBuffer();
  545. u8g2.setFont(u8g2_font_chroma48medium8_8r);
  546. ingameTime=(timeP-timeStarted)/100;
  547.  
  548. if (gamemode == 1){
  549. u8g2.drawStr(0,7,"Jail escape!");
  550. }
  551. else if (gamemode == 2){
  552. u8g2.drawStr(0,7,"Take out!");
  553. }
  554. else if (gamemode == 3){
  555. u8g2.drawStr(0,7,"Agent COOP!");
  556. }
  557.  
  558. //Difficulty
  559. if (diff == 1){
  560. u8g2.drawStr(0,16,"Normal");
  561. }
  562. else if (diff == 2){
  563. u8g2.drawStr(0,16,"Hard ");
  564. }
  565. else if (diff == 3){
  566. u8g2.drawStr(0,16,"INSANE");
  567. }
  568.  
  569.  
  570. //Time
  571. u8g2.drawStr(0,30,"Time:");
  572. u8g2.setCursor(40, 30);
  573. u8g2.print(ingameTime); //evt - her kunne sættes et komma på
  574.  
  575. //Times caught
  576. u8g2.drawStr(0,39,"Times caught:");
  577. u8g2.setCursor(105, 39);
  578. u8g2.print(timesCaught);
  579.  
  580. //GAMEMODES - Special - turrets killed or buttons pressed
  581. //GAMEMODE 1 (Jail escape)
  582.  
  583. if (gamemode == 1){
  584. if (numberOfThings == 2){
  585. buttonsPressed=but2Pressed;
  586. }
  587. if (numberOfThings == 3){
  588. buttonsPressed=but2Pressed+but3Pressed;
  589. }
  590.  
  591.  
  592. if (but1Pressed == 1){
  593. if (buttonsPressed == numberOfThings-1){
  594. state=5;
  595. }
  596. but1Pressed=0;
  597. }
  598.  
  599. //Graphical
  600. u8g2.drawStr(0,48,"Pressed:");
  601. u8g2.setCursor(64, 48);
  602. u8g2.print(buttonsPressed);
  603. u8g2.drawStr(71,48,"/");
  604. u8g2.setCursor(76, 48);
  605. u8g2.print(numberOfThings);
  606. }
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613. //Gamemode 2 (Take out)
  614. if (gamemode == 2){
  615. u8g2.drawStr(0,48,"Take outs:");
  616. u8g2.setCursor(80, 48);
  617. u8g2.print(takeOuts);
  618. u8g2.drawStr(87,48,"/");
  619. u8g2.setCursor(92, 48);
  620. u8g2.print(numberOfThings);
  621.  
  622. if (but1CheckRun == 0){
  623. if (but1Pressed == 1){
  624. client.publish("groupsix/reset", "1");
  625. takeOuts=takeOuts+1;
  626. but1CheckRun = 1;
  627. }
  628. }
  629.  
  630. if (numberOfThings>=2){
  631. if (but2CheckRun == 0){
  632. if (but2Pressed == 1){
  633. client.publish("groupsix/stopTurrets", "2");
  634. takeOuts=takeOuts+1;
  635. but2CheckRun = 1;
  636. }
  637. }
  638. }
  639. if (numberOfThings>=3){
  640. if (but3CheckRun == 0){
  641. if (but3Pressed == 1){
  642. client.publish("groupsix/stopTurrets", "3");
  643. takeOuts=takeOuts+1;
  644. but3CheckRun = 1;
  645. }
  646. }
  647. }
  648. if (but1CheckRun+but2CheckRun+but3CheckRun==numberOfThings){
  649. state=5; //u8g2.drawStr(0,48,"Pressed: O O O");
  650. }
  651. }
  652.  
  653.  
  654. //Gamemode 3 (COOP)
  655. if (gamemode == 3){
  656. u8g2.drawStr(0,48,"Pressed:");
  657. if (but1CheckRun == 0){
  658. but1StartTime=timeP;
  659. }
  660. if (but1Pressed == 1){
  661. but1CheckRun = 1;
  662. if (timeP-but1StartTime>3000){
  663. but1Pressed = 0;
  664. but1CheckRun = 0;
  665. }
  666. }
  667. if (but2CheckRun == 0){
  668. but2StartTime=timeP;
  669. }
  670. if (but2Pressed == 1){
  671. but2CheckRun = 1;
  672. if (timeP-but2StartTime>3000){
  673. but2Pressed = 0;
  674. but2CheckRun = 0;
  675. }
  676. }
  677.  
  678. if (numberOfThings==2){ //2, da det er anden selection på menuen, men faktisk betyder det at 3 knapper skal aktiveres.
  679. if (but3CheckRun == 0){
  680. but3StartTime=timeP;
  681. }
  682. if (but3Pressed == 1){
  683. but3CheckRun = 1;
  684. if (timeP-but3StartTime>3000){
  685. but3Pressed = 0;
  686. but3CheckRun = 0;
  687. }
  688. }
  689. }
  690.  
  691. if (but1CheckRun == 1){
  692. u8g2.drawStr(70,48,"o");
  693. }
  694. if (but2CheckRun == 1){
  695. u8g2.drawStr(80,48,"o");
  696. }
  697. if (but3CheckRun == 1){
  698. u8g2.drawStr(90,48,"o");
  699. }
  700.  
  701.  
  702. if (but1CheckRun+but2CheckRun+but3CheckRun==numberOfThings+1){
  703. state=5; //u8g2.drawStr(0,48,"Pressed: O O O");
  704. }
  705. }
  706.  
  707. //Turret controls
  708. //SELECT TURRET
  709. if (buttonState == HIGH and controlLimiter == 0){
  710. nowControlling=(nowControlling+1)%4;
  711. if (nowControlling == 1){
  712. client.publish("groupsix/nowControlling", "1");
  713. }
  714. if (nowControlling == 2){
  715. client.publish("groupsix/nowControlling", "2");
  716. }
  717. if (nowControlling == 3){
  718. client.publish("groupsix/nowControlling", "3");
  719. }
  720. if (nowControlling == 0){
  721. client.publish("groupsix/nowControlling", "0");
  722. }
  723.  
  724. controlLimiter = 1;
  725. }
  726.  
  727. //Alting blev spammet hvilket var hårdt for turrets. Derfor limiter
  728. else if (buttonState == LOW and controlLimiter == 1){
  729. controlLimiter = 0;
  730. }
  731.  
  732. //CONTROL TURRET
  733. if (potVal>720){
  734. if (oldDirection != newDirection){
  735. client.publish("groupsix/turretDirections", "1");
  736. oldDirection = 1;
  737. }
  738. u8g2.drawStr(90,62,"SLOW");
  739. newDirection = 1;
  740. }
  741.  
  742. if (potVal>540 and potVal<720){
  743. if (oldDirection != newDirection){
  744. client.publish("groupsix/turretDirections", "2");
  745. oldDirection = 2;
  746. }
  747. u8g2.drawStr(90,62,"slow");
  748. newDirection = 2;
  749. }
  750. if (potVal>360 and potVal<540){
  751. if (oldDirection != newDirection){
  752. client.publish("groupsix/turretDirections", "3");
  753. oldDirection = 3;
  754. }
  755. u8g2.drawStr(90,62,"med");
  756. newDirection = 3;
  757. }
  758. if (potVal>180 and potVal<360){
  759. if (oldDirection != newDirection){
  760. client.publish("groupsix/turretDirections", "4");
  761. oldDirection = 4;
  762. }
  763. u8g2.drawStr(90,62,"fast");
  764. newDirection = 4;
  765. }
  766. if (potVal<180){
  767. if (oldDirection != newDirection){
  768. client.publish("groupsix/turretDirections", "5");
  769. oldDirection = 5;
  770. }
  771. u8g2.drawStr(90,62,"FAST");
  772. newDirection = 5;
  773. }
  774.  
  775. //Turret screen
  776. u8g2.drawStr(0,62,"Controls");
  777. u8g2.setCursor(73, 62);
  778. u8g2.print(nowControlling);
  779. u8g2.sendBuffer();
  780. }
  781.  
  782.  
  783. if(state==5){
  784. u8g2.clearBuffer();
  785. u8g2.setFont(u8g2_font_ncenB14_tr);
  786. u8g2.drawStr(0,20,"Finished");
  787.  
  788. //Gamemode
  789. u8g2.setFont(u8g2_font_chroma48medium8_8r);
  790. if (gamemode == 1){
  791. u8g2.drawStr(0,30,"Jail escape!");
  792. }
  793. else if (gamemode == 2){
  794. u8g2.drawStr(0,30,"Take out!");
  795. }
  796. else if (gamemode == 3){
  797. u8g2.drawStr(0,30,"Agent COOP!");
  798. }
  799.  
  800. //Difficulty
  801. if (diff == 1){
  802. u8g2.drawStr(90,10,"Nor");
  803. u8g2.drawStr(90,20,"mal");
  804.  
  805. }
  806. else if (diff == 2){
  807. u8g2.drawStr(90,10,"Hard ");
  808. }
  809. else if (diff == 3){
  810. u8g2.drawStr(90,12,"INS");
  811. u8g2.drawStr(90,20,"ANE");
  812. }
  813.  
  814. //Time
  815. u8g2.drawStr(0,40,"Time");
  816. u8g2.setCursor(40, 40);
  817. u8g2.print(ingameTime); //FIX
  818.  
  819. //Caught
  820. u8g2.drawStr(0,50,"Times caught:");
  821. u8g2.setCursor(105, 50);
  822. u8g2.print(timesCaught);
  823.  
  824. //play again?
  825. u8g2.drawStr(0,60,"Press to restart!");
  826.  
  827. if (buzzerLimiter == 0){
  828. client.publish("groupsix/reset", "1");
  829. tone(buzzer, 1000);
  830. delay(70);
  831. noTone(buzzer);
  832. delay(20);
  833. tone(buzzer, 1000);
  834. delay(100);
  835. tone(buzzer, 1200);
  836. delay(100);
  837. tone(buzzer, 1300);
  838. delay(100);
  839. tone(buzzer, 1500);
  840. delay(700);
  841. noTone(buzzer);
  842. buzzerLimiter = 1;
  843. }
  844.  
  845. u8g2.sendBuffer();
  846. if (endgameCheck == 0){
  847. delay(1500); //så man ikke kommer til at springe over hvis man sidder og spammer knapper
  848. endgameCheck = 1;
  849. }
  850.  
  851. if (buttonState==HIGH){
  852. state=6;
  853. buttonState=LOW;
  854. }
  855. }
  856.  
  857. if (state == 6){
  858. resetSwitchCounter = 0;
  859. checkRun = 0;
  860. runTime = 0;
  861. gamemode=0;
  862. buttonNo = 0;
  863. diff = 0;
  864. numberOfThings = 0;
  865. hugeNumber = 0;
  866. timeStarted = 0;
  867. nowControlling = 0;
  868. oldDirection = 10;
  869. newDirection = 11;
  870. but1Pressed = 0;
  871. but2Pressed = 0;
  872. but3Pressed = 0;
  873. buttonsPressed = 0;
  874. takeOuts = 0;
  875. but1CheckRun = 0;
  876. but2CheckRun = 0;
  877. but3CheckRun = 0;
  878. but1StartTime = 0;
  879. but2StartTime = 0;
  880. but3StartTime = 0;
  881. timesCaught = 0;
  882. endgameCheck = 0;
  883. buzzerLimiter = 0;
  884. menuState = 1;
  885. state = 3;
  886. if (antiSpamReset==0){ //Reset bliver meget langsomt hvis de her bare spammes
  887. client.publish("groupsix/reset", "1");
  888. client.publish("groupsix/reset", "2");
  889. client.publish("groupsix/reset", "3");
  890. client.publish("groupsix/turretBegin", "0");
  891. antiSpamReset=1;
  892. }
  893. }
  894. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement