I am very new to all this so please bare with me. I am building a prop moving bot that sweeps items in and out of view. The length of the sliders are only about 5 foot in length.
I have been trying to code the servos to work in the correct speed and direction which I finally got. But how do I tell it the distance of travel. My motors right now just keep going and going on forever. This is the code I am playing with:
//90 is stopped
//Deer & coyote speed - 70/90/110
//Bugs - 50/90/130
#include <Servo.h>
const int servo1 = 2;
const int servo2 = 3;
const int servo3 = 4;
//const int home1 = 4;
//const int home2 = 5;
//home buttons
//int h1 = 0;
//int h2 = 0;
int v1 = 90;
int v2 = 90;
int v3 = 90;
int x1 = 1;
int x2 = 1;
Servo myservo1;
Servo myservo2;
Servo myservo3;
void setup() {
pinMode(servo1,OUTPUT);
pinMode(servo2, OUTPUT);
pinMode(servo3, OUTPUT);
// pinMode(home1, INPUT);
// pinMode(home2, INPUT);
myservo1.attach(servo1);
myservo2.attach(servo2);
myservo3.attach(servo3);
static int v1 =90;
static int v2 =90;
static int v3 = 90;
//home 1st servo
//while (h1 != 1) {v1=100;}
//v1=90;
//home 2d servo
//while (h1 != 1)v2=100;
//v2=90;
}
void loop() {
v3 = 95;
//while(h1 != 1) {
myservo1.write(v1);
myservo2.write(v2);
myservo3.write(v3);
//delay(1000);
for(int counter = 0; counter < 5; counter++)
{
// Let’s try and move thing one
switch(counter)
{
// move servo 1
case 1: v1=90;delay(1000);break;
case 2: v1=75; delay(20000); break;
case 3: v1=90; delay(1000); break;
case 4: v1=100; delay(1000); break;
case 5: v1=120; delay(1000); break;
case 6: v1=180; delay (1000); break;
default: v1=90; break;
v2=v1;
v3=v1;
}
}
/*
switch (x1) {
case 1: v1=130; x1=2; delay(1000); break; //increase v1 (up to 180) to increase speed of travel out (if it's the wrong direction, make it less than 90 (down to 0)
case 2: v1=90; x1=3; delay(20000); break; //increase delay here to increase amount of travel
case 3: v1=50; x1=4; delay(1000); break; //decrease v1 (down to 0) to increase speed of travel in (if it's the wrong direction, make it larger than 90 (up to 180)
case 4: v1=90; x1=1; delay(20000); break; //increase delay here to match above
default: v1=90; x1=1; break;
}
switch (x2) {
case 1: v2=130; x2=2; delay(1000); break;
case 2: v2=90; x2=3; delay(20000); break; //increase delay here to increase amount of travel
case 3: v2=50; x2=4; delay(1000); break;
case 4: v2=90; x2=1; delay(20000); break; //increase delay here to match above
default: v2=90; x2=1; break;
}
/
/
switch (x) {
case 1: v1=70; x1=2; delay(100); break; //move forward
case 2: v1=90; x1=3; delay(100); break; //slow down and stop
case 3: v1=120; x1=4;delay(100); break; //move backward
case 4: v1=90; x1=1; delay(100); break; //slow down and stop
default: v1=90; x1=1; delay(100); break;
}
*/
}
// }
//Servo::refresh();
From: Terrie Adams [mailto:terrieisme@icloud.com]
Sent: 03 September 2013 20:37
To: robert@moxhall.demon.co.uk
Subject:
//90 is stopped
//Deer & coyote speed - 70/90/110
//Bugs - 50/90/130
#include <Servo.h>
const int servo1 = 2;
const int servo2 = 3;
const int servo3 = 4;
//const int home1 = 4;
//const int home2 = 5;
//home buttons
//int h1 = 0;
//int h2 = 0;
int v1 = 90;
int v2 = 90;
int v3 = 90;
int x1 = 1;
int x2 = 1;
Servo myservo1;
Servo myservo2;
Servo myservo3;
void setup() {
pinMode(servo1,OUTPUT);
pinMode(servo2, OUTPUT);
pinMode(servo3, OUTPUT);
// pinMode(home1, INPUT);
// pinMode(home2, INPUT);
myservo1.attach(servo1);
myservo2.attach(servo2);
myservo3.attach(servo3);
static int v1 =90;
static int v2 =90;
static int v3 = 90;
//home 1st servo
//while (h1 != 1) {v1=100;}
//v1=90;
//home 2d servo
//while (h1 != 1)v2=100;
//v2=90;
}
void loop() {
v3 = 95;
//while(h1 != 1) {
myservo1.write(v1);
myservo2.write(v2);
myservo3.write(v3);
//delay(1000);
switch (x1) {
case 1: v1=130; x1=2; delay(2000); break; //increase v1 (up to 180) to increase speed of travel out (if it's the wrong direction, make it less than 90 (down to 0)
case 2: v1=90; x1=3; delay(2000); break; //increase delay here to increase amount of travel
case 3: v1=50; x1=4; delay(1000); break; //decrease v1 (down to 0) to increase speed of travel in (if it's the wrong direction, make it larger than 90 (up to 180)
case 4: v1=90; x1=1; delay(2000); break; //increase delay here to match above
default: v1=90; x1=1; break;
}
switch (x2) {
case 1: v2=130; x2=2; delay(1000); break;
case 2: v2=90; x2=3; delay(20000); break; //increase delay here to increase amount of travel
case 3: v2=50; x2=4; delay(1000); break;
case 4: v2=90; x2=1; delay(20000); break; //increase delay here to match above
default: v2=90; x2=1; break;
}
/*
switch (x) {
case 1: v1=70; x1=2; delay(100); break; //move forward
case 2: v1=90; x1=3; delay(100); break; //slow down and stop
case 3: v1=120; x1=4;delay(100); break; //move backward
case 4: v1=90; x1=1; delay(100); break; //slow down and stop
default: v1=90; x1=1; delay(100); break;
}
*/
// }
//Servo::refresh();
}
Can someone take the time to help me work this out. I need to finish this off by tomorrow night as its for a display for my kids 4H group. Also please don't expect me to understand a bunch of code speak. I need someone that can translate to noob! Thanks so much in advance.
Oh btw there are 2 servos running in and out and two up and down, and one servo that just rotates a quarter turn and back controlling a small rabbit that will sit up in the front.