Code Errors I don't understand

Hi, I’m Marcbotics, Sorry I’m new at this My project consists of controlling two dc motors simultaneously with a single thumb-stick. So far, I got both motors to spin forward when thumb-stick is pushed forward and both motors to spin backward when thumb-stick is pulled back. I’m using a Parallax thumb-stick, Arduino Uno R3, and a Dual H Bridge Driver. The issue I’m having is I tried modifying the code for Left and Right functionality. I want it to be so when I push the Thumb-Stick left the left motor spins backwards while the right motor spins forwards. I don’t understand what these errors mean, see below. I did not get any of these errors before modifying the code for Left and Right functions. Thank you for you time. Arduino: 1.6.6 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Marcbotics\Documents\Arduino\thumbstick_speed_control\thumbstick_speed_control.ino: In function 'void loop()': thumbstick_speed_control:50: error: 'MotorForward' was not declared in this scope

MotorForward(motorSpeed);

^ thumbstick_speed_control:55: error: 'MotorBackward' was not declared in this scope

MotorBackward(motorSpeed);

^ thumbstick_speed_control:69: error: 'MotorLeft' was not declared in this scope

MotorLeft(motorSpeed);

^ thumbstick_speed_control:74: error: 'MotorRight' was not declared in this scope

MotorRight(motorSpeed);

^ thumbstick_speed_control:79: error: a function-definition is not allowed here before '{' token {

^ thumbstick_speed_control:90: error: a function-definition is not allowed here before '{' token

{

^ thumbstick_speed_control:101: error: expected initializer before 'digitalWrite'

digitalWrite(motorDirPinA1, LOW);

^ thumbstick_speed_control:103: error: 'Spd' was not declared in this scope

analogWrite(motorSpeedPin1, Spd);

^ thumbstick_speed_control:111: error: expected initializer before 'digitalWrite'

digitalWrite(motorDirPinA1, HIGH);

^ thumbstick_speed_control:113: error: 'Spd' was not declared in this scope

analogWrite (motorSpeedPin1, Spd);

^ exit status 1 'MotorForward' was not declared in this scope

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

You need to post the code that caused the errors if you really want help with it.

It looks like you probably messed up your braces, { and }. Do you know what those do and why they go where they go? If you fix those it might fix the whole thing.

Thanks Delta G I will try that. I did some copy and pasting within the code. first I need to save baseline copy of the original code. and then save a copy of the code with errors. i will get back to you Delta G.

Delta G should i post the code here or as an attachment?

If you post it here it will be readable by all. If you make it an attachment then people on phones and mobile devices can't read it so you limit the number of people that can help.

here you go Delta G See attached

thumbstick_speed_controlRev2.ino (3.15 KB)

Delta_G: If you make it an attachment then people on phones and mobile devices can't read it so you limit the number of people that can help.

Unfortunately I fall into this category tonight. So I can't see it if it is attached.

The {'s which open MotorLeft and MotorRight are a line too high, should be below the void... lines.

Then, there's a spurious } above the line void MotorStop....

Hiya Marc, If you read the thread called "How to use this forum - please read." how to best post pictures, code and all that stuff is discussed.

thanks Chris I'll remember that next time

Manor_Royal i reviewed your comment and the code over and over again for life of me i cannot see what you're seeing

OPs code

//Set pin numbers:
const byte joyStickPinA = A1;
const byte joyStickPinB = A2;
const byte motorSpeedPin1 = 5;
const byte motorDirPinA1 = 4;
const byte motorDirPinB1 = 6;

const byte motorSpeedPin2 = 9;
const byte motorDirPinA2 = 10;
const byte motorDirPinB2 = 11;
 
//variables
//Joystick input variables
int joyValue = 0;
int joyValueMax = 1023;
int joyValueMin = 0;
int joyValueMid = 512;
int joyValueMidUpper = joyValueMid + 20;
int joyValueMidLower = joyValueMid - 20;
 
//DC motor variables
byte motorSpeed = 0;
byte motorSpeedMax = 255;
byte motorSpeedMin = 70; //set to smallest value that make motor move (default 0)
                         // DC motor that I use start to move at 90 pwm value
 
void setup()
{
    pinMode(joyStickPinA, INPUT);
    pinMode(joyStickPinB, INPUT);
    
    pinMode(motorSpeedPin1, OUTPUT);
    pinMode(motorDirPinA1, OUTPUT);
    pinMode (motorDirPinB1, OUTPUT);

    pinMode (motorSpeedPin2, OUTPUT);
    pinMode (motorDirPinA2, OUTPUT);
    pinMode (motorDirPinB2, OUTPUT);
}
 
void loop()
{
    joyValue = analogRead(joyStickPinA);
   
    if(joyValue > joyValueMidUpper) //Forward

    {
        motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
        MotorForward(motorSpeed);
    }
    else if(joyValue < joyValueMidLower) //Backward
    {
        motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
        MotorBackward(motorSpeed);
    }
    //joyValue Between joyValueMidLower - joyValueMidUpper.
    //Need some range here, because joystick sometime not in  exact center.
    else
    {
       MotorStop();
    }
   
    joyValue = analogRead(joyStickPinB);
   
    if(joyValue > joyValueMidUpper) //Right
    {
        motorSpeed = map(joyValue, joyValueMidUpper, joyValueMax, motorSpeedMin, motorSpeedMax);
        MotorLeft(motorSpeed);
    }
    else if(joyValue < joyValueMidLower) //Left
    {
        motorSpeed = map(joyValue, joyValueMidLower, joyValueMin, motorSpeedMin, motorSpeedMax);
        MotorRight(motorSpeed);
   }
 
}
void MotorForward( byte Spd)
{
    digitalWrite(motorDirPinA1, HIGH);
    digitalWrite(motorDirPinB1, LOW);
    analogWrite(motorSpeedPin1, Spd);

    digitalWrite (motorDirPinA2, HIGH);
    digitalWrite (motorDirPinB2, LOW);
    analogWrite (motorSpeedPin2, Spd);
}
 
void MotorBackward( byte Spd)
{
    digitalWrite(motorDirPinA1, LOW);
    digitalWrite(motorDirPinB1, HIGH);
    analogWrite(motorSpeedPin1, Spd);

    digitalWrite(motorDirPinA2, LOW);
    digitalWrite(motorDirPinB2, HIGH);
    analogWrite(motorSpeedPin2, Spd);
}
 {
void MotorLeft( byte Spd)
    digitalWrite(motorDirPinA1, LOW);
    digitalWrite(motorDirPinB1, HIGH);
    analogWrite(motorSpeedPin1, Spd);

    digitalWrite(motorDirPinA2, HIGH);
    digitalWrite(motorDirPinA2, LOW);
    analogWrite(motorSpeedPin2, Spd);
 }
 {
  void MotorRight (byte Spd)
  digitalWrite(motorDirPinA1, HIGH);
  digitalWrite(motorDirPinB1, LOW);
  analogWrite (motorSpeedPin1, Spd);

  digitalWrite(motorDirPinA2, LOW);
  digitalWrite(motorDirPinB2, HIGH);
  analogWrite (motorSpeedPin2, Spd);
 }
    
 }
void MotorStop()
{
    analogWrite(motorSpeedPin1, 0);
    analogWrite(motorSpeedPin2, 0);  
}

Take a look at your code, where you write the MotorBackward, MotorLeft, MotorRight’, and MotorStop functions. The braces are all in the wrong places. Put some blank lines between defintions, and use the Tools > Auto Format command in the IDE. You will see the problem right away. The thread I linked mentions all this and more.

Here’s the answer:

The first brace should come after the type (void), the function name (MotorRight), and the parameter list ((byte Spd)). You put it before.

Manor is talking about the below code

 {
void MotorLeft( byte Spd)
    digitalWrite(motorDirPinA1, LOW);
    digitalWrite(motorDirPinB1, HIGH);
    analogWrite(motorSpeedPin1, Spd);

    digitalWrite(motorDirPinA2, HIGH);
    digitalWrite(motorDirPinA2, LOW);
    analogWrite(motorSpeedPin2, Spd);
 }
 {
  void MotorRight (byte Spd)
  digitalWrite(motorDirPinA1, HIGH);
  digitalWrite(motorDirPinB1, LOW);
  analogWrite (motorSpeedPin1, Spd);

  digitalWrite(motorDirPinA2, LOW);
  digitalWrite(motorDirPinB2, HIGH);
  analogWrite (motorSpeedPin2, Spd);
 }
    
 }
void MotorStop()
{
    analogWrite(motorSpeedPin1, 0);
    analogWrite(motorSpeedPin2, 0);  
}

For e.g. the code

 {
  void MotorRight (byte Spd)
  digitalWrite(motorDirPinA1, HIGH);
  digitalWrite(motorDirPinB1, LOW);
  analogWrite (motorSpeedPin1, Spd);

  digitalWrite(motorDirPinA2, LOW);
  digitalWrite(motorDirPinB2, HIGH);
  analogWrite (motorSpeedPin2, Spd);
 }

See where the open braces, it should be below void … statement

void MotorRight (byte Spd)
{
   //code goes here.
}

Same with MotorLeft ()

Next there is an orphan ‘{’ just above MotorStop()

If OP uses the auto-format option of the IDE, the problem might immediately be come clear. Just an excerpt

void MotorBackward( byte Spd)
{
  ...
  ...
}
{
  void MotorLeft( byte Spd)
  ...
  ...
}

Look at the position of void MotorLeft; functions should always start at the beginning of a line after an auto-format and void MotorLeft does not.

Below the correction for the above

void MotorBackward( byte Spd)
{
  ...
  ...
}
void MotorLeft( byte Spd)
{
  ...
  ...
}

Note how the second '{' has moved. You need to fix all of those errors. @Delta_G was right about misplaced '{' and '}' and @manor_royal pointed them out (as did @sarouje while I was typing).

Thank you sarouje, Chris, and Manor the light bulb in my head just clicked on I see it now.

Thank you everyone success