Hi! I am have written an if else statement to compare 2 angles. I have set a variable for current angle and previous angle. How do I ignore the first previous angle at the start when doing comparison? The codes below are the codes that I have written:
#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, previousAngle;
double delayTime;
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);
angleDouble = 0;
previousAngle = 0;
Serial1.begin(19200); // 19200 for bluetooth RN42
Serial.println("<Arduino is ready>");
clearAll();
delay(30);
}
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
previousAngle = angleDouble; //storing previous angle value
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);
if (angleDouble <= angleDouble - 90) //wrong coding
{
Serial.println("Turn Right");
}
else if (angleDouble >= angleDouble + 90) //wrong coding
{
Serial.println("Turn Left");
}
}