Hi,
I am doing a project that uses a PING))) sensor with a servo and 2 DC motor wheels. I reached a level where the motors and the servo do not work together. I read all articles and tried all solutions but nothing works also, the motors are on pins 4 to 7 and not 9 or 10 which means when the servo library disables pins 9 and 10 the motors should not be affected but still the motors aren't working even if i detach the servo.
Any suggestion or advice please.
Thank you
Any suggestion or advice please.
Yes, follow the advice in read this before posting a programming question and upload your code.
Only 2 of the pins 4 to 7 can output PWM but without your code we don't know what you are using them for.
// Servo Library
#include <Servo.h>
#define Left 0
#define Right 1
int Array[90];
Servo servo1;
void setup() {
Serial.begin(115200);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
servo1.attach(9);
}
void loop()
{
int increment = 0;
int Max = 0;
int position;
long cm;
for(position = 0; position < 180; position += 2)
{
servo1.write(position); // Move to next position
//Here the PING))) returns the distance called cm
Array[increment] = cm;
increment ++;
}
//Returns the Index of the Max value in the table
Max = maximum(Array, 90);
drive(Left, 1, 255);
drive(Right, 1, 255);
delay(15*Max); // Motors spin for Max/2 cm equivalent to 15Max ms
drive(Left, 0, 0);
drive(Right, 0, 0);
}
void drive(byte motor, byte dir, byte spd)
{
if (motor == Left){
digitalWrite(4, dir);
analogWrite(5, spd);
}
else if (motor == Right){
digitalWrite(7, dir);
analogWrite(6, spd);
}
}
Have you posted all your code ? The code posted fails to compile at
Max = maximum(Array, 90);
for me.
'maximum' was not declared in this scope
How are the motors and the servo powered ?