Cant figure out how to put two codes together to make it work.

hi all,

I starting a new project where I need rotary encoder (KY-040 360 Degrees Rotary Encoder Module ) to be able to change brushless motor speed. Im using brushless ESC to control that motor.

this code works with potentiometer:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

and this code is for rotary encoder:

#include <ClickEncoder.h>
#include <TimerOne.h>


ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
  encoder->service();
}

void setup() {
  Serial.begin(9600);
     encoder = new ClickEncoder(A1, A0, A2);
  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr); 
  
  last = -1;
}

void loop() {

value += encoder->getValue();
  
  if (value != last) {
    last = value;
    Serial.print("Encoder Value: ");
    Serial.println(value);
  }
  
    
}

How can I combine those codes to make it work? How to swap potentiometer with rotary encoder in code?

Thanks
Spektus

You get a value of between 0 and 180 from the analogRead()/map() and use this to set the speed of the motor

What range of values do you get from the rotary encoder ?

NOTE : the standard Servo library user timer1 and so does your code for the rotary encoder by the look of it. There is an alternative servo library named ServoTimer2 but read the documentation carefully about the function used to control the servo/motor. From memory it is not write() but I could be wrong.

What range of values do you get from the rotary encoder ?

Rotary encode just counts turns, if I turn right its adding +1, if I turn left goes -1. I believe I don't have range on rotary encoder.

spektus: Rotary encode just counts turns, if I turn right its adding +1, if I turn left goes -1. I believe I don't have range on rotary encoder.

So, if you started at 0 you could add 1 to it as you tuned it right and vice versa. Constrain the total to the required range and use it as your motor speed value.

This is what i come up with, but whatever reason that rotary encoder don’t want to adjust speed of motor, but on serial.print when I turn encoder its change value. What im doing wrong here?

#include <Servo.h>
#include <ClickEncoder.h>
#include <TimerOne.h>

Servo myservo;
ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
  encoder->service();
}

void setup() {
  Serial.begin(9600);
    myservo.attach(9);
     encoder = new ClickEncoder(A1, A0, A2);
  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr); 
  
  last = -1;
}

void loop() {
  
 
value += encoder->getValue();
  
  if (value != last) {
    last = value;
    Serial.print("Encoder Value: ");
    Serial.println(value);
  }
  
 myservo.write(value);   
}

Is the value variable changing when you turn the encoder ?

Did you note what I said about the user of timer1 in my previous reply ?

Hi Spektus,

What Bob was saying is that two of your libraries compete for control of the same internal hardware. He suggested that you use an alternate servo library. This will likely be the easiest solution.