I pieced together a sketch to control a servo that turns an encoder. Eventually I plan to build this up to a DMX input, so I have the input range from 0 - 255. Basically, it's supposed to ask you to input range from 0 to 255 and then it changes that value to 0 -180 for servo control. Then it's supposed to compare that to the encoder and rotate the correct direction until the encoder value and requested values match. I'm just learning arduino so I'm sure I've made some mistake. Here is the code:

// Rotary Encoder Inputs
#define inputCLK 22
#define inputDT 24

#include <Servo.h>

Servo myservo;
int pos = 90;
int counter = 90; 
int currentStateCLK;
// Setup Serial Monitor
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
int previousStateCLK; 
String encdir ="";

void setup() { 
  Serial.begin (9600);  
  previousStateCLK = digitalRead(inputCLK);
  // Set encoder pins as inputs  
  pinMode (inputCLK,INPUT);
  pinMode (inputDT,INPUT);
void loop() { 
  Serial.println("Enter a number from 0 to 255");  
  while (Serial.available() == 0); {}  
  int val = Serial.parseInt(); // read int or parseFloat for ..float...
  long Pos = val;
  Pos = Pos * 180 / 255;
  if (Pos > Counter) {
    myservo.write 0;
  if (Pos < Counter) {
    myservo.write 180;
  if (Pos = Counter) {
    myservo.write 90;

  // 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 --;
      encdir ="CCW";
    } else {
      // Encoder is rotating clockwise
      counter ++;
      encdir ="CW";       
    Serial.print("Direction: ");
    Serial.print(" -- Value: ");
  // Update previousStateCLK with the current state
  previousStateCLK = currentStateCLK; 

So when I run the program, I'm not getting any movement on the servo.


  • 1
    reduce your code to only turn the servo in one direction ... nothing more ... it is pointless to even look at your code until you verify that the servo actually works
    – jsotola
    Dec 27, 2020 at 7:52

1 Answer 1


Your initial problem is here:

  if (Pos = Counter) {

If the result (fall through) of assigning Counter to Pos is non-zero then stop.

That's not right. You're assigning, not comparing. In C == is comparison and = is assignment. Instead it should read:

  if (Pos == Counter) {

However there is another, more sneaky, problem with your code - the usage of Serial.parseInt().

If you send the string 56\r\n as you typically would (with the CR+LF line ending typical of serial communication) then parseInt will read the first portion of the string up to and including the first non-digit character and parse it as a number. That is, the 56\r and return the value 56. Then on the next pass it will see there is still data in the serial buffer and parse it, up to and including the first non-digit character - which is just \n. So it will return 0.

That means the first pass through loop() you get 56, and then immediately after in the next pass through loop() you get 0.

And then, looking at your code a third time, there is another bigger problem: you block waiting for serial input, and your Pos variable is local and will be lost each iteration. I think you meant to use pos, which is a global variable, and not create a new long Pos variable each iteration. And where is Counter defined? That surely should be the global counter variable?

If you're blocking waiting for serial input then how can you be controlling the servo to get it to the desired location?

So, in short, there are so many errors and problems with your code that you need to go back to basics and re-think / re-write many portions of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.