I'm trying to build a system in which a rotary encoder and a servo work together connected to the same Arduino (Uno). Each component and code work well when connected on its own to the Arduino.
However, the encoder data becomes very unstable (wrong range and not consistent) when working alongside the servo.
I'm using the Simplified Example code on this page to read the encoder data
And the most basic servo sweep example to move the servo.
I also tried using the Adafruit TiCoServo Library as my guess is the core of the problem is within the interrupts.
What is the best approach to using servos and rotary encoders in the same system, running off the same Arduino?
Would something like a PCA9685 could help?
Here is my code:
//#include <Servo.h>
#include <Adafruit_TiCoServo.h>
// Servo config
Adafruit_TiCoServo myservo; // create servo object to control a servo
int servoPin = 9;
int servoEncoderPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
int pos = 0; // variable to store the servo position
// Encoder config
int val;
int encoder0PinA = 3;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
void setup() {
Serial.begin (9600);
// servo setup
myservo.attach(servoPin);
// encoder setup
pinMode (encoder0PinA, INPUT);
pinMode (encoder0PinB, INPUT);
}
void loop() {
// sweep servo
for (pos = 0; pos <= 100; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
sensorValue = analogRead(servoEncoderPin);
get_encoder_value();
// Serial.println(sensorValue);
// Serial.print(" ");
Serial.println(encoder0Pos);
delay(50); // waits 15ms for the servo to reach the position
}
// Serial.println("stop!");
delay(1000);
for (pos = 100; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
sensorValue = analogRead(servoEncoderPin);
get_encoder_value();
// Serial.println(sensorValue);
// Serial.print(" ");
Serial.println(encoder0Pos);
delay(50); // waits 15ms for the servo to reach the position
}
// Serial.println("stop!");
delay(1000);
}
void get_encoder_value(){
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
} else {
encoder0Pos++;
}
}
encoder0PinALast = n;
}