Hi all, brand NOOB here!
I am first and foremost a musician, producer and music practitioner. I co-run a community music therapy service which supports vulnerable young people and people with complex needs.
I am studying for an MA in Bristol, UK at the mo which is focused on innovation in sound. My area of interest is around designing multi sensory experiences and 'adaptive music technology' for young people with complex needs.
For my current project I am attempting to use an Arduino and Max/MSP (for the audio side of things) to design a playable, hands-free musical instrument and controller. As I say, I am totally new to Arduino and C programming, and am pretty new to Max. I enrolled on a course to learn some of the basics, however, I would really appreciate some help as I am totally stuck!
So far, I have created a circuit using some code that a certified trainer dished out to us which enables me to connect a bunch of switches. I have a push button which controls the playback of audio, a variable resister for controlling the speed of the playback, a 'panic' button to stop all audio and a switch. What I would like to do is now connect an ultrasonic sensor (HC-SR04) to the circuit so that I can use that as a controller within the Max/MSP environment.
I have some seperate code for the ultrasonic sensor, so last night I attempted to merge the two sets of code and somehow succeeded after receiving a few errors. The trouble is that the ultrasonic sensor does not seem to register at all (nothing in the serial monitor), whereas before when I used the basic code specifically for the ultrasonic it worked fine, so I MUST be doing something wrong.
I am not a coder, and am very green. I just don't have the time to learn all of it before late December (which is when my assignment is due in!!). I would greatly appreciate it if somebody could take a look at the combined code and tell me what I am doing wrong
Here is the code.....
/*
SERIAL INPUT:
- set pin state (p) og set pin value (v)
- When setting a pin state you can write 0 (input),1 (output),2 (pwm)
- set pin number
- set value
- Example: [p 3 2] & [v 3 128] == Pin 3 is set to be pwm and addressed with 50/50 duty cycle (medium "speed")
(just send as char values from any program that can communicate with serial ports ex. Max/MSP)
- You´ll need "[" and "]" to start and end requests.
SERIAL OUTPUT:
- pin number
- value
- Example: 7 0 == Pin 7 is set to receive a digital input (a carriage return is send to end request.)
- The board will only output through serial if an Arduino port is set to input.
*/
#define echoPin 12 // The number of the digital pin connected to the echo input
#define trigPin 11 // The number of the digital pin connected to the trigger output
int maximumRange = 30; // The max distance observed from the sensor
int minimumRange = 0;
long duration, distance; //duration used to calculate distance
unsigned long sendtime;
int portregister[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
String sample = "";
String explodedSample[3];
void setup() {
sendtime = millis();
Serial.begin(57600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
while (Serial.available() > 0) {
char c = (char)Serial.read();
if (c == '[')
sample = "";
else if (c == ']') {
explodeString(sample);
if (explodedSample[0].equalsIgnoreCase("v")) {
setPinValue(convertStringToInt(explodedSample[1]), convertStringToInt(explodedSample[2]));
}
else if (explodedSample[0].equalsIgnoreCase("p")) {
setPinState(convertStringToInt(explodedSample[1]), convertStringToInt(explodedSample[2]));
}
}
else
sample += c;
}
if (millis() > sendtime + 5) {
readSendInput();
sendtime = millis();
}
}
void readSendInput() {
for (int i = 0; i < 18; i++) {
if (portregister[i] == 0) {
if (i < 12) {
Serial.print(i + 2);
Serial.print(" ");
Serial.println(digitalRead(i + 2));
}
else {
Serial.print(i + 2);
Serial.print(" ");
Serial.println(analogRead(i + 2));
}
}
}
}
void setPinState(int pin, int pinState) {
portregister[pin - 2] = pinState;
if (pinState == 2)
pinState = 1;
pinMode(pin, pinState);
}
void setPinValue(int pin, int val) {
int pinState = portregister[pin - 2];
if (pinState == 1) {
digitalWrite(pin, val);
}
else if (pinState == 2) {
analogWrite(pin, val);
}
}
void explodeString(String s) {
String tem = "";
int tempointer = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ') {
explodedSample[tempointer++] = tem;
tem = "";
}
else
tem += s[i];
}
explodedSample[2] = tem;
}
int convertStringToInt(String s) {
char test[s.length() + 1];
s.toCharArray(test, sizeof(test));
return atoi(test);
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // pulse off
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // pulse for 10 microseconds
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // echo pin listens / receives
delay(50); // good practice not to overload the serial port
}
Thank you so much
Benny