Hello, im looking for some help here, im new to arduino and programming.I found this thread very useful and also Robins2 code for "Excavator". i just edit code to work like i want - normal movement of servo with joystick, but now i want to add this functionality to my code - holding position of servo arm while joystick get back to neutral. Original code of Robin2 works perfect but i dont know to implement it to my code. Here is my piece or art:
#include <Servo.h>
Servo servo[1]; // create servo object to control a servo
int potPin[1]={A0};
int potValue;
int servodt=10;
int servoPin[1]={9};
int pushbutton=1;
int servoValue[1]={1472};
void setup() {
for (byte n = 0; n < 1; n++){
servo[n].attach(servoPin[n]);
}
}
void loop() {
readPotentiometers();
moveServos();
delay(servodt);
}
void readPotentiometers(){
for (byte n = 0; n < 1; n++){
potValue = analogRead(potPin[1]);
potValue = map(potValue, 0, 1023, 544, 2400);
if (potValue >= 1372 && potValue <= 1572 ) {
servoValue[1] = 1472;
}
if (potValue <= 1372 ) {
servoValue[1] = potValue + 100;
}
if (potValue >= 1572 ) {
servoValue[1] = potValue - 100;
}
if (pushbutton = 1 && potValue < 1372) {
}
}
}
void moveServos() {
for (byte n = 0; n < 1; n++) {
servo[n].writeMicroseconds(servoValue[1]);
}
}
if (pushbutton = 1 && potValue < 1372) .... here i want to add code for hold position functionality, but i cannot figure it out how.
Can someone help me?
Thanks. It's not quite the right one - it seems that guy had a few Threads about the same project, but he is no longer a member of the Forum so I can't get a link to the other ones.
Yes i was trying a code from Reply#26 and it works as i want (lets say if button is pressed).
My code previously posted ( Reply#24) also works in different way. Also with those two codes i have two ways of moving servos. Now i neeed help to switch between those two codes.
Ok maybe i didnt show you correctly what i want to achieve. Lets say joystick has 3 positions, 1 - 0 - 2: one direction - middle position - second direction. I push it to direction 1 - servo arm moves coresponding to joystick - thats ok. Then i relise joystick to 0 position - servo arm is still in previously position - thats ok. Then i move joystick to opposite direction (2) - servo arm jump fast to middle position - this ia also ok. But there i have problem, when i move joystick to "2" position, servo arm jump to middle position and then
immediately get some more degrees in opposite direction - corresponding to joystick position. Here im trying something with deadband and delay to eliminate this movement of servo arm. Hope you will understand now..
pocukrani:
Ok maybe i didnt show you correctly what i want to achieve. Lets say joystick has 3 positions, 1 - 0 - 2: one direction - middle position - second direction. I push it to direction 1 - servo arm moves coresponding to joystick - thats ok. Then i relise joystick to 0 position - servo arm is still in previously position - thats ok. Then i move joystick to opposite direction (2) - servo arm jump fast to middle position - this ia also ok. But there i have problem, when i move joystick to "2" position, servo arm jump to middle position and then
And does it behave like that when you use the code from the link I posted in Reply #25 ?