Hello, I´m new to using Arduino and I want to use 4 ultrasonic sensors in a program called TouchDesigner. For TouchDesigner I have to code in python for it to recognize 4 different sensors, so in my Arduino code I have to name them A, B, C, D.
I think I have to do something with String and digitalRead. I tried to do this myself but it doesn't work and I cant figure out what I'm doing wrong.
For example:
String A = "A";
A += digitalRead();
Serial.println(A);
String B = "B";
B += digitalRead();
Serial.println(B);
String C = "C";
C += digitalRead();
Serial.println(C);
String D = "D";
D += digitalRead();
Serial.println(D);
The only thing I have to do is naming the sensors A, B, C and D. But I dont know how to do this in the arduino code I'm using right now
// trigger/echo pin pairs.
const byte rangers [][2] { {9, 10}, {5,4}, {12, 13}, {7, 6}};
void setup()
{
for (auto& ranger : rangers) {
pinMode(ranger [0], OUTPUT);
digitalWrite (ranger [0], LOW);
pinMode(ranger [1], INPUT);
}
Serial.begin(9600);
}
int range (const byte ranger [])
{
digitalWrite(ranger [0], HIGH);
delayMicroseconds (10);
digitalWrite(ranger [0], LOW);
unsigned long duration = pulseIn(ranger [1], HIGH);
return duration / 58.2;
}
void loop()
{
for (int i = 0; i < 4; i++) {
int distance = range (rangers [i]);
if(distance > 0 && distance < 50){
Serial.print(distance);
}
Serial.println ();
delay (50); // Ping no more frequently than 20 Hz
}
}
I will try to give a more clear discription of what I'm trying to achieve.
I have 4 ultrasonic sensors to measure the distance (each sensor separetely) between 0 and 50. In the Program TouchDesigner (for visual programming) I have four 3d objects moving along with the distances from the sensors
Object 1 = getting data from sensor A
Object 2 = getting data from sensor B
Object 3 = getting data from sensor C
Object 4 = getting data from sensor D
I'm still learning how arduino coding and TouchDesigner works, but I'm working on an art project for school. So bear with me while I'm trying to explain what I'm trying to do.
Someone from the TouchDesigner community sent me his code, what he was using himself for his project. I dont know exactly what his project was about, but he was explaining to name the sensors A, B, C and D in the Arduino code. and he sent me his phyton code for TouchDesigner.
I'm trying to achieve something like that, but with 4 ultrasonic sensors, measuring the distance between 0 and 50.
The arduino code he was using:
// trigger/echo pin pairs.
const byte rangers [][2] { {9,10}, {5,4}, {12, 13}, {7,6}};
void setup() {
//start serial connection
Serial.begin(9600);
//configure pins 2 as input and enable the internal pull-up resistor
for (int i = 2 ; i <= 13; i++){
pinMode(i, INPUT_PULLUP);
}
}
void loop() {
//read the pushbutton value into a variable
String sendVal = "v";
for (int i = 2 ; i <= 13; i++){
sendVal += digitalRead(i);
}
//print out the value of the pushbutton
Serial.println(sendVal);
//read analog into variable and print it
String A = "A";
A += digitalRead(9);
Serial.println(A);
String B = "B";
B += digitalRead(5);
Serial.println(B);
String C = "C";
C += digitalRead(12);
Serial.println(C);
String D = "D";
D += digitalRead(7);
Serial.println(D);
delay(10);
}
And the python code he was using in TouchDesigner
def onReceive(dat, rowIndex, message, bytes):
if message[0] == 'v':
op('table1').clear()
flow = list(message)
op('table1').appendCol(flow[1:])
elif message[0] == 'A':
value = message[1:]
op('table2')['A',1] = float(value)/1023
elif message[0] == 'B':
value = message[1:]
op('table2')['B',1] = float(value)/1023
elif message[0] == 'C':
value = message[1:]
op('table2')['C',1] = float(value)/1023
elif message[0] == 'D':
value = message[1:]
op('table2')['D',1] = float(value)/1023
elif message[0] == 'E':
value = message[1:]
op('table2')['E',1] = float(value)/1023
elif message[0] == 'F':
value = message[1:]
op('table2')['F',1] = float(value)/1023
return
I need to get the distances from the sensor A, B, C and D, measuring only between 0 and 50.
Then I need to use python in TouchDesigner for it to recognize the sensors separetely, that's why the sensors has to be A, B, C and D. if I understand correctly.