Jump to content


Photo

ArduinoRobot

Arduino SketchProblem

  • Please log in to reply
7 replies to this topic

#1 Arduinorobot

Arduinorobot

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 28 February 2015 - 04:10 PM

Hey I have a problem with my sketch I used your sketch for the original ArduinoRobot (http://arduino.cc/en/Main/Robot), because my own sketch had too many delay(); and your sketch was perfectly written, but for me it doesn't work. Do you know maybe where the errors are because the compiler says the sketch has no errors.

Here is the sketch its almost the same as yours I just used another pins and librarie's:

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

const int serialPeriod = 500;  //only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;  // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = TKD3;
const int ultrasonic2EchoPin = TKD4;

int ultrasonic2Distance;
int ultrasonic2Duration;

// define the states
#define DRIVE_FORWARD    0
#define TURN_LEFT        1

int state = DRIVE_FORWARD;  // 0 = drive forward (DEFAULT), 1 = turn left


void setup() 
{
     Serial.begin(9600);  
     Robot.begin();
     Robot.beginTFT();

     // ultrasonic sensor pin configurations
     pinMode (ultrasonic2TrigPin, OUTPUT);
     pinMode (ultrasonic2EchoPin, INPUT);
}

void loop() 
{

     debugOutput(); // prints debugging messages to the serial console

     if(millis() - timeLoopDelay >= loopPeriod)
     {
          readUltrasonicSensors(); // read and store the mesured distance
          
          stateMachine();
          
          timeLoopDelay = millis();
     }
}

void stateMachine()
{
     if(state == DRIVE_FORWARD) // no obstacles detected
     {
          if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // if there is nothing in front of us (note: ultrasonicDistance will be negative for some ultrasonics if there's nothing in range)
          {
               //Fahre vorwärts            
               Robot.motorsWrite(128, 128);
              
          }
          else  // there is an obstacle in front of us
          {
               state = TURN_LEFT;
          }
     }
     else if(state == TURN_LEFT) // obstacle detected -- turn left
     {
          unsigned long timeToTurnLeft = 1100; // it takes around 1.1 seconds to turn 90 °
          
          unsigned long turnStartTime = millis();  // save the time we started turning
          
          while((millis()-turnStartTime < timeToTurnLeft)) // stay in this loop until timeToTurnLeft (1.1 seconds) has elapsed
        {
               // turn left
              Robot.turn(90);
              delay(1100);
        }
     
          state = DRIVE_FORWARD;
     }
}

void readUltrasonicSensors()
{
     // Ultraschalssensor 2
     
     Robot.digitalWrite(ultrasonic2TrigPin, HIGH);
     delayMicroseconds(10);                 // must keep the trig pin high for at least 10us
     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();
    }
}

  • 0

#2 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 01 March 2015 - 10:25 PM

You said there weren't any compiler errors. What happens after you download the sketch to your robot? Do you see any output in the serial monitor?


  • 0

#3 Arduinorobot

Arduinorobot

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 04 March 2015 - 02:24 PM

Ok it's done. I just needed to return the Arduino motorsboard to it's default state. Now it works fine :)


  • 0

#4 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 04 March 2015 - 02:36 PM

Great, glad to hear it's working!


  • 0

#5 Arduinorobot

Arduinorobot

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 09 March 2015 - 08:48 PM

But I have another question, what does timeSerialdelay and timeLoopdelay. Because they have no value, just 0 but why 0.

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

and this part here:

void loop()
{
    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay >= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distances
        
        timeLoopDelay = millis();

the part with if(millis() - timeLoopdelay  >= loopPeriod) what does it mean ? And why the timeLoopDelay = millis();  Sorry for so many questions but I need it for school and I will try to understand :D


  • 0

#6 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 10 March 2015 - 02:00 PM

No problem, glad you are trying to understand it!

 

timeSerialdelay and timeLoopdelay have an initial value of zero, but then their values are changing later in the program.

 

Inside of the IF statement in loop(), you can see that we set timeLoopDelay = millis(), where millis() is the number of milliseconds the Arduino has been powered on for. Initially, timeLoopDelay is zero, as you noticed. In the conditional statement (IF statement) we check that millis() - timeLoopDelay >= loopPeriod. In other words, we check to see if looPeriod milliseconds has elapsed since the last time the code in the loop was executed. We do this to make the code inside the loop only run every loopPeriod milliseconds, instead of running much more frequently.

 

The timeSerialDelay variable is used similarly, to only print information out to the serial terminal every quarter second instead of constantly printing it out (which makes it impossible to read).


  • 1

#7 Arduinorobot

Arduinorobot

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 10 March 2015 - 04:48 PM

Ok thank's but which values get they during the loop ?

And in this part here: we check to see if looPeriod milliseconds has elapsed since the last time the code in the loop was executed. do you mean, if loop period has elapsed it starts again by void loop:

void loop()
{
    debugOutput(); // prints debugging messages to the serial console
 ...

  • 0

#8 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 10 March 2015 - 09:37 PM

The two variables get assigned the output from millis() since we have these lines:

timeLoopDelay = millis();

and

timeSerialDelay = millis();

millis() returns the number of milliseconds since the microcontroller has been powered on.

 

We update the values inside timeLoopDelay and timeSerialDelay so that we can check (using an IF statement) if the amount of time in the serialPeriod or loopPeriod has passed. If that amount of time has passed, then we want the program to branch inside the IF statement with that conditional statement.

 

The purpose is to control how often the code inside loop() is executed and how often the stuff inside debugOutput() is being executed.


  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users