stray, declaration and syntax errors...

Hi everyone,

I just bought my first Arduino UNO card Ultrasonic Smart Tracking Motor Robot Car Kit for Arduino

The kit came without any notice or a single line of code, I found it on a PDF in another website…

To avoid any hidden character, I have copied/paste the code from the PDF in a single TextEdit app.

Here’s the code :

/*
  Firmware for the "2WD Ultrasonic Motor Robot Car Kit"

  Stephen A. Edwards

  Hardware configuration :
  A pair of DC motors driven by an L298N H bridge motor driver
  An HC−SR04 ultrasonic range sensor mounted atop a small hobby servo */
#include <Servo.h>
Servo servo;

// Ultrasonic Module pins
const int trigPin = 13; // 10 microsecond high pulse causes chirp , wait 50 us
const int echoPin = 12; // Width of high pulse indicates distance

// Servo motor that aims ultrasonic sensor .
const int servoPin = 11; // PWM output for hobby servo

// Motor control pins : L298N H bridge
const int enAPin = 6; // Left motor PWM speed control
const int in1Pin = 7; // Left motor Direction 1
const int in2Pin = 5; // Left motor Direction 2
const int in3Pin = 4; // Right motor Direction 1
const int in4Pin = 2; // Right motor Direction 2
const int enBPin = 3; // Right motor PWM speed control

// Set motor speed: 255 full ahead, −255 full reverse , 0 stop void go( enum Motor m, int speed)
enum Motor { LEFT, RIGHT };
{
  digitalWrite (m == LEFT ? in1Pin : in3Pin , speed > 0 ? HIGH : LOW );
  digitalWrite (m == LEFT ? in2Pin : in4Pin , speed <= 0 ? HIGH : LOW );
  analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? −speed : speed );
}

// Initial motor test :
// left motor forward then back // right motor forward then back
void testMotors()
{
  static int speed[8] = {128, 255, 128, 0, −128, −255, −128, 0};
  go(RIGHT, 0);
  for (unsigned char i = 0 ; i < 8 ; i++)
    go(LEFT, speed[i ]), delay (200);
  for (unsigned char i = 0 ; i < 8 ; i++)
    go(RIGHT, speed[i ]), delay (200);
}

// Read distance from the ultrasonic sensor , return distance in mm //
// Speed of sound in dry air , 20C is 343 m/s
// pulseIn returns time in microseconds (10ˆ−6)
// 2d=p*10ˆ−6s*343m/s=p*0.00343m=p*0.343mm/us
unsignedint readDistance()
{
  digitalWrite ( trigPin , HIGH );
  delayMicroseconds (10);
  digitalWrite ( trigPin , LOW );
  unsigned long period = pulseIn ( echoPin, HIGH );
  return period * 343 / 2000;
}

#define NUM ANGLES 7
unsigned char sensorAngle[NUM ANGLES] = { 60, 70, 80, 90, 100, 110, 120 };
unsigned int distance [NUM ANGLES];
// Scan the area ahead by sweeping the ultrasonic sensor left and right
// and recording the distance observed . This takes a reading , then
// sends the servo to the next angle . Call repeatedly once every 50 ms or so .
void readNextDistance()
{
  static unsigned char angleIndex = 0;
  static signed char step = 1;
  distance [angleIndex] = readDistance ();
  angleIndex += step ;
  if (angleIndex == NUM ANGLES − 1) step = −1;
  else if (angleIndex == 0) step = 1;
  servo . write ( sensorAngle[angleIndex] );
}

// Initial configuration
//
// Configure the input and output pins
// Center the servo
// Turn off the motors
// Test the motors
// Scan the surroundings once
//
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  digitalWrite ( trigPin , LOW);
  pinMode(enAPin, OUTPUT);
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  pinMode(enBPin, OUTPUT);
  servo . attach ( servoPin );
  servo . write (90);
  go(LEFT, 0);
  go(RIGHT, 0);
  testMotors ();
  // Scan the surroundings before starting
  servo . write ( sensorAngle[0] );
  delay (200);
  for (unsignedchar i = 0 ; i < NUM ANGLES; i++)
    readNextDistance (), delay (200);
}

// Mainloop:
//
// Get the next sensor reading
// If anything appears to be too close , back up
// Otherwise, go forward
//
void loop() {
  readNextDistance ();
  // See if something is too close at any angle
  unsigned char tooClose = 0;
  for (unsigned char i = 0 ; i < NUM ANGLES ; i++)
    if ( distance [i] < 300)
      tooClose = 1;

  if ( tooClose ) {
    // Something's nearby: back up left
    go(LEFT, −180);
    go(RIGHT, −80);
  }
  else {
    // Nothing in our way: go forward
    go(LEFT, 255);
    go(RIGHT, 255);
  }
  // Check the next direction in 50 ms
  delay (50);
}

and here’s (part of) the errors log :

Arduino : 1.8.8 (Mac OS X), Carte : "Arduino/Genuino Uno"

robot-car_ENTIER:32:3: error: stray '\342' in program
   analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? −speed : speed );
   ^
robot-car_ENTIER:32:3: error: stray '\210' in program
robot-car_ENTIER:32:3: error: stray '\222' in program
robot-car_ENTIER:39:3: error: stray '\342' in program
   static int speed[8] = {128, 255, 128, 0, −128, −255, −128, 0};
   ^
robot-car_ENTIER:51:1: error: 'unsignedint' does not name a type
 unsignedint readDistance()
 ^
robot-car_ENTIER/robot-car_ENTIER.ino: In function 'void testMotors()':
robot-car_ENTIER:40:14: error: 'go' was not declared in this scope
   go(RIGHT, 0);
              ^
robot-car_ENTIER/robot-car_ENTIER.ino: At global scope:
robot-car_ENTIER:51:1: error: 'unsignedint' does not name a type
 unsignedint readDistance()
 ^
exit status 1
stray '\342' in program

Can anyone give me some clues to fix one of these ?

Thanks in advance :slight_smile:

// Set motor speed: 255 full ahead, −255 full reverse , 0 stop void go( enum Motor m, int speed)
enum Motor { LEFT, RIGHT };
{

Massive oops right there.
Look at the comment

unsignedint sp. “unsigned int”

I ran through your code and found the original PDF you mention:

This compiles. Whether or not it works is up to you to decide:

/*
  Firmware for the "2WD Ultrasonic Motor Robot Car Kit"

  Stephen A. Edwards

  Hardware configuration :
  A pair of DC motors driven by an L298N H bridge motor driver
  An HC−SR04 ultrasonic range sensor mounted atop a small hobby servo */
#include <Servo.h>
Servo servo;

// Ultrasonic Module pins
const int trigPin = 13; // 10 microsecond high pulse causes chirp , wait 50 us
const int echoPin = 12; // Width of high pulse indicates distance

// Servo motor that aims ultrasonic sensor .
const int servoPin = 11; // PWM output for hobby servo

// Motor control pins : L298N H bridge
const int enAPin = 6; // Left motor PWM speed control
const int in1Pin = 7; // Left motor Direction 1
const int in2Pin = 5; // Left motor Direction 2
const int in3Pin = 4; // Right motor Direction 1
const int in4Pin = 2; // Right motor Direction 2
const int enBPin = 3; // Right motor PWM speed control

#define NUM_ANGLES 7
unsigned char sensorAngle[NUM_ANGLES] = { 60, 70, 80, 90, 100, 110, 120 };
unsigned int distance [NUM_ANGLES];

// Set motor speed: 255 full ahead, −255 full reverse , 0 stop void go( enum Motor m, int speed)
enum Motor { LEFT, RIGHT };

void go( enum Motor m, int speed )
{
    digitalWrite(m == LEFT ? in1Pin : in3Pin , speed > 0 ? HIGH : LOW );
    digitalWrite(m == LEFT ? in2Pin : in4Pin , speed <= 0 ? HIGH : LOW );
    analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? -speed : speed );
};

// Initial motor test :
// left motor forward then back // right motor forward then back
void testMotors()
{
    static int 
        speed[8] = {128, 255, 128, 0, 128, 255, 128, 0};
    go(RIGHT, 0);
    
    for (unsigned char i = 0 ; i < 8 ; i++)
        go(LEFT, speed[i ]), delay (200);
    for (unsigned char i = 0 ; i < 8 ; i++)
        go(RIGHT, speed[i ]), delay (200);
}

// Read distance from the ultrasonic sensor , return distance in mm //
// Speed of sound in dry air , 20C is 343 m/s
// pulseIn returns time in microseconds (10ˆ−6)
// 2d=p*10ˆ−6s*343m/s=p*0.00343m=p*0.343mm/us
unsigned int readDistance()
{
    digitalWrite ( trigPin , HIGH );
    delayMicroseconds (10);
    digitalWrite ( trigPin , LOW );
    unsigned long period = pulseIn ( echoPin, HIGH );
    return period * 343 / 2000;
}

// Scan the area ahead by sweeping the ultrasonic sensor left and right
// and recording the distance observed . This takes a reading , then
// sends the servo to the next angle . Call repeatedly once every 50 ms or so .
void readNextDistance()
{
    static unsigned char angleIndex = 0;
    static signed char step = 1;
    distance [angleIndex] = readDistance ();
    angleIndex += step ;
    if (angleIndex == NUM_ANGLES - 1) 
        step = -1;
    else if (angleIndex == 0) 
        step = 1;
  
    servo.write( sensorAngle[angleIndex] );
}

// Initial configuration
//
// Configure the input and output pins
// Center the servo
// Turn off the motors
// Test the motors
// Scan the surroundings once
//
void setup() 
{
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    digitalWrite ( trigPin , LOW);
    pinMode(enAPin, OUTPUT);
    pinMode(in1Pin, OUTPUT);
    pinMode(in2Pin, OUTPUT);
    pinMode(in3Pin, OUTPUT);
    pinMode(in4Pin, OUTPUT);
    pinMode(enBPin, OUTPUT);
    servo.attach( servoPin );
    servo.write(90);
    go(LEFT, 0);
    go(RIGHT, 0);
    testMotors ();
  
    // Scan the surroundings before starting
    servo.write( sensorAngle[0] );
    delay (200);
    for (unsigned char i = 0 ; i < NUM_ANGLES; i++)
        readNextDistance (), delay (200);
}

// Mainloop:
//
// Get the next sensor reading
// If anything appears to be too close , back up
// Otherwise, go forward
//
void loop() 
{
    readNextDistance ();
    // See if something is too close at any angle
    unsigned char tooClose = 0;
    for(unsigned char i = 0 ; i < NUM_ANGLES ; i++)
        if ( distance [i] < 300)
            tooClose = 1;

    if ( tooClose ) 
    {
        // Something's nearby: back up left
        go(LEFT, -180);
        go(RIGHT, -80);
    }
    else 
    {
        // Nothing in our way: go forward
        go(LEFT, 255);
        go(RIGHT, 255);
    }
    // Check the next direction in 50 ms
    delay (50);
}

Many thanks guys or thanks a lot, as you prefer !

So unsigned char instead of unsignedchar

The minus sign wasn't good

void go( enum Motor m, int speed ) was part of a comment but not in the code

and so on...

One more time, thanks a lot : you saved my time to help me to properly dicepher and read this kind of code ! Next time, I'll try to solve it by myself :)

Hello, you helped with my first Arduino. I got a lot of stray-messages when I copied it from the web site! Thank you!

The sketch of Blackfin works perfect for me.

The servo turns app. 45 - 60 degrees. Where in the sketch can change it to 90 - 120 degrees?

Thanks in advance Mario