ArduinoRobot doesn't move

Hey there, I have a problem with my sketch:

#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>

const int serialPeriod = 500;  // Gibt alle viertel Sekunden die Werte auf der Konsole wieder
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;
unsigned long timeLoopDelay = 0;

// Benennt den Echo & Trig Pin die für den Ultraschallsensor gebraucht werden
const int ultrasonic2TrigPin = TKD3;
const int ultrasonic2EchoPin = TKD4;

int ultrasonic2Distance;
int ultrasonic2Duration;

// definiere Werte
#define DRIVE_FORWARD    0
#define TURN_LEFT        1

int state = DRIVE_FORWARD;


void setup() 
{
     Serial.begin(9600);  //Übertragungsgeschwindigkeit für die Kommunikation mit dem Computer
     Robot.begin();
     Robot.beginTFT();

     // Ultraschallsensor Pin Konfiguration
     pinMode (ultrasonic2TrigPin, OUTPUT);
     pinMode (ultrasonic2EchoPin, INPUT);
}

void loop() 
{

     debugOutput(); // Gibt die Werte des Ultraschalssensor's in der Konsole an

     if(millis() - timeLoopDelay >= loopPeriod)
     {
          readUltrasonicSensors(); //ließt und speichert die gemessene Distanz
          
          stateMachine();
          
          timeLoopDelay = millis();
     }
}

void stateMachine()
{
     if(state == DRIVE_FORWARD) // keine Hindernisse vor mir
     {
          if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // Wenn der Weg frei ist
          {
               //Fahre vorwärts            
               Robot.motorsWrite(128, 128);
              
          }
          else  // Hindernis vor mir
          {
               state = TURN_LEFT;
          }
     }
     else if(state == TURN_LEFT) // Hindernis im Weg, dreh nach links
     {
          unsigned long timeToTurnLeft = 1100; //  Es braucht -+ 1.1 Sekunden um 90° zu drehen
          
          unsigned long turnStartTime = millis();  //Speichert die Zeit als der Robot mit dem Drehen gestartet ist
          
          while((millis()-turnStartTime < timeToTurnLeft)) // bleibt in der Schleife bis timeToTurnLeft (1.1 Sekunden) vorbei ist
        {
               // dreh nach links
              Robot.turn(90);
        }
     
          state = DRIVE_FORWARD;
     }
}

void readUltrasonicSensors()
{
     // Ultraschalssensor 2
     
     Robot.digitalWrite(ultrasonic2TrigPin, HIGH);
     delayMicroseconds(10);                 // der TrigPin muss für mindestens 10 us an bleiben
     Robot.digitalWrite(ultrasonic2TrigPin, LOW);      
     
     ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
     ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}


void debugOutput()
{
    if((millis() - timeSerialDelay) > serialPeriod)
    {
      Robot.print("ultrasonic2Distance: ");
      Robot.print(ultrasonic2Distance);
      Robot.print("cm");
      Robot.println();
      
      timeSerialDelay = millis();
    }
}

the compiler say there are no errors but when I upload the sketch to the ArduinoRobot, he doesn't move, he just give the distance between him and an object on the tft screen out.
P.S: It,s the original Robot from Arduino, http://arduino.cc/en/Main/Robot

          unsigned long timeToTurnLeft = 1100; //  Es braucht -+ 1.1 Sekunden um 90° zu drehen
          
          unsigned long turnStartTime = millis();  //Speichert die Zeit als der Robot mit dem Drehen gestartet ist
          
          while((millis()-turnStartTime < timeToTurnLeft)) // bleibt in der Schleife bis timeToTurnLeft (1.1 Sekunden) vorbei ist
        {
               // dreh nach links
              Robot.turn(90);
        }

How is this any improvement over:

    Robot.turn(90);
    delay(timeToTurnLeft);

What is state when stateMachine() is called?

The else if statement looks like it should be an if statement. You've decided that turning is needed, but that happens in the if block, so the else statement, where turning happens, is skipped.

Does the distance shown on the screen vary when an object is placed in front of the sensor ?

    if(millis() - timeLoopDelay >= loopPeriod)
     {
       //print a message here - do you see it when the program is run ?
          readUltrasonicSensors(); //ließt und speichert die gemessene Distanz

See my comment in the code.

I would make the loopPeriod variable an unsigned long to match millis() and timeLoopDelay but I don't think that it is actually causing a problem.

How is the robot powered ?

Have you tried taking out all the state machine and just making the robot go forward? Does that work?

#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>

const int serialPeriod = 500; // Gibt alle viertel Sekunden die Werte auf der Konsole wieder
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;
unsigned long timeLoopDelay = 0;

// Benennt den Echo & Trig Pin die für den Ultraschallsensor gebraucht werden
const int ultrasonic2TrigPin = TKD3;
const int ultrasonic2EchoPin = TKD4;

int ultrasonic2Distance;
int ultrasonic2Duration;

// definiere Werte
#define DRIVE_FORWARD 0
#define TURN_LEFT 1

int state = DRIVE_FORWARD;

void setup()
{
Serial.begin(9600); //Übertragungsgeschwindigkeit für die Kommunikation mit dem Computer
Robot.begin();
Robot.beginTFT();

** does this work?**
** // Ultraschallsensor Pin Konfiguration**
** pinMode (ultrasonic2TrigPin, OUTPUT);**
** pinMode (ultrasonic2EchoPin, INPUT);**
** debugOutput(); // Gibt die Werte des Ultraschalssensor's in der Konsole an**
** //Fahre vorwärts **
** Robot.motorsWrite(128, 128);**
delay(1000);
assuming this is how you stop it
Robot.motorsWrite(0,0); // ???
debugOutput(); // Gibt die Werte des Ultraschalssensor's in der Konsole an
while(1){}

}

void loop()
{

debugOutput(); // Gibt die Werte des Ultraschalssensor's in der Konsole an

if(millis() - timeLoopDelay >= loopPeriod)
{
readUltrasonicSensors(); //ließt und speichert die gemessene Distanz

stateMachine();

timeLoopDelay = millis();
}
}

void stateMachine()
{
if(state == DRIVE_FORWARD) // keine Hindernisse vor mir
{
if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // Wenn der Weg frei ist
{
//Fahre vorwärts
Robot.motorsWrite(128, 128);

}
else // Hindernis vor mir
{
state = TURN_LEFT;
}
}
else if(state == TURN_LEFT) // Hindernis im Weg, dreh nach links
{
unsigned long timeToTurnLeft = 1100; // Es braucht -+ 1.1 Sekunden um 90° zu drehen

unsigned long turnStartTime = millis(); //Speichert die Zeit als der Robot mit dem Drehen gestartet ist

while((millis()-turnStartTime < timeToTurnLeft)) // bleibt in der Schleife bis timeToTurnLeft (1.1 Sekunden) vorbei ist
{
// dreh nach links
Robot.turn(90);
}

state = DRIVE_FORWARD;
}
}

void readUltrasonicSensors()
{
// Ultraschalssensor 2

Robot.digitalWrite(ultrasonic2TrigPin, HIGH);
delayMicroseconds(10); // der TrigPin muss für mindestens 10 us an bleiben
Robot.digitalWrite(ultrasonic2TrigPin, LOW);

ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}

void debugOutput()
{
if((millis() - timeSerialDelay) > serialPeriod)
{
Robot.print("ultrasonic2Distance: ");
Robot.print(ultrasonic2Distance);
Robot.print("cm");
Robot.println();

timeSerialDelay = millis();
}
}

Why is this stupid "thing" keep replacing double question marks and double semicolons with idiotic smiling faces?

Of course it doesn't work, Vaclav; the robot doesn't understand emoticons

Vaclav:
Why is this stupid "thing" keep replacing double question marks and double semicolons with idiotic smiling faces?

because you didn't put your code inside [ code][ /code] tags. Please go back and edit your post.

arduinodlb:
because you didn't put your code inside [ code][ /code] tags. Please go back and edit your post.

We keep telling him, but it just doesn't seem to sink in.

Aerocell:
Hey there, I have a problem with my sketch:

the compiler say there are no errors but when I upload the sketch to the ArduinoRobot, he doesn't move, he just give the distance between him and an object on the tft screen out.

Did you read the part about the motors being disabled when the robot was connected to a USB cable? Unplug the USB cable, run off the batteries, and use the built-in display for output.

@PaulS I tried it with the delay(timeToTurnLeft) but it didn't work soon I will looking for the else if statement.

@UKHeliBob, I tried it with unsigned long but it didn't work.

@arduinodlb yeah that would be worth a try, I will try it

@Vaclav No it didn't work, when the sketch run, the tft display shows:
ultrasonic2Distance
0cm
ultrasonic2Distance
0cm
but there is no object in front of the Robot, there must be a distance of +- 30 cm

@Johnwasser what do you mean exactly ? (sry but I don't undestand English very well )

anyway thanks for everyone to help me

@Johnwasser what do you mean exactly ?

For safety the motors will not normally run when the robot is connected to a PC by the USB cable.

johnwasser:
Did you read the part about the motors being disabled when the robot was connected to a USB cable? Unplug the USB cable, run off the batteries, and use the built-in display for output.

Aerocell:
@Johnwasser what do you mean exactly ? (sry but I don't undestand English very well )cable, run off the batteries, and use the built-in display for output.

I mean:

The motors will not run if you have a USB cable connected. This is for the safety of the USB connectors. If the robot started to speed away it would jerk on the cable and possibly damage one or more of the USB connectors

UNPLUG the USB cable if you want to use the motors.

Use the built-in battery case for power.

Printing to Serial Monitor will not work because you have to unplug the USB cable. If you want to display text you should use the built-in display.

@Vaclav No it didn't work, when the sketch run, the tft display shows:
ultrasonic2Distance
0cm
ultrasonic2Distance
0cm
but there is no object in front of the Robot, there must be a distance of +- 30 cm

OK, but I was not trying to check the distance just using debugOutput as debug trace ( not a good choice) , I just wanted to know if the robie moved for 1 second and than stopped.

I was not clear in that, I am sorry.

BTW there should have been only one debugOutput - after the 1 second "run" and delay.

So if robie did not move it is time to check the hardware.

After that I would suggest to rebuild your hardware function(s) so they return a value.
It's a good practice to have a feedback from functions even it is is just "completed OK".

For example something like this:

boolean debugOutput()
{
if((millis() - timeSerialDelay) > serialPeriod)
{
Robot.print("ultrasonic2Distance: ");
Robot.print(ultrasonic2Distance);
Robot.print("cm");
Robot.println();

timeSerialDelay = millis();

retrun true; // distance measured OK
}
return false; // ? ? problem ?
}

Than you can check the function return
if( debugOutput())

// distance measured OK
Serial.print...

else

// failed to measure
Serial.print...

AWOL:
We keep telling him, but it just doesn't seem to sink in.

So it seems.

@arduinodlb, how do I print out the turn decisions, the sensor readings can I print out with the serial monitor right ?

Please read your PM

@arduinodlb, yeah I tried it but the serial monitor will not work, I can't see anything in it even with a diffrent Baut Rate, first I used 9600 and then 115200 but I can't get any informations on the serial monitor I think I need to change a value in the device manager but I'm not sure which one. Perhaps it is because I use Robot.println(); and not Serial.println(); ?

Aerocell:
@arduinodlb, yeah I tried it but the serial monitor will not work, I can't see anything in it even with a diffrent Baut Rate, first I used 9600 and then 115200 but I can't get any informations on the serial monitor I think I need to change a value in the device manager but I'm not sure which one. Perhaps it is because I use Robot.println(); and not Serial.println(); ?

Oh yeah. Sorry. I forgot you had to disconnect the USB to have the robot move.

Sorry, on re-reading the thread I realise that basically, you are not getting correct values into your sensor.

So, you can plug in the USB cable again, and debug the sensors until they are return correct distances to objects. The motor movement code will not work correctly until you are reading the sensors correctly.

Can you please explain what happens when you run the latest version of your program and what should happen ?