I don't get it. I'm new to Arduino, but not to programming. I don't understand why led pin 13 keeps blinking.
here is my code..
#include <Servo.h>
// X servo angle will stay in [min_x, max_x] range
// Y servo angle will stay in [min_y, max_y] range
// to be adjusted to the size of your living room
float min_x = 5;
float max_x = 50;
float min_y = 5;
float max_y = 35;
int min_freeze = 200;
int max_freeze = 3000;
float minimal_movement = 5;
// finding center of square for starting point
int random_delay;
float x_position = min_x + (max_x - min_x)/2;
float y_position = min_y + (max_y - min_y)/2;
float x_old_position = x_position;
float y_old_position = y_position;
float x_new_position;
float y_new_position;
float x_speed;
float y_speed;
int movement_time;
// Instantiating two servos
Servo x_servo;
Servo y_servo;
int pos = 0;
void setup() {
y_servo.attach(6); // attaches the y servo on pin 6 to the servo object
x_servo.attach(9); // attaches the x servo on pin 9 to the servo object
pinMode (13, OUTPUT);
digitalWrite (13, HIGH); // switch on the laser
//Place the servos in the center at the beginning
y_servo.write(y_position);
x_servo.write(x_position);
}
void loop() {
movement_time = random(10,40);
random_delay = random(min_freeze, max_freeze);
x_new_position = random(min_x+minimal_movement, max_x-minimal_movement);
y_new_position = random(min_y+minimal_movement, max_y-minimal_movement);
if( (y_new_position > y_old_position) && (abs(y_new_position - y_old_position) < 5 )) {
y_new_position = y_new_position + minimal_movement;
} else if ( (y_new_position < y_old_position) && (abs(y_new_position - y_old_position) < 5 )) {
y_new_position = y_new_position - minimal_movement;
}
if( (x_new_position > x_old_position) && (abs(x_new_position - x_old_position) < 5 )) {
x_new_position = x_new_position + minimal_movement;
} else if ( (x_new_position < x_old_position) && (abs(x_new_position - x_old_position) < 5 )) {
x_new_position = x_new_position - minimal_movement;
}
x_speed = (x_new_position - x_old_position)/movement_time;
y_speed = (y_new_position - y_old_position)/movement_time;
for (pos = 0; pos < movement_time; pos += 1) {
x_position = x_position + x_speed;
y_position = y_position + y_speed;
x_servo.write(x_position);
y_servo.write(y_position);
delay(10);
}
x_old_position = x_new_position;
y_old_position = y_new_position;
delay(random_delay);
}
Nowhere in this sketch, the line "digitalWrite (13, LOW);" is contained, yet I am unable to keep the LED on solid without blinking when the Arduino is looping. I've other people do it, with this code, I don't get what I'm missing!
Windows 7 64bit, Arduino Uno board, Arduino 1.8.9 program
1 Answer 1
I tried your sketch on an Arduino Uno, with no servos connected, and the LED on pin 13 did not blink.
DataFiddler makes a good point in their comment. To be sure you are actually uploading the sketch, try uploading the Blink
sketch that comes with the IDE. Change the delays from 1000ms to 200ms then upload it. The difference in flash rates should confirm or deny the ability to successful upload sketches.
You don't say which servos you are using, so I'll assume they are SG90. I'm not sure if the data sheet for every SG90 servo is the same, but if you look at this one, SG90 Specifications, you'll see the "stall current" can be between 570mA and 730mA, each. If you are powering them from the Uno, it can't supply that much current. Another limiting factor is the computer's USB maximum current output, (if you are powering the Uno with a USB port). FWIW, my computer can only provide 500mA per USB.
I would suggest powering the servos with a separate power supply, then see what happens.
-
If you power your Arduino from the USB port the Arduino current-limits the draw from the USB to 500mA even if the computer's USB port is capable of more.Duncan C– Duncan C2019年03月01日 21:19:41 +00:00Commented Mar 1, 2019 at 21:19
-
That is exactly what I found out. I was like why is it not working like it does in the video, then I decided to plug it in the outlet to see how bad it was, and that is when I notice that it was performing just like the video. Thanks for your help.Skywalker2000– Skywalker20002019年03月02日 23:42:46 +00:00Commented Mar 2, 2019 at 23:42
{}
). (I edited your question and did it already.)