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);
myservo.attach(9);
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;
Serial.println(val);
Serial.println(Pos);
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(encdir);
Serial.print(" -- Value: ");
Serial.println(counter);
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}
So when I run the program, I'm not getting any movement on the servo.
Thanks.