Hi everybody i have a small question . I tried to modify/hack a cheep RC chinese car .I wanted to make autonomous car which avoid objects and executes other small functions. The basic is that i have removed the chip RX_2B and i use the arduino board instead.I use 4 pins( 2 for the controlling of forward and reverse mouvement , and 2 for going right or left . I am using also a ping ultrasonic sensor for avoiding objects ( here i use 4 pins - one to +5 V , one to GND , one who called Trigger and one who called ECHO ). Also i use a combination of photo resistor and a pair of leds for make the leds light when its dark in the room ( here i use one pin for the led , because they are 2 in a row ). I also use 2 leds to light when i make reverse mouvement.(another 2 pins ) The main idea of this car is this : When i dont have an object in front of my car than , the car must just go ahead - using the forward command ( the code this must be executed when the distance is greater than 12.50 ) .And if there is an object which is closer to the car than 12.50 , the car must stop , go reverse at same time turning on the leds , than after a small period stops and after that going forward and making a right turn for another small period .That’s the basics . I am using 4 x 1.5 V , i have an limiting diod and a 2200 uF capacitator with the purpose tto make the voltage about 5.4 V , because of the operating voltage of arduino board . The main problem is that when i turn on the car without the connection cable (usb )on my board (i mean when i use just the batteries) , than instead of going streight ahead(because there is no object in front of the ping sensor ) the car goes reverse for a small period , go forward and after a small period , go right . Ang this cycle is repeated twice , before starting executing the main program ( or just going ahead ) .BUT when i use the usb cable connection to the pc + the 5.4 volts of the batteries, than there is not such problem, and everything is alright . I want to ask is this some problem with the processor , i mean am i supposed to say something on the first lines of the program , to indicate some adresses or there is another problem. Thanks . This is the code :
int forward = 11;
int reverse = 10;
int left = 9;
int right = 8;
#define ECHOPIN 3
#define TRIGPIN 2
int ledBL = 5; // back led 1
int ledBR = 6; // back led 2
int ledBlue1 = 7; // front led
int lightPin = A5; // photoresistor
void setup() {
Serial.begin(9600);
pinMode(forward, OUTPUT);
pinMode(reverse, OUTPUT);
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(ledBR, OUTPUT);
pinMode(ledBL, OUTPUT);
pinMode(ledBlue1, OUTPUT);
}
void loop() {
Serial.println(analogRead(lightPin));
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
// Compute distance
float distance = pulseIn(ECHOPIN, HIGH);
distance= distance/58;
Serial.println(distance);
delay(200);
if ( distance < 12.50) {
analogWrite(forward,0);
delay(1000);
digitalWrite(ledBR,HIGH);
digitalWrite(ledBL,HIGH);
analogWrite(reverse,100);
delay(1000);
analogWrite(reverse,0);
delay(1000);
digitalWrite(ledBR,LOW);
digitalWrite(ledBL,LOW);
analogWrite(forward,100);
digitalWrite(right,HIGH);
delay(1500);
digitalWrite(right,LOW);
delay(500);
}
if ( distance > 12.50) {
analogWrite(forward,100);
delay(1000);
}
if(analogRead(lightPin)<850){
digitalWrite(ledBlue1,HIGH);
}else{digitalWrite(ledBlue1,LOW);
}
}