Max not receiving data from Arduino correctly
Hi,
I have an Arduino with two sonic sensors and four potentiometers on breadboards. They all work as expected in the Arduino console but not in Max. Three of the potentiometers work and the fourth one on A0 does not. The data from both the sonic sensors looks like it is displayed in the same number box (box 5 in the patch)
I've setup the potentiometers on their own and they all work as expected in Max. Below is the max patch and Arduino code.
Shout if I haven't given enough information about anything.
Any help is greatly appreciated
Thanks


#define echoPin 5
#define trigPin 6
#define echoPin01 7
#define trigPin01 8
int knobValue = 0;
int val [4]; // number of sensors
// defines variables
long duration; // variable for the duration of sound wave travel
int distance = 0; // variable for the distance measurement
long duration01; // variable for the duration of sound wave travel
int distance01 = 0; // variable for the distance measurement
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
pinMode(trigPin01, OUTPUT); // Sets the trigPin01 as an OUTPUT
pinMode(echoPin01, INPUT); // Sets the echoPin01 as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
}
void loop() {
digitalWrite(trigPin, LOW); // Clears the trigPin condition
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
digitalWrite(trigPin01, LOW); // Clears the trigPin condition
delayMicroseconds(2);
digitalWrite(trigPin01, HIGH);
delayMicroseconds(10); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin01, LOW);
duration01 = pulseIn(echoPin01, HIGH);
// Reads the echoPin, returns the sound wave travel time in microseconds
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
distance01 = duration01 * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
for(int i = 0; i < 4; i++) {
val[i] = analogRead(i);
Serial.print(val[i]);
Serial.print(" ");
}
Serial.println(distance);
Serial.println(distance01);
Serial.println();
delay(50); }
to receive list of 6 values, only LAST one should be sent using
println.
this is the mistake:

it is visible in arduino serial monitor.
You get 6th value on separate line and then one more line inserted.
for(int i = 0; i < 4; i++) { val[i] = analogRead(i); Serial.print(val[i]);Serial.print(" ");}
Serial.print(distance); Serial.print(" ");
Serial.println(distance01);
Thanks for the speedy reply. So should it look like this? The first two readings are coming from the first potentiometer and the other 3 are ignored. I changed the number of inputs to 6
for(int i = 0; i < 6; i++) { val[i] = analogRead(i); Serial.print(val[i]);Serial.print(" "); {
val[i] = analogRead(i);
Serial.print(val[i]);
Serial.print(" ");}
Serial.print(distance); Serial.print(" ");
Serial.println(distance01);
}
Serial.println();
Sorry, it looks like its printing the reading from every potentiometer on its own line twice and then the sonic sensor readings.

Why do you repeat stuff in the code ?
read 6 inputs, send them out with space inserted,
at the end print 2 sensors, last one using Println
Insert you sensors reading code into this:

Thanks @sourceaudio, I tried that but couldn't get it to work. This if fairly new to me as is coding in general and I'm slowly starting to understand.
I ended up removing the second sonic sensor and it works fine with the potentiometers and one sonic but will revisit when I have time to figure it out as there's a deadline I've to get it working for and one sensor will do as a workaround. Thanks again for your help
If you post your last code with 2 sensors it
should be easy to fix it .
Thank you for your help and apologies for the delay. Here is the most recent code that I had applied some of the suggestions to. I may have saved over a more recent one.
#define echoPin 5
#define trigPin 6
#define echoPin01 7
#define trigPin01 8
int distance = 0;
int distance01 = 0;
int val[6] = {0, 0, 0, 0, 0, 0,};
int knobValue = 0;
// defines variables
long duration; // variable for the duration of sound wave travel
long duration01; // variable for the duration of sound wave travel
void setup() { Serial.begin(9600);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
pinMode(trigPin01, OUTPUT); // Sets the trigPin01 as an OUTPUT
pinMode(echoPin01, INPUT); // Sets the echoPin01 as an INPUT
}
void loop() {
digitalWrite(trigPin, LOW); // Clears the trigPin condition
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
digitalWrite(trigPin01, LOW); // Clears the trigPin condition
delayMicroseconds(2);
digitalWrite(trigPin01, HIGH);
delayMicroseconds(10); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin01, LOW);
duration01 = pulseIn(echoPin01, HIGH);
// Reads the echoPin, returns the sound wave travel time in microseconds
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
distance01 = duration01 * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
for(int i = 0; i < 6; i++) { val[i] = analogRead(i);
Serial.print(val[i]);Serial.print(" "); {
val[i] = analogRead(i);
Serial.print(val[i]); Serial.print(" ");}
Serial.print(distance); Serial.print(" ");
Serial.println(distance01);
}
delay(50); }
do you not see this mistake ?????
WHY DO YOU REPEAT val[i] = analogRead(i)
in middle of printing result out ????

You also need to know that querying a distance sensor will lock
the loop in case there is no echo from sensor.
Loop will continue after 1000ms timeout and rest of serial mesages will get sent.
I mention this in case you disconnect one sensor, but keep both in the loop code active.
for that reason I would place analog reading at the loop beginning.
this would be complete code:
#define echoPin 5
#define trigPin 6
#define echoPin01 7
#define trigPin01 8
int distance = 0; int distance01 = 0;
long duration; long duration01;
int val[6] = {0, 0, 0, 0, 0, 0,};
void setup() { Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin01, OUTPUT);
pinMode(echoPin01, INPUT);
}
void loop() {
for(int i = 0; i < 6; i++) {val[i] = analogRead(i);
Serial.print(val[i]);Serial.print(" ");}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
Serial.print(distance); Serial.print(" ");
digitalWrite(trigPin01, LOW);
delayMicroseconds(2);
digitalWrite(trigPin01, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin01, LOW);
duration01 = pulseIn(echoPin01, HIGH);
distance = duration * 0.034 / 2;
distance01 = duration01 * 0.034 / 2;
Serial.println(distance01);
delay(50);
}
I see it now. I reduced the number of analog readings to 4 in your code and it works perfectly in Max.
Thanks you so much for your help, now I can control the synth I made like a theremin.