Thankyou everyone for the reply! I only post a snippet of the codes because I was still editing some parts of it as the previous person working on this project added some codes that were not used in the program but I really thanked all of you for willing to guide me with my project. The codes below are the full coding:
#include <SoftPWM.h>
const byte numChars = 100;
char receivedChars[numChars]; // an array to store the received data
const int numbers = 5;
int fiveNumber[numbers];
boolean newData = false;
int i=0;
int count;
// save data sent in by bluetooth through android phone
int dataNumber = 0;
char distance[100] = {
0};
char angle[100] = {
0};
// declare variables
double x;
double angleDouble;
double delayTime;
/* declare motor pins
=================
M1 |in2,3 in4,5| M2
| |
M3 |in9,10 in11,12| M4
=================
SMART CART [PINS]
*/
// motor 1
int in2 = 2;
int in3 = 3;
// motor 2
int in4 = 4;
int in5 = 5;
// motor 3
int in9 = 9;
int in10 = 10;
// motor 4
int in11 = 11;
int in12 = 12;
void setup() {
// arduino default serial
Serial.begin(9600);
// enable softPWM
SoftPWMBegin();
SoftPWMSet(2,0);
SoftPWMSet(3,0);
SoftPWMSet(4,0);
SoftPWMSet(5,0);
SoftPWMSet(9,0);
SoftPWMSet(10,0);
SoftPWMSet(11,0);
SoftPWMSet(12,0);
// change bluetooth serial port to 115200
Serial1.begin(115200);
Serial1.print("$");
Serial1.print("$");
Serial1.print("$");
delay(100);
Serial1.begin(19200); // 19200 for bluetooth RN42
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showData();
}
void recvWithEndMarker() {
static byte ndx = 0; //local variable to store up to 8 digit numbers from 0 to 255
char endMarker = '\n'; //break line
char rc;
if (Serial1.available() > 0) { //Bluetooth connected
rc = Serial1.read(); //storing value for connected Bluetooth
if (rc != endMarker) { //connected Bluetooth is not equals to break line
receivedChars[ndx] = rc; //store the connected Bluetoothh values into an array
ndx++; //increment
if (ndx >= numChars) { //if index is more than or equals to 100
ndx = numChars - 1; //index = 100 - 1
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0; //index = 0
newData = true; //there is newData
}
}
}
void showData() {
if (newData == true) { //if there is newData
dataNumber = 0; // new for this version
dataNumber = atoi(receivedChars); // new for this version , atoi(receivedchars) is to convert all Bluetooth data stored in array to int
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(receivedChars,","); // get the first part - the string , split the values by ,
strcpy(distance, strtokIndx); // copy value to distance
Serial.print("Distance: ");
x = atof(distance); // in meters , convert string to float/double
Serial.println(x);
// get angle
strtokIndx = strtok(NULL,","); // continue , split the values by ,
strcpy(angle, strtokIndx); // copy value to angle
angleDouble = atof(angle); //convert string to float/double
Serial.print("Angle: ");
Serial.println(angleDouble);
count = 0;
if (count < 5){
fiveNumber[count] = angleDouble; //storing angle into array
count++; //increment
Serial.println(fiveNumber[count]);
}
else{
count = 0;
}
if (x > 0){ //if distance is more than 0
double delayTime = x * 10; //distance * 10
if(i > 1){
// 1 second * distance // move for amount of seconds 10ms // delay time 10 = 1s
}
}
else {
clearAll();
}
clearAll();
newData = false;
}
i++;
}
// start of motor wheel movements
void forward(){
SoftPWMSetPercent(in2, 200); //motor 1
SoftPWMSetPercent(in4, 200); //motor 2
SoftPWMSetPercent(in9, 199); //motor 3
SoftPWMSetPercent(in11, 199); //motor 4
delay(1000);
}
void backward(int speed) {
SoftPWMSetPercent(in3, speed); //motor 1
SoftPWMSetPercent(in5, speed); //motor 2
SoftPWMSetPercent(in10, speed); //motor 3
SoftPWMSetPercent(in12, speed); //motor 4
delay(1000);
}
void turnLeft(int speed){
SoftPWMSetPercent(in4, speed); //motor 2 forward
SoftPWMSetPercent(in11, speed); //motor 4 forward
SoftPWMSetPercent(in3, speed); //motor 1 backward
SoftPWMSetPercent(in10, speed); //motor 3 backward
delay(1000);
}
void turnRight(int speed){
SoftPWMSetPercent(in2, speed); //motor 1 forward
SoftPWMSetPercent(in9, speed); //motor 3 forward
SoftPWMSetPercent(in5, speed); //motor 2 backward
SoftPWMSetPercent(in12, speed); //motor 4 backward
delay(1000);
}
int clear = 0; //set clear to 0
void clearAll(){ //set all speed of the motors to 0
SoftPWMSetPercent(in2, clear);
SoftPWMSetPercent(in3, clear);
SoftPWMSetPercent(in4, clear);
SoftPWMSetPercent(in5, clear);
SoftPWMSetPercent(in9, clear);
SoftPWMSetPercent(in10, clear);
SoftPWMSetPercent(in11, clear);
SoftPWMSetPercent(in12, clear);
}
// end of motor wheel movements