Hello!
Hardware: Arduino Uno R3, Parallax 360 degree feedback servo, Velleman WPI435 digital encoder, 4-digit 7 segment display module
The servo is powered from the Arduino. I know that is not good, however right now there is no other possibility, but if this might be the reason for the malfunction, I would be "glad" to hear that. The feedback of the servo uses one interupt pin and the other one is used for the CLK from my encoder.
I want to turn the servo from 0 to 300 degrees corresponding to a value from 0 to 100 which I increase and decrease with the encoder, the value from 0 to 100 is displayed at the display. I had to use a mix of the standard arduino servo library and a library made for the parallax feedback servo (Link to Github repository: GitHub - HyodaKazuaki/Parallax-FeedBack-360-Servo-Control-Library-4-Arduino: Arduino library which control Parallax FeedBack 360° High Speed Servo easy. ), because I had troubles with servo jitter (at least thats what I think it is) when using the parallax libraries .rotate() function and changing thhe angle in small increments.
Now the servo turns, always in between the 0 and 300 degree range, but it sometimes bounces back. I feel like it works smoother in one direction (counterclockwise) than the other way round, I thought this might have to do with the interupt.
Here is my code:
#include "TM1637.h"
#include <Servo.h>
#include <Encoder.h>
#include "FeedBackServo.h"
Encoder myEnc(3, 7);
int8_t TimeDisp[] = { 0x00, 0x00, 0x00, 0x00 };
long oldPosition = 0;
long newPosition;
long displayValue_new = 0;
long displayValue_old = 0;
long displayValue_tmp = 0;
int maxDeg = 300;
int startDeg = 0;
int displayValue_last = 0;
bool firstLoop = true;
#define FEEDBACK_PIN 2
#define CLK 12 //pins definitions for TM1637 and can be changed to other ports
#define DIO 13
TM1637 tm1637(CLK, DIO);
Servo myservo; // create servo object to control a servo
FeedBackServo servo = FeedBackServo(FEEDBACK_PIN);
void setup() {
myservo.attach(5); // attaches the servo on pin 9 to the servo object
tm1637.set();
tm1637.init();
Serial.begin(9600);
}
void loop() {
newPosition = myEnc.read();
if (newPosition != oldPosition) {
displayValue_new = displayValue_old - (newPosition - oldPosition);
if (displayValue_new <= 100 && displayValue_new >= 0) {
displayValue_old = displayValue_new;
// Serial.println(displayValue_old);
}
else {
if (displayValue_new >= 100) {
displayValue_new = 100;
displayValue_old = displayValue_new;
// Serial.println(displayValue_old);
} else if (displayValue_new <= 0) {
displayValue_new = 0;
displayValue_old = displayValue_new;
// Serial.println(displayValue_old);
}
}
oldPosition = newPosition;
}
displayValue_tmp = displayValue_new;
for (int i = 3; i >= 0; i--) {
TimeDisp[i] = displayValue_tmp % 10; // remainder of division with 10 gets the last digit
displayValue_tmp /= 10; // dividing by ten chops off the last digit.
}
displayValue_tmp = displayValue_new * maxDeg / 100;
if (firstLoop == false) {
if (displayValue_new != displayValue_last) {
if (displayValue_tmp > servo.Angle()) {
myservo.write(1280); // sets the servo position according to the scaled value
}
if (displayValue_tmp < servo.Angle()) {
myservo.write(1720);
}
} else myservo.write(1500);
}
delay(15);
Serial.println(servo.Angle());
Serial.println(displayValue_new);
Serial.println(displayValue_last);
Serial.println(firstLoop);
tm1637.display(TimeDisp);
displayValue_last = displayValue_new;
firstLoop = false;
}
Can please anyone tell me why this is happening?
And why does the servo always spins for 4 seconds clockwise, stops for 2 seconds and then spins for another one until it stays in position? This I don't understand at all.
And last, why does the servo still turn very slowly sometimes, while testing with the hold pulsewith (1500), is this related to powering it with the arduino?
Thank you very much for your help!
