Programming for Arduino Uno VS Latte Panda with Arduino Leonardo

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);
}}}}

I'll post the sketch below. Any ideas as to what needs changing?

Lots of stuff.

int Direction1 = 2; int Speed1 = 3; int Direction2 = 4; int Speed2 = 5;  //Motor Speed Control

It's ONE statement per line.

void setup() {pinMode(2, OUTPUT); pinMode(4, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);
  Serial.begin(57600);
}

If you can't be bothered writing readable code, I can't be bothered reading that crap.

Could be some timer conflict, leonardo vs UNO, try different pins

P.S.
one statement per line is only cosmetic rule, does not affect functionality at all
don't get annoyed by such crappy answers

@PaulS I didn't mean to offend you with the coding. To be honest I'm a complete Newbie, and someone wrote this code for me in order to help me learn further.

It's not that I can't be bothered, I just don't know what is considered 'readable code' and what isn't. If you'd like to tell me what is I'd be happy to learn that too. To be honest I don't find your statements that helpful or encouraging.

@a-by thanks for that feedback, and your honest 'PS' too :slight_smile: I'll try using different pins and see if that helps, many thanks!!!

Although I can't help with the Latte Panda, I can show you an easily readable snippet when the variable is ONLY 1 character via use of "switch case":

void loop() {
  if (Serial.available() > 0) {
      char val = Serial.read();
      switch (val){
        case 'S':
          digitalWrite(Direction1,LOW); digitalWrite(Speed1, 0); 
          digitalWrite(Direction2,LOW); digitalWrite(Speed2, 0);
          break;

        case 'M':
          int D1 = Serial.parseInt(); 
          int S1 = Serial.parseInt(); 
          int D2 = Serial.parseInt();  
          int S2 = Serial.parseInt();
          break;
      }
  }
}

It's just PaulS' way of saying "learn to write readable code; you will benefit from it later on".

He has his ways of pointing out things and some people get offended by it (not you as far as I can see). Accept the way he answers or ignore his posts.

@JMeller: Thanks. So it's just a formatting thing, but I can see how that is easier to understand. And formatting doesn't affect it's function then?

@strerretje: I choose not to get involved with emotions here, I'm just interested in the sharing of information. After all, how do I know what's messy or not when I'm just a Newb?

Correct - formatting does not affect the function; especially helpful when the closing curly bracket is directly below the conditional statement's initiation. Closing the function with "}}}}" makes it difficult to properly place statements later.

Statements per line is a personal preference. One statement per line is recommended; however, I have placed two or three small statements per line if there is distinct and readable pattern.

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);
}
}
}
}

Yes- much easier to read. Now it is time to begin explaining your expected results versus what is actually happening.

What I see thus far is an accidental combining of two different coding languages:
if (distanceFront < 20 and distanceFront > 0)

vs

if (distanceFront < 20 && distanceFront > 0)

edit: I didn't realize alternates could be implemented successfully; when I accidentally crossed languages, I also had other issues to correct before I received expected results.