Hi! Im trying to make a small bot that turns around when ever it hits something.. I made it out of cardboard and the sensor that detects a collision is two pieces of aluminum foil that acts as a button. (With a 5kohm pulldown resister between the pin and ground)
The thing is run by two servos (one acts as the "motor" the other one is for steering)
When i power the bot through USB everything works fine, but when i try to power it by a 9v battery the arduino seems to reset when the bot goes from backwards direction to forwards.
the 9 V battery is attached to Vin & gnd
Here is my code:
#include <Servo.h>
int frontSensorPin = 12;
int motorServoPin = 3;
int turnServoPin = 5;
int initialized = 0;
Servo motorServo;
Servo turnServo;
void setup(){
turnServo.attach(turnServoPin);
pinMode(frontSensorPin, INPUT);
pinMode(13, OUTPUT);
randomSeed(analogRead(0));
turnServo.write(79); //go straight
}
void loop(){
if(initialized != 0){
motorServo.write(110); //Go forward
turnServo.write(79); //go straight
if(digitalRead(frontSensorPin) == HIGH){
//Turn in a random direction and go backwards if sensor is activated
if(random(10) > 5){
turnServo.write(60);
} else {
turnServo.write(100);
}
motorServo.write(0);
delay(3000); //Arduino seems to reset after this delay when powered by 9V
}
} else {
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
if(digitalRead(frontSensorPin) == HIGH){
initialized = 1;
motorServo.attach(motorServoPin);
}
}
}
The servos are attached according to the servo tutorials..
Is the servos drawing too much power, causing the arduino to reset, or what exactly is going on here?