while loop not working (pololu 3pi)

help!! We want our robot to turn 180 degrees to face a light at the opposite end of a square track. We want the robot to then drive towards that light. When it reaches the light, the light switches off. Another light turns on to the right of the robot at the other corner of the track. We want the robot to turn 90 degrees so its faceing the light and move towards it, then repeat the last part again for a third light. Our robot at the moment it turns 180 degrees, turns 90 degrees (x3) then starts again… HELP :fearful: :frowning:

#include <OrangutanLCD.h>
#include <OrangutanMotors.h>

int LeftLDR = 6;
int RightLDR = 7;
int AverageLDR = 0;
int i = 0;

void setup()
{
  pinMode(LeftLDR,INPUT);
  pinMode(RightLDR,INPUT);
  delay(5000);
set_motors(20,-20);
delay(1800);
set_motors(0,0);
long LLDR = analogRead(LeftLDR);
long RLDR = analogRead(RightLDR);
LLDR = map(LLDR,0,1023,100,0);
RLDR = map(RLDR,0,1023,100,0);
AverageLDR = (LLDR + RLDR)/2;
print_long(AverageLDR);
delay(1000);
clear();
if(AverageLDR >= 40)
{
set_motors(40,40);
delay(10);
}
else
{
set_motors(0,0);
delay(1000);
set_motors(20,-20);
delay(900);
set_motors(0,0);
delay(2000);
}
while(i<=2)
if(AverageLDR >= 40)
{
set_motors(40,40);
delay(10);
}
else
{
set_motors(0,0);
delay(1000);
set_motors(20,-20);
delay(900);
set_motors(0,0);
delay(2000);
i++;
}
}
void loop()
{

}

Moderator edit: Please, please use code tags.

Welcome to the Forum. Please read the two posts by Nick Gammon at the top of the Forum for guidelines on posting to the Forum, especially the use of code tags for source code listings. Also, while your code is still in the IDE, use Ctrl-T to reformat it before posting; it makes it easier to read, which makes it easier for us to help you.

What do your debug prints tell you, and us?

You haven't got any debug prints. Why not?

In your code, you define two long variables, LLDR and RLDR, which receive data from analogRead(). However, analogRead() returns an int, so there's no reason to make them a long.

Next, it would seem that most of this code should appear in loop() rather than setup(), as you want things to change over time and setup() is only run once. Perhaps only the pinMode() calls belong in setup().

Finally, I am not familiar with the Orangutan library, but it would seem to me that you need to have instantiated an Orangutan object somewhere (probably as a global) which is used to access the set_motors() and clear() method calls. Go into their library and look in the Examples directory to see how to instantiate an object.

AWOL: What do your debug prints tell you, and us?

You haven't got any debug prints. Why not?

"Binary sketch size: 3,118 bytes (of a 32,768 byte maximum)"

econjack: In your code, you define two long variables, LLDR and RLDR, which receive data from analogRead(). However, analogRead() returns an int, so there's no reason to make them a long.

Next, it would seem that most of this code should appear in loop() rather than setup(), as you want things to change over time and setup() is only run once. Perhaps only the pinMode() calls belong in setup().

Finally, I am not familiar with the Orangutan library, but it would seem to me that you need to have instantiated an Orangutan object somewhere (probably as a global) which is used to access the set_motors() and clear() method calls. Go into their library and look in the Examples directory to see how to instantiate an object.

We thought it was to go in the loop() but were unsure, we're unsure about the while loop and how it works. Would a for loop be better in this situation in your opinion? thank for all the help :)

OK, that's the size of your sketch and there's plenty of memory spare, but you still haven't got any debug prints which would tell you what your sketch is doing.

AWOL: OK, that's the size of your sketch and there's plenty of memory spare, but you still haven't got any debug prints which would tell you what your sketch is doing.

could you explain what a Debug Print is? sorry im just new to this, we only started in March. Does it mean me printing code to display as the process goes on?

If you look at the examples provided with the IDE, you'll see stuff like

Serial.begin (9600);
...
...
Serial.print ("A is ");
Serial.println (A);

If you print the values of the input variables to your algorithm, you might be able to see what it is doing with those values.

  while(i<=2)
    if(AverageLDR >= 40)
    {
      set_motors(40,40);
      delay(10);
    }
    else
    {
      set_motors(0,0);
      delay(1000);
      set_motors(20,-20);
      delay(900);
      set_motors(0,0);
      delay(2000);
      i++;
    }

If you look at your while loop, on entry, if the value of “AverageLDR” is >= 40, the
variable “i” never gets incremented. Because “AverageLDR” is never recalculated, you’ll
never leave the while loop.

I just went to this site:

https://www.pololu.com/docs/0J17/5.e

and found a sample program that uses the same library. Near the top of the program are the following lines:

OrangutanAnalog analog;
OrangutanLEDs leds;
OrangutanMotors motors;

These three statements create Orangutan object variables in your program that are used for the motor control. I suggest you start with their example (there may be others) and learn from those.

AWOL's suggestion of debug prints would be something like this:

Serial.begin(9600);                       // Add this line to setup()

// these line belong in loop()
motors.set_motors(0,0);                   // Note how this uses the motors object defined earlier
long LLDR = analogRead(LeftLDR);
Serial.print("LLDR = ");                  // Debug print statements to look at LLDR
Serial.println(LLDR);

long RLDR = analogRead(RightLDR);
Serial.print("RLDR = ");                  // Debug print statements to look at LLDR
Serial.println(RLDR );

Reformat your code with Ctrl-T. After the program is loaded, run the Serial monitor (Ctrl-Shift-M)