Hi all,
I've just run into some issues with a sketch that I've been using to run a simple robotics project, and I'm using an Arduino to interface with max/msp.
It was all working fine from my computer using an Arduino Uno, but I'm now trying to get it to run using a Latte Panda which has an onboard Arduino Leonardo of sorts.
All I'm trying to control is two motors, and an ultrasonic sensor, it's a basic 'tank' robot that perceives depth so it doesn't crash. The motors still work fine, but for some reason the ultrasonic sensor part of the sketch is no longer working.
I'll post the sketch below. Any ideas as to what needs changing?
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);
}}}}