0

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
    Welcome to the Arduino SO site. You should select your code and tap the code formatting button (It looks like open and close curly braces ({}). (I edited your question and did it already.) – Duncan C Mar 1 at 19:47
  • 2
    You did not upload that code. That's still the initial blink sketch, coming together with the bootloader. My guess. :) – DataFiddler Mar 1 at 19:52
  • Or maybe your sketch causes a reset (not sure what happens during an exception). Change the initial code to see what happens (like a small pattern of the LED to play, than you also know that your sketch is loaded like DataFiddler assumes (which is even more likely than mine). – Michel Keijzers Mar 1 at 20:33
  • Use the serial monitor. Send a message from the setup function so you know that the setup function is executed. Then send a message from the loop. – Jot Mar 1 at 20:45
  • 1
    If you're running a motor you need to have a separate power supply for the motor. There's no way you can drive a servo motor from the Arduino 5V line. You are much better off using a completely different power supply for your servo motor (just connect the ground lines.) – Duncan C Mar 1 at 21:21
3

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 Mar 1 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 Mar 2 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.