Code Error while compiling

Hi, I am new to Arduino. I found the following code on DroneBot Workshop where a servo is controlled by a rotary encoder but I get a compiling error.

/*
Rotary Encoder with Servo Motor Demo
rot-encode-servo-demo.ino
Demonstrates operation of Rotary Encoder
Positions Servo Motor
Displays results on Serial Monitor
DroneBot Workshop 2019
https://dronebotworkshop.com
*/

// Include the Servo Library
#include <Servo.h>

// Rotary Encoder Inputs
#define inputCLK 4
#define inputDT 5

// Create a Servo object
Servo myservo;

int counter = 0;
int currentStateCLK;
int previousStateCLK;

void setup() {

// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);

// Setup Serial Monitor
Serial.begin (9600);

// Attach servo on pin 9 to the servo object
myservo.attach(9);

// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);

}

void loop()

// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);

// If the previous and the current state of the inputCLK are different then a pulse has occured
if (currentStateCLK != previousStateCLK){

 // If the inputDT state is different than the inputCLK state then 
 // the encoder is rotating counterclockwise
 if (digitalRead(inputDT) != currentStateCLK) { 
   counter --;
   if (counter<0){
    counter=0;
   }
  
 } else {
   // Encoder is rotating clockwise
   counter ++;
   if (counter>180){
    counter=180;
   }
   
 }
 
 // Move the servo
 myservo.write(counter);
  
 Serial.print("Position: ");
 Serial.println(counter);

}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}

This is the error message I copied while compiling

Arduino: 1.8.16 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

Servo_with_Rotary:46:4: error: expected initializer before 'currentStateCLK'

currentStateCLK = digitalRead(inputCLK);

^~~~~~~~~~~~~~~

Servo_with_Rotary:49:4: error: expected unqualified-id before 'if'

if (currentStateCLK != previousStateCLK){

^~

Servo_with_Rotary:75:4: error: 'previousStateCLK' does not name a type

previousStateCLK = currentStateCLK;

^~~~~~~~~~~~~~~~

Servo_with_Rotary:76:2: error: expected declaration before '}' token

}

^

exit status 1

expected initializer before 'currentStateCLK'

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

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

If you use the autoformat tool (ctrl-t or Tools, Auto format) on your improperly posted code you will see that there is no { to open the loop() function.

Common compiler errors caused by mismatched brackets:

"does not name a type" or
"expected declaration before" or
"expected unqualified-id before" or
"expected initializer before" or
"expected constructor, destructor, or type conversion before '(' token"
Usually means you forgot a '{' or put in an extra '}' in the previous function. Since all of the open brackets have been closed, the compiler is looking for further global declarations (variables or functions). If it finds something that looks like executable code instead of a global declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"a function-definition is not allowed here before '{' token"
(can cause: "'functionName' was not declared in this scope")
Usually means you forgot a '}' or put in an extra '{' in the previous function. Since a set of brackets has not been closed yet the compiler is looking for more code to put in the function. You can't declare a function inside a function so if the compiler finds a function declaration it emits an error. Make sure that the brackets in the preceding function are in matching pairs '{' followed by '}'.

"expected '}' at end of input"
Usually means you forgot a '}' or put in an extra '{' in the last function in the sketch. Since a set of brackets has not been closed yet, the compiler is looking for more code to put in the function. When it hits the end of the file instead, it emits an error. Make sure that the brackets in the last function are in matching pairs '{' followed by '}'.

"expected primary-expression before '}' token"
Usually means you have an incomplete statement before a '}'. The block statement (between '{' and matching '}') can only contain complete statements. Complete statements always end with ';' (or '}' for a block statement).

@johankotze, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

Are you missing the starting { of loop()?

I add the missing { loop(), and it works. Now I have to figure out how to get Tower Pro SG90 to rotate continuously. When servo is not powered then I can manually turn the servo continuously.

Has the servo been modified to be a continuous servo? A continuous servo has had its feedback element (usually a potentiometer) disconnected. It is no longer a servo, it is a gear motor that can have its speed and direction controlled by a servo type signal, but not its position.

Turning a servo or continuous "servo" by hand can be hard on the gears in the servo and is not recommended.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.