#include #include //game variables bool inprogress = false; bool ss = false; //searching ship bool DS = false; //ship detecting bool D = false; //Destroying ship bool MM = false; //motor movement bool II = false; //instructional input int sensor, counter = 0; const int stepPin = 32; const int dirPin = 33; const int stepPin1 = 34; const int dirPin1 = 35; // step limits 1050 for both axis int z = 500; // delay after each step int b = 50; // number of steps int xpin = 45, ypin = 44; int x_counter, y_counter; bool sensorState[6] = {true, true, true, true, true, true}; // for single digit display byte num[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82}; //, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e}; int latchPin = 12; int clockPin = 13; int dataPin = 11; String ESPData; void setup() { Serial.begin(9600); Serial1.begin(9600); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, num[0] ^ 0xff); digitalWrite(latchPin, HIGH); pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); pinMode(stepPin1, OUTPUT); pinMode(dirPin1, OUTPUT); pinMode(xpin, INPUT); pinMode(ypin, INPUT); } void loop() { GameStart(); SearchingShip(); DetectingShip(); Destroyingship(); GameOver(); } void GameStart() { if (inprogress == false) { inprogress = true; reset(); ss = true; DS = true; } } void GameOver() { ss = false; DS = false; D = false; } void SearchingShip() { if (ss == true) { InstructionalInput(); MotorMovement(); /* if (MM != true) { Serial.print("MM_on"); } MM = true; */ } } void reset() { while (digitalRead(xpin) != HIGH) { digitalWrite(dirPin, HIGH); digitalWrite(dirPin1, HIGH); for (int i = 0; i < 10; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(500); digitalWrite(stepPin, LOW); delayMicroseconds(500); digitalWrite(stepPin1, HIGH); delayMicroseconds(500); digitalWrite(stepPin1, LOW); delayMicroseconds(500); } } x_counter = 0; while (digitalRead(ypin) != HIGH) { digitalWrite(dirPin, LOW); digitalWrite(dirPin1, HIGH); for (int i = 0; i < 10; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(500); digitalWrite(stepPin, LOW); delayMicroseconds(500); digitalWrite(stepPin1, HIGH); delayMicroseconds(500); digitalWrite(stepPin1, LOW); delayMicroseconds(500); } } y_counter = 0; } void MotorMovement() { if (Serial1.available()) { //Checks if there is data in UART1 ESPData = Serial1.readString(); //Serial.print(ESPData); if (ESPData.indexOf("left") >= 0 && y_counter < 1050) { digitalWrite(dirPin, HIGH); digitalWrite(dirPin1, LOW); stepping(); y_counter += 50; stepping(); y_counter += 50; stepping(); y_counter += 50; } if (ESPData.indexOf("right") >= 0 && digitalRead(ypin) == LOW) { digitalWrite(dirPin, LOW); digitalWrite(dirPin1, HIGH); stepping(); y_counter -= 50; stepping(); y_counter -= 50; stepping(); y_counter -= 50; } if (ESPData.indexOf("up") >= 0 && x_counter < 1050) { digitalWrite(dirPin, LOW); digitalWrite(dirPin1, LOW); stepping(); x_counter += 50; stepping(); x_counter += 50; stepping(); x_counter += 50; } if (ESPData.indexOf("down") >= 0 && (digitalRead(xpin) == LOW)) { digitalWrite(dirPin, HIGH); digitalWrite(dirPin1, HIGH); stepping(); x_counter -= 50; stepping(); x_counter -= 50; stepping(); x_counter -= 50; } if (ESPData.indexOf("stop") >= 0) { digitalWrite(stepPin, LOW); digitalWrite(dirPin, LOW); digitalWrite(stepPin1, LOW); digitalWrite(dirPin1, LOW); } } } void stepping() { for (int i = 0; i < b; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(z); digitalWrite(stepPin, LOW); delayMicroseconds(z); digitalWrite(stepPin1, HIGH); delayMicroseconds(z); digitalWrite(stepPin1, LOW); delayMicroseconds(z); } } void InstructionalInput() { int x, y; x = analogRead(A1); y = analogRead(A0); if (x > 800) { Serial.println("LCD_L"); delay(500); } else if (x < 200) { Serial.println("LCD_R"); delay(500); } else if (y > 800) { Serial.println("LCD_D"); delay(500); } else if (y < 200) { Serial.println("LCD_U"); delay(500); } } void DetectingShip() { if (DS == true) { int s[6]; s[0] = analogRead(A2); s[1] = analogRead(A3); s[2] = analogRead(A9); s[3] = analogRead(A5); s[4] = analogRead(A8); s[5] = analogRead(A4); for (int i = 0; i < 6; i++) { if (s[i] > 100 && sensorState[i] == true) { //change value for light sensor = i + 1; sensorState[i] = false; ss = false; //Serial.print("MM_off"); DS = false; D = true; delay(500); switch (sensor) { case 1: Serial.println("S1"); break; case 2: Serial.println("S2"); break; case 3: Serial.println("S3"); break; case 4: Serial.println("S4"); break; case 5: Serial.println("S5"); break; case 6: Serial.println("S6"); break; } } } } } void Destroyingship() { while (D == true) { if (Serial1.available()) { //Checks if there is data in UART1 ESPData = Serial1.readString(); //Serial.print(ESPData); if (ESPData.indexOf("Shipping station destroyed") >= 0) { counter++; digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, num[counter] ^ 0xff); digitalWrite(latchPin, HIGH); D = false; ss = true; //Serial.print("MM_on"); DS = true; } if (counter == 6) { GameOver(); } } } } /* when received directional input from wifi move stepper motor in direction count steps of stepper motor set max steps when limit switch is closed stop stepper motor in that direction reset step count to 0 when steps of motor reaches limit stop stepper motor in that direction when standby joystick inputed send string of directional input to wifi when photoresistor analogRead above certain amount stop stepper motor movement, stop standby joystick instruction, and send string "shipping port detected" and send which sensor task to do when received that sensor task is completed from wifi counter increased by 1 turn off pin for that sensor's photoresistor resume stepper motor movement and standby joystick instruction when counter reaches 6 end game */