Hi peeps,
As my first ever forum post I find myself begging for help. I'm not much of a coder and I somehow have talked myself into creating a project that takes sound data from pd and passes it over tcp ip to a processing sketch and then to an arduino. The idea is to influence LED colours through sound and pass the LED's through an old school video feedback loop in an immersion dome.
I'm using the osc external for pd and am getting data into processing fine with the following code:
import oscP5.;
import netP5.;
import processing.serial.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
int pitchInt;
Serial myPort;
void setup() {
oscP5 = new OscP5(this,9997);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 115200);
myRemoteLocation = new NetAddress("127.0.0.1",9997);
}
void draw(){
try {
myPort.write(pitchInt);
int ardCallback = myPort.read();
println(ardCallback);
} catch (Exception e) {
//do anything you want to handle the exception
println("Unable to parse int");
}
}
/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
pitchInt = theOscMessage.get(0).intValue();
}
I'm then trying to write the values over serial to my arduino using a premade shiftbrite sketch I've found online:
/* Ports and Pins
Direct port access is much faster than digitalWrite.
You must match the correct port and pin as shown in the table below.
Arduino Pin Port Pin
13 (SCK) PORTB 5
12 (MISO) PORTB 4
11 (MOSI) PORTB 3
10 (SS) PORTB 2
9 PORTB 1
8 PORTB 0
7 PORTD 7
6 PORTD 6
5 PORTD 5
4 PORTD 4
3 PORTD 3
2 PORTD 2
1 (TX) PORTD 1
0 (RX) PORTD 0
A5 (Analog) PORTC 5
A4 (Analog) PORTC 4
A3 (Analog) PORTC 3
A2 (Analog) PORTC 2
A1 (Analog) PORTC 1
A0 (Analog) PORTC 0
*/
// Defines for use with Arduino functions
#define clockpin 13 // CL
#define enablepin 10 // BL
#define latchpin 9 // XL
#define datapin 11 // SI
// Defines for direct port access
#define CLKPORT PORTB
#define ENAPORT PORTB
#define LATPORT PORTB
#define DATPORT PORTB
#define CLKPIN 5
#define ENAPIN 2
#define LATPIN 1
#define DATPIN 3
// Variables for communication
unsigned long SB_CommandPacket;
int SB_CommandMode;
int SB_BlueCommand;
int SB_RedCommand;
int SB_GreenCommand;
// Define number of ShiftBrite modules
#define NumLEDs 1
// Create LED value storage array
int LEDChannels[NumLEDs][3] = {0};
// Set pins to outputs and initial states
void setup() {
Serial.begin(115200);
pinMode(datapin, OUTPUT);
pinMode(latchpin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(clockpin, OUTPUT);
digitalWrite(latchpin, LOW);
digitalWrite(enablepin, LOW);
SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
}
// Load values into SPI register
void SB_SendPacket() {
if (SB_CommandMode == B01) {
SB_RedCommand = 127;
SB_GreenCommand = 110;
SB_BlueCommand = 110;
}
SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_GreenCommand;
while(!(SPSR & (1<<SPIF)));
}
// Latch values into PWM registers
void SB_Latch() {
delayMicroseconds(1);
LATPORT += (1 << LATPIN);
//ENAPORT += (1 << ENAPIN);
delayMicroseconds(1);
//ENAPORT &= ~(1 << ENAPIN);
LATPORT &= ~(1 << LATPIN);
}
// Send all array values to chain
void WriteLEDArray() {
SB_CommandMode = B00; // Write to PWM control registers
for (int i = 0; i<NumLEDs; i++) {
SB_RedCommand = LEDChannels*[0] & 1023;*
SB_GreenCommand = LEDChannels*[1] & 1023;
SB_BlueCommand = LEDChannels[2] & 1023;
SB_SendPacket();
_ }*_
* SB_Latch();*
* SB_CommandMode = B01; // Write to current control registers*
* for (int z = 0; z < NumLEDs; z++) SB_SendPacket(); *
* SB_Latch();*
}
// Fade between two colors at specified interval
void fadeall(int rate, int fromred, int fromgreen, int fromblue, int tored, int togreen, int toblue) {
for (int i = 0; i < 33; i++) {
* for (int j1 = 0; j1 < NumLEDs; j1++) {*
_ LEDChannels[j1][0] = (fromred * (32 - i) + tored * i)/32;
LEDChannels[j1][1] = (fromgreen * (32 - i) + togreen * i)/32;
LEDChannels[j1][2] = (fromblue * (32 - i) + toblue * i)/32;
* }*_
* WriteLEDArray();*
* delay(rate);*
* } *
}
void loop() {
while (Serial.available() > 0) {
* int inByte = Serial.read();*
* Serial.write(inByte);*
_ inByte = inByte 5;
if (inByte > 0){
fadeall(60,0,500,0,0,0,1023);
delay(inByte);
fadeall(60,0,0,1023,0,0,0);
delay(inByte);
}
else {*_
* }*
}
}
Due to the nature of this setup i'm finding it really hard to debug as the serial port is being used constantly. As my processing sketch is printing to the port I am having to process it on the arduino and then re-write it to the port and print it from processing :s
Can anyone identify any silly mistakes I am making?
Any help would be greatly appreciated
Nick