Hi! I am currently facing some problems when trying to use a flag to do a particular action only once. I wanted to print the value to move forward and turn left and continue to move forward all the way but this coding that I have done, it returns me forward, turn left, forward, turn right, forward, turn right and so on... Can anyone help me with this? Thank You so much!
I have found out the way to solve this problem and would like to share it here. The codes are as follows:
int set1_flag = 0;
int previousAngle = 270;
int angleDouble = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (angleDouble >= 0 && angleDouble < 90) //testing range for 270 and 360 turn left only
{
Serial.println("Forward");
delay(2000);
if(set1_flag == 0)
{
if ((angleDouble <= previousAngle - 270))
{
Serial.println("Left");
Serial.print("Minus Angle: ");
Serial.println(previousAngle - 270);
Serial.println();
set1_flag = 1;
delay(1000);
}
else
{
Serial.println("Right Side");
set1_flag = 1;
delay(1000);
}
}
}
}