Hi!
I have an arduino uno r3, which i connected with the pind))) sensor and two DC motors.
I wrote a program to control the motors, but after 2-3 sec it stops. What can be the problem?
Here is the program:
int motor1Pin = 5;
int motor2Pin = 6;
const int pingPin = 7;
void setup()
{
Serial.begin(9600);
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
}
int motorright()
{
analogWrite(motor1Pin, 120);
analogWrite(motor2Pin, 0);
}
int motorleft()
{
analogWrite(motor1Pin, 0);
analogWrite(motor2Pin, 120);
}
int motorgo()
{
analogWrite(motor1Pin, 120);
analogWrite(motor2Pin, 120);
}
int motorstop()
{
analogWrite(motor1Pin, 0);
analogWrite(motor2Pin, 0);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void loop()
{
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
if (cm>=20){
motorgo;
}
else{
motorright;
delay(4000);
motorgo;
}
}
If u could help me it would be great
thanks for answers.
Regards: Peter
Im using And-tech arduino motor shield(this one: http://zestawyuruchomieniowe.pl/en/13-ard-motor-shield.html); corrected it, and it doesnt matter to it if something is before sensor or not.
I just copied from the example the sensors code, but i have to put this:
I expect it to go when the sensor doesnt see anything closer than 20 cm and turn to left when something is before it, and all that this does is that the right motor always runs and the left stops for some time(what i write in delay).
Is your ping sensor and distance calculated from it giving you the distance values that you expect? Unless the distance measurement is working, nothing else is going to work?
Starting from a minimal sketch that does nothing but initialise and control the motors, can you make each motor go and stop?
what would you expect to get from func; instead of func(); address of func maybe?
Yes. The statement func; evaluates to a value (the address of the function). The value is not used in any way, but that is no different, or more or less useless, than