DFRobotShop Rover Programming

Hehe. Welcome to coding, where even the smallest syntax mistake can render hundreds of lines of code useless. It will likely be frustrating at first, but then you get an idea of what may be wrong based on the error messages.

Best of success,

Right now your code says do something until a specific value of 230 is reached - unfortuately its incredibly difficult to reach an exact value, so we suggest almost always using greater or less than ‘>’ ‘<’ signs. Also, we suggest coming to a full stop and waiting for a second before changing course. Most of your errors come because you have an extra ‘}’ bracket.

Likely pin 0 did not work because the jumper connecting it to the onboard sensor is still in place. Note that the board includes both a temperature and a light sensor connected to pins 0 and 1. Removing the jumper should solve the issue. Happy to see you have it working.

Sincerely,

The 2x3 pins are for in-system programming of the ATMega328 chip. Almost all microcontrollers have these pins, though it is incredibly rare that someone would need to use them (it changes the “bootup” code on the chip - much like changing the operating system on your computer). We suggest not playing with them or thinking about them. All GND pins are common.

Hi UncleMike,

For Arduino specific code issues (or platform specific code in general) we suggest posting on the manufacturer’s site. In the case of the DFRobotShop Rover, it would be Arduino. By posting on that forum, it ensures the feedback is done by people who use that specific language (Arduino in this case). You can also link to that post from here so you get the benefit of both communities.

A few ideas are as follows:

  • declare your integers outside of the loop
  • note that the two conditions of your if statements can occur simultaneously
  • try using cases instead of if statements and printing the results in the serial display window before using the motors.

Sincerely,

1/2 the battle is understanding the error msgs.
UncleMike

Help!
I got my IR sensor (Sharp GP2D120) and tested it with a very simple test program, that worked. But when I try to write a simple “go forward until a value is reached then turn right”

[code]int E1 = 6;
int E2 = 5;
int M1 = 8;
int M2 = 7;

void setup()
{
int leftspeed = 255;
int rightspeed = 255;

void loop()

{

int distance = analogRead(0);
if ( distance == 230 )
{
analogWrite (E1,leftspeed); // right
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,LOW);
}
else
{
analogWrite (E1,leftspeed); // forward
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,HIGH);
}
}[/code]But I can’t make heads or tails of the syntax error codes. I can’t find any real help from all the tutorials, what is wrong?

ir_test.cpp: In function ‘void setup()’:
ir_test:13: error: a function-definition is not allowed here before ‘{’ token
ir_test:32: error: expected `}’ at end of input

UncleMike

I made the changes you recomended, but can not figure which bracket is redundent, with every mod I get a compleatly different set of error msgs.

[code]int E1 = 6;
int E2 = 5;
int M1 = 8;
int M2 = 7;
int leftspeed = 255;
int rightspeed = 255;

void setup()

}

void loop()

{
int distance = analogRead(0);

if ( distance > 230 )
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);

delay(1000); // A small pause to allow the motors to stop

analogWrite (E1,leftspeed); // right
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,LOW);
}
else
{
analogWrite (E1,leftspeed); // forward
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,HIGH);
}[/code]with the following errors

sketch_may12b:9: error: expected initializer before ‘}’ token
sketch_may12b:9: error: expected declaration before ‘}’ token

can some one help with the syntex. I have balanced the Brackets but still seem to have things out of order
UncleMike

Here it is, took a while but I got it to run. For all you wondering how to intergrate the Sharp IR into the basic (most Basic) code;

[code]int E1 = 6;
int E2 = 5;
int M1 = 8;
int M2 = 7;
int leftspeed = 255;
int rightspeed = 255;
void setup()
{
}
void loop()
{
int distance = analogRead(1); // had to use analog pin 1, for some reason pin 0 did not work
if ( distance < 230 ) // takes some experimenting to find the right value, yours may differ.
{
analogWrite (E1,leftspeed); // right
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,LOW);
}
else
{
analogWrite (E1,leftspeed); // forward
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,HIGH);
}
}[/code]now begins the fun of tweeking it.
PS took many attemps of moving things around untill the syntax errors went away.
good luck
UncleMike

I have found the jumpers for Light Sensor (LS(A0)) and the Temp sensor (LM35(A1)), but there are 3 sets of jump-able contacts (1CSP) just ahead of the reset sw. What are they for. Are “analog ground” and “Digital ground” common?

UncleMike

OK OK I get the picture I wont touch or even think about the pins…lol
UncleMike

OK now I have added 2 more sensors and thought this code would work…but it now defaults to turn left with no input, right with input on left sensor, run right with input on the right sensor, and run right with input to forward sensor. it never falls through to run forward. even with all sensors unplugged it still runs left

[code]int E1 = 6;
int E2 = 5;
int M1 = 8;
int M2 = 7;
int leftspeed = 255;
int rightspeed = 255;
int ledGreen =13;
int ledRed = 12;
void setup()
{
}
void loop()
{
int distanceLeft = analogRead(2);
int distanceCenter = analogRead(3);
int distanceRight = analogRead(4);
if ( distanceLeft < 230 && distanceCenter < 230) // values to be gotten through trial
{
digitalWrite(ledGreen,LOW);
analogWrite (E1,leftspeed); // right
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,LOW);
}
else
{
digitalWrite(ledGreen,HIGH);
digitalWrite(ledRed,HIGH);
analogWrite (E1,leftspeed); // forward
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,HIGH);
}
if ( distanceRight < 230 && distanceCenter < 230) // values to be gotten through trial
{
digitalWrite(ledRed,LOW);
analogWrite (E1,leftspeed); // left
digitalWrite(M1,LOW);

analogWrite (E2,rightspeed);
digitalWrite(M2,LOW);
}
else
{
digitalWrite(ledGreen,HIGH);
digitalWrite(ledRed,HIGH);
analogWrite (E1,leftspeed); // forward
digitalWrite(M1,HIGH);

analogWrite (E2,rightspeed);
digitalWrite(M2,HIGH);
}
}[/code]what is going wrong, been over the code so many times… I need a new set of eyes to check it out.
UncleMike