int sx = 2; //Step for the x-axis int dirx = 3; // direction for the x-axis. int sy = 4; //Step for the y-axis. int diry = 5; //direction for the y-axis. //prox_sensors //current step. int curstepx; int curstepy; // joystick int joyx; int joyy; int state; const int inPressed = 6; //joystick button //the proximitysensors. int prox1; int prox2; int proxpos1 = 50; int proxpos2 = 150; int movexstep; int fart = 5; int notPressed = 0; int relay = 7; int trigPin1 = 8; // Trigger int echoPin1 = 9; // Echo int trigPin2 = 10; int echoPin2 = 11; int i; void setup() { //Defining the variables. Serial.begin(9600); //Sets the baud for serial data transmission pinMode(sx, OUTPUT); pinMode(dirx, OUTPUT); pinMode(sy, OUTPUT); pinMode(diry, OUTPUT); pinMode(A0, INPUT); pinMode(A1, INPUT); pinMode(inPressed, INPUT_PULLUP); pinMode(relay, OUTPUT); } void loop() { // // long duration1, distance1; // digitalWrite(trigPin1, LOW); // Added this line // delayMicroseconds(2); // Added this line // digitalWrite(trigPin1, HIGH); // delayMicroseconds(10); // Added this line // digitalWrite(trigPin1, LOW); // duration1 = pulseIn(echoPin1, HIGH); // distance1 = (duration1/2) / 29.1; //// //// if (distance1 < 10 || distance1 <= 0){ //// Serial.println("SHOOT!"); //// digitalWrite(relay, HIGH); //// delay(1000); //// digitalWrite(relay, LOW); //// //// //// } // Serial.print("Sensor1 "); // Serial.print(distance1); // Serial.println("cm"); // // // //// delay(2); // } //long duration2, distance2; // digitalWrite(trigPin2, LOW); // Added this line // delayMicroseconds(2); // Added this line // digitalWrite(trigPin2, HIGH); // delayMicroseconds(10); // Added this line // digitalWrite(trigPin2, LOW); // duration2 = pulseIn(echoPin2, HIGH); // distance2= (duration2/2) / 29.1; // if (distance2 <= 10 ){ // Serial.println("BEHIND ME!"); // for (i = 0; i < 100; i++) { // digitalWrite(dirx, LOW); // digitalWrite(sx, HIGH); // delay (fart); // digitalWrite(sx, LOW); // delay(fart); // curstepx++;} // // } // // } // // else { // Serial.print("Sensor2 "); // Serial.print(distance2); // Serial.println("cm"); // } if(Serial.available() > 0) { state = Serial.read(); //Read the incoming data and store it into variable Incoming_value Serial.print(state); //Print Value of Incoming_value in Serial monitor Serial.print("\n"); //New line } //Read the joystick. joyx = analogRead(A0); joyy = analogRead(A1); notPressed = digitalRead(inPressed); if ( notPressed == 0 || state == 8) { // Serial.print(notPressed); digitalWrite(relay, HIGH); } else{ digitalWrite(relay, LOW); } // When these terms are true, the axis will do nothing. if (joyx < 550 && joyx > 450 || joyy < 550 && joyy > 450 ) { digitalWrite(dirx, LOW); digitalWrite(diry,LOW); digitalWrite(sx, LOW); digitalWrite(sy, LOW); // digitalWrite(relay, LOW); } //The x, axis will move left. if (joyx > 550 || state == 1 ) { digitalWrite(dirx, LOW); digitalWrite(sx, HIGH); delay (fart); digitalWrite(sx, LOW); delay(fart); curstepx++; } //The axis will start moving right. if (joyx < 450 || state == 2) { digitalWrite(dirx, HIGH); digitalWrite(sx, HIGH); delay (fart); digitalWrite(sx, LOW); delay(fart); curstepx--; } // The y-axis will start moving up. if (joyy > 550 || state == 4) { digitalWrite(diry, LOW); digitalWrite(sy, HIGH); delay (fart); digitalWrite(sy, LOW); delay(fart); curstepy++; } // The y-axis will start moving down. if (joyy < 450 || state == 5) { digitalWrite(diry, HIGH); digitalWrite(sy, HIGH); delay (fart); digitalWrite(sy, LOW); delay(fart); curstepy--; }