Arduino toy car with 2 sensors. small code issue
hello people! part of highschool science fair project running simple rc car arduino. using light sensor can display in terms of voltage light intensity , ultrasonic sensor. problem code doesn't work. now, tested code individually, each separate segment code works. car motor pwm works. light sensor works, sonar sensor works. however, when put them in single code, both sensor work because connected them led check, motor doesn't move. of please me because it's first time using arduino , don't know if code logic good, used samples , examples reference.
code: [select]
int echo=9; //sonar sensor
int trig=8; //sonar sensor
int led=7;
int enb=2; //enables motor
int forward=10; // + on side makes motor go forward
int back=11;
void setup()
{
pinmode(echo, input);
pinmode(led, output);
serial.begin (9600);
pinmode(trig, output);
pinmode(enb, output);
pinmode(forward, output);
pinmode(back, output);
}
void loop()
{
digitalwrite(enb, high);
digitalwrite(trig, low);
delaymicroseconds(2);
digitalwrite(trig, high);
delaymicroseconds(10);
digitalwrite(trig, low);
int sensorval=analogread(a0);
float voltage= sensorval*(5.0/1023.0);
int duration = pulsein(echo, high);
int distance = (duration/2) / 29.1;
if (distance <25 || voltage >3) // led on/off happens
{
analogwrite(forward, 0);
digitalwrite(led,high);
delaymicroseconds(1000);
digitalwrite(led,low);
}
else
{
analogwrite(forward, 200);
digitalwrite(back, low);
}
if (distance >= 200 || distance <= 0)
{
serial.println("out of range");
serial.print("color voltage =");
serial.print(voltage);
serial.println("v");
}
else
{
serial.print("distance ahead ");
serial.print(distance);
serial.println(" cm");
serial.print("color voltage =");
serial.print(voltage);
serial.println("v");
}
delay(1000);
}
don't cross post!.
mark
mark
Arduino Forum > Using Arduino > Programming Questions > Arduino toy car with 2 sensors. small code issue
arduino
Comments
Post a Comment