Notifications
Clear all

Using Rotary Encoder with Arduino

9 Posts
3 Users
0 Likes
1,250 Views
 lodv
(@lodv)
Member
Joined: 5 years ago
Posts: 7
Topic starter  

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)
Mr.
Joined: 5 years ago
Posts: 61
 

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

Int currentStateCLK;

hj


   
ReplyQuote
 lodv
(@lodv)
Member
Joined: 5 years ago
Posts: 7
Topic starter  

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)
Mr.
Joined: 5 years ago
Posts: 61
 

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)
Member
Joined: 5 years ago
Posts: 7
Topic starter  

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)
Mr.
Joined: 5 years ago
Posts: 61
 

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)
Member
Joined: 5 years ago
Posts: 7
Topic starter  

That's it.

Thank you so much.

Now it works.

I'm happy.

Yvonne

 

 


   
ReplyQuote
hstaam
(@hstaam)
Mr.
Joined: 5 years ago
Posts: 61
 

glad to have helped.

hj


   
ReplyQuote
(@robomaster)
Member
Joined: 3 years ago
Posts: 36
 

I need some help with the basic encoder code got it working with one encoder but when I try to add a second encoder I can't get it to work.


   
ReplyQuote