I am new to Arduino programming. I have a Arduino micro and I have connected it to two IR sensors and three servo's. It get's stuck sometimes. I have a 12v power supply (In order to avoid my Arduino from burning out I am using a step down voltage regulator) let me know if this is necessary. I am attaching the code. I am not sure if it's the power issue or something else. could some one please let me know how to fix this issue. I also intend to add one temperature sensor and fan to this connection.
CODE:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
int val = 10;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);// set pin as input
pinMode(10, INPUT);
myservo.attach(5);
myservo1.attach(8);
myservo2.attach(4);
myservo.write(25);
}
void loop() {
//Written for Robojax on Dec 28, 2017
int detect = digitalRead(2);// read obstacle status and store it into "detect"
int detect1 = digitalRead(10);
if (detect == LOW) {
Serial.println("Obastacle on the way");
myservo.write(90);
delay(2000);
myservo.write(20);
delay(500);
myservo1.write(150);
myservo2.write(150);
delay(300);
}
More information please. What "step-down voltage regulator"? What servos? How is everything connected? A schematic/circuit diagram is needed. What does "gets stuck" mean? Describe what actually happens.
I guess it's probably a power problem. Maybe you are trying to power the servos from the Arduino or connect them through a breadboard or your regulator can't provide enough current or....
Please read the post at the start of any forum , entitled "How to use this Forum".
OR http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Connect your servo's red wire to the step down regulator and feed the Arduino from a separate step down regulator. Servo's create a lot of power fluctuations that may "hang" your Arduino.