sensor and motors

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 :slight_smile:
thanks for answers.
Regards: Peter

motorright ||motorleft;

Novel construct.
What do you think it does?

What do you think it does?

About the same as these:

    motorgo;
    motorgo;

That is, nothing.

Only post the code you are really using and do so in code tags.

Mark

PS which motor controller are you using.

M

AWOL:

motorright ||motorleft;

Novel construct.
What do you think it does?

Hi! I thought it will do motorright or motorleft, but it seems it wont :smiley: so i corrected it and there is just motorright

holmes4:
Only post the code you are really using and do so in code tags.

Mark

PS which motor controller are you using.

M

that is the full program, i wrote it in arduino 1.0.1

You need to call the functions.
To do this, follow the function name with parentheses.

motorgo motorleft etc should be called as motorgo(), motorleft() etc. I've no idea why your code builds let alone that it does if run!

change

  if (cm>=20){
    motorgo;
  }  
  else{
    motorright;
    delay(4000);
    motorgo;
  }

to

  if (cm>=20){
    motorgo();
  }  
  else{
    motorright();
    delay(4000);
    motorgo();
  }

Mark

Which motor shield are you using? You are using one are you not.

Mark

I've no idea why your code builds let alone that it does if run!

Why not? The code is syntactically valid even though it is logically wrong.

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:

long microsecondsToCentimeters(long microseconds)
{

  return microseconds / 29 / 2;
}

before void loop.

Please post all your code.

int motor1Pin = 5;
int motor2Pin = 6;
const int pingPin = 7;
void setup()
{
  Serial.begin(9600);
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
}
int motorleft()
{
  analogWrite(motor1Pin, 120);
  analogWrite(motor2Pin, 0);
}
int motorright()
{
  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(2000);
    motorgo();
  }

}

here you are :slight_smile:

So, what is the problem? The code does something. You expect it to do something. What are those two things?

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?

@PaulS what would you expect to get from func; instead of func(); address of func maybe?

Mark

i tried what you said, i can make the motors go/ stop:

int motor1Pin = 5;
int motor2Pin = 6;

void setup()
{
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
}

int motorgo(){
  analogWrite(motor2Pin, 120);
  analogWrite(motor1Pin, 120);

}
int motorstop(){
  analogWrite(motor2Pin, 0);
  analogWrite(motor1Pin, 0);


}
int motorleft(){
  analogWrite(motor2Pin, 0);
  analogWrite(motor1Pin, 120);

}
int motorright(){
  analogWrite(motor2Pin, 120);
  analogWrite(motor1Pin, 0);

}
void loop()
{
  motorgo();
  delay(4000);
  motorleft();
  delay(4000);
  motorright();
  delay(4000);
  motorstop();
  delay(10000);

}

so both motors are going for 4 sec then just the left ,then just right and both stops for 10 sec

petieee:
i tried what you said, i can make the motors go/ stop

That's a good start - now we know the motor side of things is OK.

Is your ping sensor and distance calculated from it giving you the distance values that you expect?

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

7;

which also compiles fine.

The web site for your motor shield does not (as far as I could see) have a link to a data sheet for the shield.

We need of know what each of the pins does (not what you think they do!).

Mark