//************************************************************************************************************************************************** // This procedure reads the serial port - Serial1 - for bluetooth commands being sent from the Android device void bluetooth() { while (Serial1.available()) // Read bluetooth commands over Serial1 // Warning: If an error with Serial1 occurs, make sure Arduino Mega 2560 is Selected { { str = Serial1.readStringUntil('\n'); // str is the temporary variable for storing the last sring sent over bluetooth from Android device //Serial.print(str); // for testing purposes } //--------------------------------------------------- if (str.startsWith("GPS")) { String lati = str.substring(3, 12); bluelati = lati.toDouble(); Serial.print("APP latitude: "); Serial.println(bluelati, 6); } if (str.startsWith("GPS")) { String longi = str.substring(12, 21); bluelongi = longi.toDouble(); Serial.print("APP longitude: "); Serial.println(bluelongi, 6); } //------------------------------------------------------- blueToothVal = (str.toInt()); // convert the string 'str' into an integer and assign it to blueToothVal Serial.print("BlueTooth Value "); Serial.println(blueToothVal); // ************************************************************************************************************************************************** switch (blueToothVal) { case 1: Serial1.println("Forward"); Forward(); //break; case 2: //Serial1.println("Reverse"); //Reverse(); break; case 3: Serial1.println("Left"); LeftTurn(); //StopCar(); break; case 4: Serial1.println("Right"); RightTurn(); //StopCar(); break; case 5: Serial1.println("Stop "); StopCar(); break; case 6: setWaypoint(); break; case 7: goWaypoint(); break; case 8: //Serial1.println("Turn Around"); //turnAround(); break; case 9: //Serial1.println("Compass Forward"); //setHeading(); //Compass_Forward(); break; case 10: //setHeading(); break; case 11: gpsInfo(); break; case 12: //Serial1.println("Compass Turn Right"); //CompassTurnRight(); break; case 13: //Serial1.println("Compass Turn Left"); //CompassTurnLeft(); break; case 14: //Serial1.println("Calibrate Compass"); //calibrateCompass(); break; case 15: //pingToggle(); break; case 16: clearWaypoints(); break; case 17: // finish with waypoints ac = 0; Serial1.print("Waypoints Complete"); break; case 18: Serial1.println("Long Left"); LongLeftTurn(); //StopCar(); stepAcc = 0; break; case 19: Serial1.println("Long Right"); LongRightTurn(); //StopCar(); stepAcc = 0; break; case 20: storeWaypoints(); //Šalje aktualne NEO GPS-kordinate iz Arduina u MitApp aplikaciju break; } // end of switch case // ************************************************************************************************************************************************** // Slider Value for Speed if (blueToothVal) { Serial.println(blueToothVal); if (blueToothVal >= 1000) { Serial1.print("Speed set To: "); Serial1.println(blueToothVal - 1000); run_Speed = (blueToothVal - 1000); Serial.println(); Serial.print("Run_PWM_Speed "); Serial.println(run_Speed); } } } // end of while loop Serial1 read // if no data from Bluetooth if (Serial1.available() < 0) // if an error occurs, confirm that the arduino mega board is selected in the Tools Menu { Serial1.println("No Bluetooth Data "); } } // end of bluetooth procedure // ************************************************************************************************************************************************** void Forward() { while(true) { bluetooth(); // Run the Bluetooth procedure to see if there is any data being sent via BT if (blueToothVal == 5){ break; } unsigned long currentMillis = millis(); if (((unsigned long)(currentMillis - previousMillis) >= interval_a) && (pwm < run_Speed)) { pwm = pwm + a; // acc analogWrite(runPin, pwm); // Start motor acc. Serial.print("PWM="); Serial.println(pwm); previousMillis = currentMillis; } else if (((unsigned long)(currentMillis - previousMillis) >= interval_d) && (run_Speed < pwm)){ pwm = pwm - a; //deacc. analogWrite(runPin, pwm); //Start motor acc. Serial.println(pwm); previousMillis = currentMillis; } } } // *********************************************************************************************************************** void StopCar() { digitalWrite(runPin, LOW); noTone(stepPin); pwm = 0; } // *********************************************************************************************************************** void RightTurn() { for (int stepCount = 0 ; stepCount <= 800; stepCount += 1) { //step count 25*16 za 5°; 50*16 za 10°; 100*16 za 20° getriba 11:100;16microstepping digitalWrite(dirPin, LOW); // Stepper motor turn CW (LOW) digitalWrite(stepPin, HIGH); delayMicroseconds(250); digitalWrite(stepPin, LOW); delayMicroseconds(250); } } // ************************************************************************************************************************************************** void LongRightTurn() { while(true) { bluetooth(); // Run the Bluetooth procedure to see if there is any data being sent via BT if (blueToothVal == 5){ break; } digitalWrite(runPin, LOW); // Stop motor digitalWrite(dirPin, LOW); // Stepper motor turn CCW (LOW) //----------------------------------------------- unsigned long currentMillis = millis(); if (((unsigned long)(currentMillis - previousMillis) >= M) && (stepAcc < Puls)) { stepAcc = stepAcc + 250; previousMillis = currentMillis; } //----------------------------------------------- tone(stepPin,stepAcc); Serial.println(stepAcc); } } // ************************************************************************************************************************************************** void LeftTurn() { for (int stepCount = 0 ; stepCount <= 800; stepCount += 1) { //step count 25*16 za 5°; 50*16 za 10°; 100*16 za 20° getriba 11:100;16microstepping digitalWrite(dirPin, HIGH);// Stepper motor se okreće suprotno od smijera kazaljke na satu HIGH digitalWrite(stepPin, HIGH); delayMicroseconds(250); digitalWrite(stepPin, LOW); delayMicroseconds(250); } } // ************************************************************************************************************************************************** void LongLeftTurn() { while(true) { bluetooth(); // Run the Bluetooth procedure to see if there is any data being sent via BT if (blueToothVal == 5){ break; } digitalWrite(runPin, LOW); // Stop motor digitalWrite(dirPin, HIGH);// Stepper motor turn CW (HIGH) //----------------------------------------------- unsigned long currentMillis = millis(); if (((unsigned long)(currentMillis - previousMillis) >= M) && (stepAcc < Puls)) { stepAcc = stepAcc + 250; previousMillis = currentMillis; } //----------------------------------------------- tone(stepPin,stepAcc); Serial.println(stepAcc); } } //---------------------------------------------------------------------------------------------------------------- void storeWaypoints() { if ((gpsCount >= 0) && (gpsCount < 1)){ getGPS(); // get the latest GPS coordinates Home_LAT[ab] = gps.location.lat(); Home_LON[ab] = gps.location.lng(); Serial1.print("#"); //Begining of line for app // send serial string to arduino "#,45.123456,13.123456 " Serial1.print(","); //separator for app Serial1.print(Home_LAT[0],6); //Latitude for app Serial1.print(","); //separator for app Serial1.print(Home_LON[0],6); //Longitude for app Serial1.println(); // end of line for app Serial.print("#"); //Begining of line for app Serial.print(","); //separator for app Serial.print(Home_LAT[0],6); //Latitude for app Serial.print(","); //separator for app Serial.print(Home_LON[0],6); //Longitude for app Serial.println(); // end of line for app delay(50); gpsCount++; Serial.print("gpsCount:"); Serial.print(gpsCount); ab++; Serial.print(" ab:"); Serial.println(ab); } else { Serial1.println("Waypoints Full"); Serial.println("Waypoints Full"); } }