I am having a huge problem with servo jitter and I have been trying for weeks to get it to stop. At this point I am thinking that is must be something with the timers and other libraries I am using in my program that is causing this. I tried creating a program without the servo library and that improved the problem a little bit. However, it still is not good enough. From some of the things I have read it sounds like delays can really mess with things since they halt the entire program for that amount of time. The only problem is I am not even sure how I would begin to create a program that would do this. Is it possible? How would I go about it? I have found examples of one or the other online but not both.
nathan0929:
I am having a huge problem with servo jitter and I have been trying for weeks to get it to stop. At this point I am thinking that is must be something with the timers and other libraries I am using in my program that is causing this. I tried creating a program without the servo library and that improved the problem a little bit. However, it still is not good enough. From some of the things I have read it sounds like delays can really mess with things since they halt the entire program for that amount of time. The only problem is I am not even sure how I would begin to create a program that would do this. Is it possible? How would I go about it? I have found examples of one or the other online but not both.
I'm guessing that you're trying to use the softwareserial.h library with the servo library (which is bad juju). Instead of rewriting the servo library, you can use the altsoftserial library and the servotimer2 library as long as you comment out the line
typedef uint8_t boolean;
I never tried it, but apparently worked for this guy.
I am having a huge problem with servo jitter
Tell us more preferably with code, schematic and examples of your debug prints
So I am actually not trying to compile those two together. I am working on a robotic hand that uses linear actuators and am developing a wrist which uses a servo. The hand already has code that works great and it uses some libraries that came with it (open source). I am just trying to integrate it with a servo and have been running into trouble.
Since I posted I was able to use this code to get it to a position
unsigned long interval= 1000;
unsigned long previousMicros=0;
bool ServoState = false;
void setup() {
pinMode(22, OUTPUT);
digitalWrite(22, ServoState);
}
void loop() {
unsigned long currentMicros = micros();
if ((unsigned long)(currentMicros - previousMicros) >= interval) {
ServoState = !ServoState;
digitalWrite(22, ServoState);
previousMicros = micros();
}
}
however, as you can see this is obviously not helpful since I need to send it different positions, not a constant one which I am doing via matlab.
I was thinking I could modify this to be able to send it different positions but I am still trying to find a process that works.
I tried this(unimportant code has been deleted for clarity)
#include "RunningMedian.h"
#include <FingerLib.h>
void setup() {
Serial.begin(38400);
finger[0].attach(5, 2, A0);
finger[1].attach(3, 6, A1);
finger[2].attach(7, 8, A2);
finger[3].attach(10, 9, A3);
finger[4].attach(11, 12, A4);
pinMode(22, OUTPUT);
}
void loop() {
while (Serial.available() > 0) {
byte input[6];
Serial.readBytes(input, 6);
for (int i = 0; i < 5; i++) {
finger[i].writePos(convertPos(input[i]));
}
int integer = int(input[5]);
int wrist1 = map(integer, 0, 255, 850, 2100);
unsigned long interval= wrist1;
unsigned long previousMicros=0;
bool ServoState = false;
unsigned long currentMicros = micros();
if ((unsigned long)(currentMicros - previousMicros) >= interval) {
ServoState = !ServoState;
digitalWrite(22, ServoState);
// save the "current" time
previousMicros = micros();
}
Unfortunately that did not provide the desired result so I am troubleshooting that right now to see where I went wrong.
previousMicros = micros();Tell me about the scope of previousMicros
I am confused about your question. previousMicros is a unsigned long that is initially equal to 0 that changes for each loop so that the servo can move using PWM.
previousMicros is a unsigned long that is initially equal to 0
Bingo!
Try making it static.