You might find it easier to use a servo control library that has a speed setting like this for example (I haven't used it myself, just know about it).
Note: The Servo library can use any digital I/O pin, not just PWM (analogWrite()) pins. On the UNO the library can control 12 servos. On the MEGA: 48 servos.
You put an extra ';' between 'loop()' and '{'.
You misspelled "startTime3" as "starTime3".
The "<<< " and " >>>" marks are not valid code.
With those things fixed the sketch compiles with only a few warnings:
#include <Servo.h>
//Create a for loop
// Add the servo library.
#include<Servo.h>
//Define the 5 servo motors
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
unsigned long startTime1 = millis();
unsigned long startTime2 = millis();
unsigned long startTime3 = millis();
unsigned long startTime4 = millis();
unsigned long currentTime = millis();
void setup()
{
// define the servo signal inputs ( digital PMW 3,5,6,9,10)
servo1.attach(3);
servo2.attach(5);
servo3.attach(6);
servo4.attach(9);
servo5.attach(10);
}
void loop()
{
servo1.write(0);
if (currentTime - startTime1 > 1000);
servo2.write(45);
if (currentTime - startTime2 > 2000);
servo3.write(90);
if (currentTime - startTime3 > 3000);
servo4.write(135);
if (currentTime - startTime4 > 4000);
}
The warnings are about the 'if' statements that contain only one null statement: ';'. The warning message recommends that you make the intent to do nothing clearer by putting brackets around the null statement:
if (currentTime - startTime4 > 4000) {;}
You probably didn't want to do nothing. At a minimum you would typically reset the timer so it goes off again:
servo1.write(0);
if (currentTime - startTime1 > 1000)
{
startTime1 = currentTime;
}
Now you have a block of code that gets executed every 1001 milliseconds (1.001 seconds). What did you want to happen at that interval? (You probably wanted 1 second. If so use ">= 1000" instead of "> 1000").
WARNING: You should put:
currentTime = millis();
at the top of loop(). Without it, your time is stuck near 0 forever.
how do you add it to the library on the arduino IDE
ive been seeing the 9600 serial begin. is that necessary to add it
Here is an example of how you would get the servos to do something interesting. The timer loop for each servo cycles through a list of positions. Each time the timer expires the servo moves to the next position on the list.
Note: The 'sizeof' operator returns the array size IN BYTES. Since the lists are lists of bytes, this is the same as the length of the list. For arrays that aren't 'byte' or 'char' you have to divide the 'sizeof' value by the number of bytes in each element to get the length of the list. Easily done with: (sizeof list / sizeof list[0])
Note: The modulo operator ('%') gives the integer remainder after division. It wraps the index back to 0 when the index goes past the end of the list. This will just start the list over from the beginning when the end is reached.
#include<Servo.h>
//Define the 5 servo motors
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
unsigned long startTime1 = millis();
unsigned long startTime2 = millis();
unsigned long startTime3 = millis();
unsigned long startTime4 = millis();
void setup()
{
// define the servo signal pins
servo1.attach(3);
servo2.attach(5);
servo3.attach(6);
servo4.attach(9);
servo5.attach(10);
}
// Lists of positions for each servo to cycle through
byte servo1positions[] = {45, 120, 10};
byte servo2positions[] = {85, 12, 100, 53};
byte servo3positions[] = {135, 45};
byte servo4positions[] = {120, 10, 60, 14, 90};
// Current index into the list of positions
byte servo1index = 0;
byte servo2index = 0;
byte servo3index = 0;
byte servo4index = 0;
void loop()
{
unsigned long currentTime = millis();
if (currentTime - startTime1 >= 1000)
{
startTime1 = currentTime;
servo1index = (servo1index + 1) % sizeof servo1positions;
servo1.write(servo1positions[servo1index]);
}
if (currentTime - startTime2 >= 2000)
{
startTime2 = currentTime;
servo2index = (servo2index + 1) % sizeof servo2positions;
servo2.write(servo2positions[servo2index]);
}
if (currentTime - startTime3 >= 3000)
{
startTime3 = currentTime;
servo3index = (servo3index + 1) % sizeof servo3positions;
servo3.write(servo3positions[servo3index]);
}
if (currentTime - startTime4 >= 4000)
{
startTime4 = currentTime;
servo4index = (servo4index + 1) % sizeof servo4positions;
servo4.write(servo4positions[servo4index]);
}
}
The Serial.begin(9600) code will allow to exchange data/messages between Serial Monitor and the MCU of the Arduino UNO Board.
youve made my day sir. THANK YOU its something that i know coming from just starting but having this is a WAY!!!! better alternative. THANK YOU.........
couple questions of whats the ('%') and sizeof
and also needing this
(((servo2.write(servo2position[servo2index];))) i would need to add that after claiming a if statement with millis.
STILL new to the programming but thank you again so much
Yes, a bit complex for a first time user.
Time to study
gotta long way to go. But i love it
As if you needed more to learn.
![]()
Have fun !
Regarding serial.begin(9600): that is the link between the Arduino and the Serial Monitor in the IDE. if you serial.begin(115200) you communicate 12 times faster, leaving more processor time for other things. if it works at 115200, use that.
I EXPLAINED THEM BOTH ALREADY. Go back and read the explanations.
I don't understand the question. You have to use .write() to tell the servo the new position to go to. In this case, we are looking the position up in a list of positions. If you can figure out a formula for the position, you can calculate it.
% (Modulus) Operator:
If I am asked to answer what will be the quotient and remainder after this operation: 7/3? I will answer as: 2 and 1.
The above dialog could be represented by the following programming codes:
byte y = 7;
byte remainder = y%3; //remainder = 1
byte quotient = y/3. //quotient = 2
sizeof Operator:
The sizeof operator returns how many 8-bit (byte) data items are there in a variable. For examples:
1.
byte y = 0x23; //contains one 8-bit data item.
byte z = sizeof y; // z = 1
2.
int y = 0x1234;
byte z = sizeof y; //z = 2
3.
byte y[] = {0x12, 0x34, 0x56};
byte z = sizeof y; //z = 3
4.
char y[] = "Arduino";
byte z = sizeof y; // y = 8; why is not 7?
5.
int y[] = {12, 34, 56};
byte z = sizeof y; //z = 6 or 3?
6.
long *ptr = 0x12345678;
byte z5 = sizeof ptr; //z5 = 2; what does the result mean?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
