Many thanks, I know that this won't be 100% correct, but is this better? I still need help with this patch, and the difference between Uno and Leonardo, so I'm trying to make it clearer in the hope of obtaining help with this issue.
int Direction1 = 2;
int Speed1 = 3;
int Direction2 = 4;
int Speed2 = 5; //Motor Speed Control
const int trigPin = 6;
const int echoPin = 7;
long timeFront;
int distanceFront; // Ultra Sensor
void setup() {
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(57600);
}
void loop() {
digitalWrite(trigPin, LOW); delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
timeFront = pulseIn(echoPin, HIGH, 2940);
distanceFront = timeFront*0.034/2; // NEED QUED TRIG > READ ?????
if (distanceFront < 20 and distanceFront > 0)
{Serial.println("CLOSE");
} //stop ROBOT Serial.print(distanceFront);
delay(50);
if (Serial.available() > 0) {
char val = Serial.read();
if (val == 'S')
{digitalWrite(Direction1,LOW);
digitalWrite(Speed1, 0);
digitalWrite(Direction2,LOW);
digitalWrite(Speed2, 0);}
if (val == 'M'){
int D1 = Serial.parseInt();
int S1 = Serial.parseInt();
int D2 = Serial.parseInt();
int S2 = Serial.parseInt();
if (Serial.read() == '\n') {
digitalWrite(Direction1, D1);
analogWrite(Speed1, S1);
digitalWrite(Direction2, D2);
analogWrite(Speed2, S2);
}
}
}
}