New experiments

A student’s idea prompted me to try this…..I grabbed a cheap little ultrasonic sensor (HC-SR04) and interfaced it with an Arduino, then wrote some code in Max to pull out the serial data and turn it into something musical…..

Here’s the Arduino code:

#define trigPin 12
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 800 || distance = 0){
    Serial.println(“Out of range”);
  else {
    Serial.println(” cm”);

The Max Patch:


And the hardware: