I'm trying to create an Interface for Max/MSP where I use sensors to read data in and then use PWM outs to feed data out and back into the inputs.
I'm testing it by simply using one input, mapping it to an oscillator and at the same time dimming an LED. This isn't working though.
If anyone can help I'd be eternally grateful.
Cheers, Jan
Arduino:
/*
* Arduino2Max
* Send pin values from Arduino to MAX/MSP
*
* Arduino2Max.pde
* ------------
* This version: .5, November 29, 2010
* ------------
* Copyleft: use as you like
* by Daniel Jolliffe
* Based on a sketch and patch by Thomas Ouellet Fredericks tof.danslchamp.org
*
*/
//=========================Declare Global Variables====================================
int x = 0; // a place to hold pin values
int serialvalue; //a place to hold the serial.read values
const int ledPin = 9;
//==========================setup()==========================================
void setup()
{
Serial.begin(9600); // 115200 is the default Arduino Bluetooth speed
digitalWrite(13,HIGH); ///startup blink
delay(600);
digitalWrite(13,LOW);
pinMode(12,OUTPUT); //Declare all of the pins that you'll use as OUTPUTs here
pinMode(13,OUTPUT);
pinMode(9,OUTPUT);
}
//===========================loop()============================================
void loop()
{
byte brightness;
if (Serial.available() > 0){ // Check serial buffer for characters
serialvalue = Serial.read();
if (serialvalue == 'r') { // If an 'r' is received then read the pins (r = ascii code 114)
for (int pin= 0; pin<=5; pin++){ // Read and send analog pins 0-5
x = map(analogRead(pin), 1023, 0, 0, 1023);
sendValue (x);
}
for (int pin= 2; pin<=11; pin++){ // Read and send digital pins 2-11. Modify this so you're
x = digitalRead(pin); // only reading the pins that aren't used below for digital out
sendValue (x);
}
Serial.println(); // Send a carriage returnt to mark end of pin data.
delay (5); // add a delay to prevent crashing/overloading of the serial port
}//end of if (serialvalue == 'r')
/*------------------------Here we receive the numbers sent from the Max digitalOuts-------------------
The digital writes and reads here are meant only as examples. You can replace what is in these
and expand this whole section to deal with more incoming numbers. You can use any number from
0-255 except 114, since that is the ASCII code for 'r' and is used above
Be sure any pin you use here is declared in setup() as an OUTPUT and that you're not reading from
it just above this section.*/
if(serialvalue == 0){
digitalWrite(12, LOW);
}
else if(serialvalue == 1){
digitalWrite(13, HIGH);
}
else if(serialvalue == 2){
digitalWrite(12, LOW);
}
else if(serialvalue == 3){
digitalWrite(13, HIGH);
}
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
// set the brightness of the LED:
analogWrite(ledPin, brightness);
}
//--------------------------End of receiving numbers---------------------------------
}//end of if (Serial.available() > 0
}
void sendValue (int x){ // function to send the pin value followed by a "space".
Serial.print(x);
Serial.write(byte(32));
}
I don't really know, I just ripped that code out of the example that comes with Max2Arduino. I've barely used Arduino before. I have my sensor built and it is being read into Max just fine (cheers for the tip about inverting values though). I want to use Analog Write instead of the Digital Out stuff, to feed values back from the Max Patch back into the sensors.
I'm still stuck getting Analog Write to work. If I use the 'serial out' part of the patch in isolation it works with Arduino and I can use a Max slider to fade an LED. I believe the example tutorial is called Dimmer. I am essentially trying to integrate this into my project.
I have a sensor that feeds data in to Max from the Arduino this data then needs to be transmitted back to the Arduino in a feedback loop so I can modulate the voltage coming from the sensor (I will run PWM through a LPF to convert to analog voltage once I have it working properly). So I need to use Analog Write on PWM outs. But this isn't working. I have an LED attached to pin 9 then to ground and it is not dimming.
Can someone please help me with the code, I have no idea what I'm doing! :~