Notifications
Clear all

Using Rotary Encoder with Arduino  

  RSS

lodv
 lodv
(@lodv)
Active Member
Joined: 1 year ago
Posts: 9
2020-06-12 2:41 pm  

I'm following the topic Using Rotary Encoder with Arduino.

I connected the rotary encoder and the Servo motor and uploaded the code from the workshop.

I get the following error message:

Arduino:1.8.10 (Mac OS X), Board:"Arduino/Genuino Uno"

Rotary_Encoder_Demo_Met_Servo:45:4: error: expected initializer before 'currentStateCLK'
currentStateCLK = digitalRead(inputCLK);
^~~~~~~~~~~~~~~
Rotary_Encoder_Demo_Met_Servo:48:4: error: expected unqualified-id before 'if'
if (currentStateCLK != previousStateCLK){
^~
Rotary_Encoder_Demo_Met_Servo:74:4: error: 'previousStateCLK' does not name a type
previousStateCLK = currentStateCLK;
^~~~~~~~~~~~~~~~
Rotary_Encoder_Demo_Met_Servo:75:2: error: expected declaration before '}' token
}
^
Meerdere bibliotheken gevonden voor "Servo.h"
Gebruikt: /Applications/Arduino.app/Contents/Java/libraries/Servo
exit status 1
expected initializer before 'currentStateCLK'

Can anybody give me a hand what to do.

Thank you in advance.

Yvonne


Quote
hstaam
(@hstaam)
Trusted Member
Joined: 1 year ago
Posts: 57
2020-06-12 2:48 pm  

you need to specify the type of currentStateCLK before using it. something like:

Int currentStateCLK;

hj


ReplyQuote
lodv
 lodv
(@lodv)
Active Member
Joined: 1 year ago
Posts: 9
2020-06-12 3:27 pm  

Thank you.

But it is defined:

// 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);

}

Any other idea?

Thanks.


ReplyQuote
hstaam
(@hstaam)
Trusted Member
Joined: 1 year ago
Posts: 57
2020-06-12 3:35 pm  

It is probably a typo before the 'if' statement. I am just guessing, as I do not see the relevant code.

hj


ReplyQuote
lodv
 lodv
(@lodv)
Active Member
Joined: 1 year ago
Posts: 9
2020-06-12 4:08 pm  

This is the whole code.

I don't see the typo, but maybe you can discover something.

Thank you for your time and help.

/*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;
}


ReplyQuote
hstaam
(@hstaam)
Trusted Member
Joined: 1 year ago
Posts: 57
2020-06-12 4:19 pm  

OK 

Right after 'void loop() there should be the opening brace, like this '{'

Just add it on after void loop() and  it will compile!

 

hj


ReplyQuote
lodv
 lodv
(@lodv)
Active Member
Joined: 1 year ago
Posts: 9
2020-06-12 6:30 pm  

That's it.

Thank you so much.

Now it works.

I'm happy.

Yvonne

 

 


ReplyQuote
hstaam
(@hstaam)
Trusted Member
Joined: 1 year ago
Posts: 57
2020-06-13 9:56 am  

glad to have helped.

hj


ReplyQuote