Hello.
Im working with servos for my robot arm and have a problem with humming/buzzing servos. So my question is how to get rid of it? i need to use delays for my joystick input, but is there a way around it? I've heard of a command to use but forgot how it's called, or if i can use some kind of loop (while,if,...). I need a delay that doesn't stop the whole program.
Thank you in advance,
Tim.
i need to use delays for my joystick input,
It is almost certain that you don't but as you did not post your code it is difficult to provide help.
I've heard of a command to use but forgot how it's called, or if i can use some kind of loop (while,if,...). I need a delay that doesn't stop the whole program.
See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.
Oh sorry here you go. Do millis work without delaying whole program? Thanks 
Nov dokument z besedilom.txt (2.74 KB)
Do millis work without delaying whole program?
If you use it properly. Read the details in the links I provided
Yes i do understan how millis work now but i have a problem implementig it into my program. You just call delay whenever you want, but this is a bit more complicated for me... any ideas?
For the slower membeers, here's the code...
#include <Servo.h>
#include <LiquidCrystal.h>
#include <SimpleTimer.h>
Servo s1;
Servo s2;
Servo s3;
//Servo s4;
const int rs=6,en=7,d4=2,d5=3,d6=4,d7=5;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
SimpleTimer timer1;
SimpleTimer timer2;
//int joysticka
//int button1 = 8;
int joyX = 0;
int joyY = 1;
//inicializacija senzorja
int trigPin = 13;
int echoPin = 12;
long duration;
int distance;
int val=0;
int joyVal;
int x = 90;
int y = 170;
//int x2 = 90;
int y2 = 90;
//unsigned long prevMillis = 0;
//unsigned long interval = 10000;
void senzor() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Razdalja: ");
if(distance>199||distance<6){
Serial.println("out of bound.");
}
if(distance<200&&distance>5){
Serial.println(distance);
}
}
void LCD() {
lcd.setCursor(0,0);
lcd.print("Razdalja: ");
lcd.setCursor(0,1);
if(distance>199||distance<7){
lcd.print("Out of bound.");
}
if(distance<200&&distance>6){
lcd.print(distance);
lcd.print(" cm ");
}
}
void setup() {
//servo
s1.attach(11);
s2.attach(10);
s3.attach(9);
s2.write(y);
s3.write(y);
//senzor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//lcd
lcd.begin(16,2);
Serial.begin(9600);
timer1.setInterval(1000, senzor);
timer2.setInterval(1000, LCD);
}
void loop()
{
//SimpleTimer
timer1.run();
timer2.run();
//if(joyX==LOW&&joyY==HIGH){
//S1 servo
joyVal = analogRead(joyX);
//slow
if(joyVal>550&&joyVal<900)
{
if(x<=180){
x=x+1;
s1.write(x);
}
}
//fast
if(joyVal>900)
{
if(x<=180){
x=x+2;
s1.write(x);
}
}
//slow
if(joyVal<450&&joyVal>100)
{
if(x>=0){
x=x-1;
s1.write(x);
}
}
//fast
if(joyVal<100)
{
if(x>=0){
x=x-2;
s1.write(x);
}
}
//Serial.println(x);
//Serial.println(analogRead(joyX));
delay(12);
//S2 in S3 servo
joyVal = analogRead(joyY);
if(joyVal>550&&joyVal<1023)
{
if(y<=170){
y=y+1;
s2.write(y);
s3.write(y);
}
}
if(joyVal==1023)
{
if(y<=170){
y=y+2;
s2.write(y);
s3.write(y);
}
}
if(joyVal<450&&joyVal>0)
{
if(y>=70){
y=y-1;
s2.write(y);
s3.write(y);
}
}
if(joyVal==0)
{
if(y>=70){
y=y-2;
s2.write(y);
s3.write(y);
}
}
//Serial.println(y);
//Serial.println(analogRead(joyY));
delay(12);
//}
}
Your delays are quite short, but nonetheless they may affect your perception of some functions.
More likely, the servo calls don't return until the movement has completed - you may want to look for an asynchronous servo library that returns immediately, and runs in te 'background' - using millis()
Thanks for y'all help but i fugured out that the main problem is in voltage. I connected an external power supply to servos insted running it trough arduino and the delays aren't a disturbance...for now.
I'll keep you updated 
If you were powering the servos directly from the Arduino then you were taking chances as they can take more current than the Arduino can safely provide.