Hello there,
I have recently been introduced to arduino as i am required to understand it for my final year project.
Few days ago, i am tasked to create a simple circuit and then program it to show to my supervisor. Connecting the servo motor and the led wasn't that bad, but the programming part is killing me.
I have played around a little, experimented a number of times, and even looking through the given examples. But i am unable to get my desired result.
I'm planning to have the led continuously lighted up, even when the servos motor is moving. But from the video, the code i have tested with, only lights the led up after the servos had completed it's turn.
Please advise.
Thanks,
JingKang
Below is the code and a video of my servo motor and led light.
#include <Servo.h>
Servo servo1;
const int leftServoPin = 8;
const int ledPins[] = {9,10,11,12,13};
void setup()
{
int index;
servo1.attach(8);
for(index = 0; index <= 4; index++)
{
pinMode(ledPins[index], OUTPUT);
}
}
void loop()
{
pingPong(); // Chase lights
int position;
int index;
servo1.write(90); // Tell servo to go to 90 degrees
delay(1000); // Pause to get it time to move
servo1.write(180); // Tell servo to go to 180 degrees
delay(1000); // Pause to get it time to move
servo1.write(0); // Tell servo to go to 0 degrees
delay(1000); // Pause to get it time to move
for(position = 0; position < 180; position += 2)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
// Tell servo to go to 0 degrees, stepping by one degree
for(position = 180; position >= 0; position -= 1)
{
servo1.write(position); // Move to next position
delay(20); // Short pause to allow it to move
}
}
void pingPong()
{
int position;
int index;
int delayTime = 35;
for(index = 0; index <= 4; index++)
{
digitalWrite(ledPins[index], HIGH);
delay(delayTime);
digitalWrite(ledPins[index], LOW);
}
for(index = 4; index >= 0; index--)
{
digitalWrite(ledPins[index], HIGH);
delay(delayTime);
digitalWrite(ledPins[index], LOW);
}
}
You can't tell when the servo is moving, after you command a change of position it
will move until it reaches that position, taking whatever time necessary, but not
communicating back - you can only guess for how long it moves.
Servos have a speed specification, how long it takes to move through a given angle,
so you could use that value to estimate the time it takes to complete each commanded
move, since you know the change of angle.
Thanks for the reply, but i am still a little confused as to what you are trying to imply.
Are you saying that the problem lies with the code i have for the servos? And in order for me to make it so that the led will continue lighting up even when the servos is moving, i would have to estimate the time it takes for the servos to complete a turn?
If so, how should i go about changing it? As this is part of a test circuit which i have in mind for my wall avoiding robot/obstacle avoiding robot, i would be including another 1 or 2 servos in.
Also, i have in hand 2 arduino uno with me. Would it be better if i include both uno onto my robot's chassis, separating the codes for the led, sensor, and servos?
I'm not sure what you're trying to do: what do you mean by lighting the led while the servo is moving? You want it to sense that it's moving and light up?... or else just turn the light on by program, move the servo, turn led off?
You need to command the servos without using delay so that you can quickly get back to flashing your LEDs. As ever, take a look at the blink without delay example that ships with the IDE.
sorry for not making myself clear.
I'm planning to have the led continuously lighted up, even when the servos motor is moving. But from the video, the code i have tested with, only lights the led up after the servos had completed it's turn.
I'm planning to have the led continuously lighted up, even when the servos motor is moving.
That is had to understand. If you just want the LED lit while the servo is moving, then use an incremental timed servo move similar to the servo sweep example. Turn the LED on, start the incremental servo move, then turn the LED off after the last servo command.
Yes! Except that, instead of having the LED just lighted up, i would like to have it move in a pingPong manner. Thus the pingPong code.
But i am troubled as to how i should go about coding it.
The current code i have only allows the led to finish 1 cycle of pingPong, then move the servos. And then the loop continues.
jingkang93:
Yes! Except that, instead of having the LED just lighted up, i would like to have it move in a pingPong manner. Thus the pingPong code.
There's an easier way if you use the Sweep example.
But i am troubled as to how i should go about coding it.
The current code i have only allows the led to finish 1 cycle of pingPong, then move the servos. And then the loop continues.
You should probably throw out most of that code in loop() . Load up the Sweep example and build on that one. Here's the loop() function of that example. The comments with *** show how you might want to ping-pong the LEDs.
void loop()
{
// *** turn LED1 ON. turn LED2 OFF
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
// *** if (pos % 10 == 0), toggle LED1, toggle LED2 (for every pos evenly divisible by 10)
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
// *** turn LED1 OFF, turn LED2 ON
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
// *** if (pos % 10 == 0), toggle LED1, toggle LED2
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
// *** turn LED1 OFF, turn LED2 OFF
Thank you so much for helping me with this.
I have finally get it to work with the code you have provided.
One last question, in the code, you comment out the if (pos % 10 == 0)
Does it mean that, everytime my servos move 10 degree, my led would light up?
@zoomkat
after reading the code that lar3ry have provided, i finally understood what do you mean by counting the moves and then toggle my led accordingly. Thank you for your help!
jingkang93:
Thank you so much for helping me with this.
I have finally get it to work with the code you have provided.
Great!
One last question, in the code, you comment out the if (pos % 10 == 0)
Does it mean that, everytime my servos move 10 degree, my led would light up?
That comment was not actual code (well, you can use that as an if statement), but the toggles need to be coded.
What it means is that every time pos is evenly divisible by 10, you will toggle the LEDS, so if pos is indicating degrees, yes, every ten degrees. You can make it every 5, or 2, or 20, or whataver looks best.
Let us know how that goes, and tell us if it looks good.
Servo.write() takes a value between 0 and 180 so you could have 180 fade steps using a for loop. Servo writeMicroseconds() takes a value between 1000 and 2000 so you could have 1000 fade steps using a for loop.
In either case the principle would be to move the servo to the next position, change the LED brightness, wait a while then do it again until the servo has moved to its target position. The LED brightness figure used in analogWrite() would need to be derived from the servo position using the map() function but the LED brightness would not change in a linear manner because of the steps in value produced by the mapping.
jingkang93:
Thank you so much for helping me with this.
I have finally get it to work with the code you have provided.
Great!
One last question, in the code, you comment out the if (pos % 10 == 0)
Does it mean that, everytime my servos move 10 degree, my led would light up?
That comment was not actual code (well, you can use that as an if statement), but the toggles need to be coded.
What it means is that every time pos is evenly divisible by 10, you will toggle the LEDS, so if pos is indicating degrees, yes, every ten degrees. You can make it every 5, or 2, or 20, or whataver looks best.
Let us know how that goes, and tell us if it looks good.