DC Motor with in build Encoder code issues

Hello,I recently brought a DC Motor with encoder build in off Amazon;

DC Motor Encoder LINK

Motor Driver LINK

Ive got it all wired up, ran a few test programs and all is a success at component and hardware level.

Code wise not so much, ill attached the full code at the bottom but please bare in mind its a working progress.

as soon as i comment back in any of the if statments on the pushbuttons i get the following error;

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\***\Documents\Arduino\motorencodertestTEST\motorencodertestTEST.ino: In function 'void loop()':

motorencodertestTEST:108:17: error: a function-definition is not allowed here before '{' token

 void Forward () {

                 ^

motorencodertestTEST:114:17: error: a function-definition is not allowed here before '{' token

 void Reverse () {

                 ^

motorencodertestTEST:119:14: error: a function-definition is not allowed here before '{' token

 void Stop () {

              ^

motorencodertestTEST:123:1: error: expected '}' at end of input

 }

 ^

exit status 1
a function-definition is not allowed here before '{' token

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

I hope someone can shed some light on this for me, struggling here.

// Created By XXX
//---------------------------------------------------------
// LED Output DATA
const int led1 =  2;        // LED 1 pin 2 on Arduino Board. Colour = BLUE
const int led2 =  3;        // LED 2 pin 3 on Arduino Board. Colour = RED
const int led3 =  4;        // LED 3 pin 4 on Arduino Board. Colour = AMBER
const int led4 =  5;        // LED 4 pin 5 on Arduino Board. Colour = BLUE
// PushButton Input DATA
const int button1 = 6;      // Pushbutton 1 pin 6 on Arduino Board
const int button2 = 7;      // Pushbutton 2 pin 7 on Arduino Board
const int button3 = 12;     // Pushbutton 3 pin 12 on Arduino Board
const int button4 = 13;     // Pushbutton 4 pin 13 on Arduino Board
// PushButton Input state checker DATA
int buttonState1 = 0;       // Pushbutton 1 current state for CODE
int buttonState2 = 0;       // Pushbutton 2 current state for CODE
int buttonState3 = 0;       // Pushbutton 3 current state for CODE
int buttonState4 = 0;       // Pushbutton 4 current state for CODE
// DC Motor Encoder DATA
#define outputA 9
#define outputB 8
int counter = 0; 
int aState;
int aLastState; 
const int SpinFwd = 10 ;
const int SpinRev = 11 ;
int target = 0;
//long Forward = (10,1 11,0)
//long Reverse = (10,0 11,1)
//long Stop = (10,1 11,1)
//---------------------------------------------------------
 void setup() { 
//
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
//
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
//
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
//
pinMode(SpinFwd,OUTPUT) ;  //Logic pins are also set as output
pinMode(SpinRev,OUTPUT) ;
//
//Forward = (10,1 11,0)
//
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);   
} 
 void loop() { 
//---------------------------------------------------------
//Read Current State of each pushbutton
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  buttonState3 = digitalRead(button3);
  buttonState4 = digitalRead(button4); 
//---------------------------------------------------------
//Read Encoder
   aState = digitalRead(outputA); // Current state of Output A
   if (aState != aLastState){ // If the previous value of Output A is different to the current state then the encorder is moving.
     if (digitalRead(outputB) != aState) { 
       counter ++; // Count UP (If Output B is less than or equal to Output A then the motor is running Clockwise [forward])
     } else {
       counter --; // Count Down (If Output B is more than Output A then the motor is running Anti-Clockwise [Reverse])
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   aLastState = aState; // Updates the previous state of the outputA with the current state
//---------------------------------------------------------
//    if(counter != 500) {
//  Forward();
//  } 
//    if(counter > 501) {
//  Stop();
//  } 
//---------------------------------------------------------  
// Auto pushbutton input of Stepper Position
//Push Button 1
//  if (buttonState1 == HIGH) {
//   (target=500);
//Push Button 2
//  if (buttonState2 == HIGH) {
//    target=1000;
//Push Button 3
//  if (buttonState3 == HIGH) {
//    target=1500;
//Push Button 4
//  if (buttonState4 == HIGH) {
//    target=2000;
// Run to target value
//  if (target == 500 && counter!= 500){
//      Forward();
//  }
//  if (target == 500 && counter> 500){
//      Reverse();
//  }
//  if (count == 500) { //need to build in some tolerance here
//    Stop();

}

void Forward () {
  digitalWrite(SpinFwd, HIGH); 
 digitalWrite(SpinRev, LOW); 
  
}

void Reverse () {
  digitalWrite(SpinFwd, LOW); 
 digitalWrite(SpinRev, HIGH); 
  
}
void Stop () {
  digitalWrite(SpinFwd, LOW); 
 digitalWrite(SpinRev, LOW); 
  
}

My goal with this program is to have 4 separate push buttons when a push button is pressed it goes to the defined encoded count waits X seconds then returns back home - my code doesn't do that yet but i was chipping away at it bit by bit until i came across this error which stumped me.

Any help would be massively appreciated.

Thanks,
Anthony.

You have a misplaced curly bracket, probably. Use the auto format tool in the IDE to indent your code and the error may be easy to see. I put every { and } on ther own lines, that also helps.

You cannot define a function inside of a function. I think the } to close the loop() funtion is missing.

Hello,

Thanks for the reply, I thought the exact same and its taken me a while to try and debug turner out its because the variable count doesn't exist and its actually counter, once that was changed and i complied it works... strange one, id have thought it would come up with variable not defined.

But resolved anyway.....

MORE SO....

I don't think the code will work for what I want in any case but i wanted to debug in case I come across it in future.

Couple of alternative questions for the forum would be...

  • How can I simply code in tolerance for the encoder, for example I want to go to encoder count 500 but in actual fact when the motor switches off till end up being 512count etc how do i build tolerance for this?... any direction to point me towards would be great (baring in mind I'm not a strong
    programmer)

Many thanks.