Go Down

Topic: New to C/C++ get errors and need help to find the problem (Read 509 times) previous topic - next topic

Hi!

Just started on my new arduino project.
I need help to figure out what the problem with my code is
The project consists of two files "MSTC_0_1_ino.ino" and "types.h"

"MSTC_0_1_ino.ino"
Code: [Select]
//Includes
#include <AFMotor.h>
#include "types.h"

//Objects
AF_DCMotor MotorLeft(4, MOTOR12_64KHZ);
AF_DCMotor MotorRight(3, MOTOR12_64KHZ);

//Constant values
#define MinRange 8;
#define MaxRange 200;
#define MinSpeed 0;
#define MaxSpeed 255;
#define PointTurn true;
#define DistanceClose 20;

//Debugging function
#define DEBUG true;
#ifdef DEBUG
  #define DEBUG_PRINT(x) Serial.println(x);
#else
  #define DEBUG_PRINT(x);
#endif

//Pin declaration
#define IRRangePin 0;

//Global values
Distances Distance = Unknown;
Actions Action = Stop;
int Speed;

void setup(){
  Serial.begin(9600);
  DEBUG_PRINT("Motor test!");
  #ifdef DEBUG
//    MotorTest();
  #endif
}

void loop(){
  //kontrollera avstånd
  MapRangeToDistance(ReadRange());
  DEBUG_PRINT(ReadRange());
  //vänta på kommando
  GetAction();
  //kontrollera värden
  //mappa om avståndet till hastighet
  //agera
  //Rapportera värden
}

void GetAction(){
  boolean read = false;
  while(!read){
    #ifdef DEBUG
      if(Serial.available() > 0){
        switch(Serial.read()){
          case 'f':
            Action = Forward;
            read = true;
            break;
          case 'r':
            Action = Right;
            read = true;
            break;
          case 'l':
            Action = Left;
            read = true;
            break;
          case 'b':
            Action = Backward;
            read = true;
            break;
          case 's':
            Action = Stop;
            read = true;
            break;
          default:
            DEBUG_PRINT("Not a valid command!");
            break;
        }
      }
    #else
      //TODO IRReciverCode
      Action = Stop;
      read = true;
    #endif
  }
}

//Make the motors beep to check that they are connected.
void MotorTest(){
  Speed = 50;
  SetSpeed();
  Action = Forward;
  Act();
  delay(200);
  Action = Stop;
  Act();
  delay(200);
}

//Set Distance acording to Range
void MapRangeToDistance(int Range){
  if(Range > DistanceClose) Distance = Far;
  else if((Range <= MaxRange) && (Range > DistanceClose)) Distance = InRange;
  else if(Range <= DistanceClose) Distance = Close;
  else Distance = Unknown;
}

//Make shure that range is in a valid range then map it from that range to the speed range
void MapRangeToSpeed(int Range){
  //result = a > b ? x : y;
  if (Range < MinRange) Range = MinRange;
  else if (Range > MaxRange) Range = MaxRange;
  Speed = map(Range, MinRange, MaxRange, MinSpeed, MaxSpeed);
}

//Update motor speed
void SetSpeed(){
  MotorLeft.setSpeed(Speed);
  MotorRight.setSpeed(Speed);
}

//Returns the distance read from the ir range finder
int ReadRange(){
  float volts = analogRead(IRRangePin)*0.0048828125;   // value from sensor * (5/1024)
  return int(65*pow(volts, -1.10));                 // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
}

//Act upon distance read and desired command.
void Act(){
// if(((Distance == Close) || (Distance == Unknown)) && (Action == Forward)) Action = Stop;
  switch(Action)
  {
    case Forward:
      MotorLeft.run(FORWARD);
      MotorRight.run(FORWARD);
      break;
    case Right:
      MotorLeft.run(FORWARD);
      if (PointTurn) MotorRight.run(BACKWARD);
      break;
    case Left:
      MotorRight.run(FORWARD);
      if (PointTurn) MotorLeft.run(BACKWARD);
      break;
    case Stop:
      MotorLeft.run(RELEASE);
      MotorRight.run(RELEASE);
      break;
  }
}


"types.h"
Code: [Select]
enum Distances {
  Unknown,
  Far,
  InRange,
  Close
};
enum Actions{
  Forward,
  Right,
  Left,
  Backward,
  Stop
};


error codes
Quote

MSTC_0_1_ino.cpp: In function 'void MapRangeToDistance(int)':
MSTC_0_1_ino:105: error: expected `)' before ';' token
MSTC_0_1_ino:105: error: expected primary-expression before ')' token
MSTC_0_1_ino:105: error: expected `;' before ')' token
MSTC_0_1_ino:106: error: 'else' without a previous 'if'
MSTC_0_1_ino:106: error: expected `)' before ';' token
MSTC_0_1_ino:106: error: expected `)' before ';' token
MSTC_0_1_ino:106: error: expected primary-expression before ')' token
MSTC_0_1_ino:106: error: expected `;' before ')' token
MSTC_0_1_ino:106: error: expected primary-expression before ')' token
MSTC_0_1_ino:106: error: expected `;' before ')' token
MSTC_0_1_ino:107: error: 'else' without a previous 'if'
MSTC_0_1_ino:107: error: expected `)' before ';' token
MSTC_0_1_ino:107: error: expected primary-expression before ')' token
MSTC_0_1_ino:107: error: expected `;' before ')' token
MSTC_0_1_ino:108: error: 'else' without a previous 'if'
MSTC_0_1_ino.cpp: In function 'void MapRangeToSpeed(int)':
MSTC_0_1_ino:114: error: expected `)' before ';' token
MSTC_0_1_ino:114: error: expected primary-expression before ')' token
MSTC_0_1_ino:114: error: expected `;' before ')' token
MSTC_0_1_ino:115: error: 'else' without a previous 'if'
MSTC_0_1_ino:115: error: expected `)' before ';' token
MSTC_0_1_ino:115: error: expected primary-expression before ')' token
MSTC_0_1_ino:115: error: expected `;' before ')' token
MSTC_0_1_ino:116: error: expected `)' before ';' token
MSTC_0_1_ino:116: error: expected primary-expression before ',' token
MSTC_0_1_ino:116: error: expected primary-expression before ',' token
MSTC_0_1_ino:116: error: expected primary-expression before ',' token
MSTC_0_1_ino:116: error: expected primary-expression before ')' token
MSTC_0_1_ino:116: error: expected `;' before ')' token
MSTC_0_1_ino.cpp: In function 'int ReadRange()':
MSTC_0_1_ino:127: error: expected `)' before ';' token
MSTC_0_1_ino:127: error: expected primary-expression before ')' token
MSTC_0_1_ino:127: error: expected `;' before ')' token
MSTC_0_1_ino.cpp: In function 'void Act()':
MSTC_0_1_ino:142: error: expected `)' before ';' token
MSTC_0_1_ino:142: error: expected primary-expression before ')' token
MSTC_0_1_ino:142: error: expected `;' before ')' token
MSTC_0_1_ino:146: error: expected `)' before ';' token
MSTC_0_1_ino:146: error: expected primary-expression before ')' token
MSTC_0_1_ino:146: error: expected `;' before ')' token


I've doubblechecked the syntax without finding any obvious errors.
Since I've been at it for 6 hours now I decided to ask for help.

Added the project for download.

Thanks in advance //Henke

WizenedEE

#defines shouldn't have semicolons.
In fact, I'd suggest dumping them completely. Just replace "#define" with "const int" and you'll get the same stuff, just with better style (fewer weird bugs with off error messages). Your macros are probably fine though (the DEBUG_PRINT)

MarkT

Either use
Code: [Select]

#define MinRange 8

or
Code: [Select]

const int MinRange = 8 ;


#define uses pure textual substitution so you were putting semicolons in lots of if conditions.
[ I won't respond to messages, use the forum please ]


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy