Hey! I just got an Arduino, and I'm trying to do some serial sort of communication through Processing.
Here's what I have placed on my Arduino...
char val;
int pin0 = 51;
int pin1 = 52;
int pin2 = 53;
void setup() {
pinMode(pin0, OUTPUT);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
val = Serial.read();
}
if (val == 'k') {
digitalWrite(pin0, HIGH);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
} else if (val == 's') {
digitalWrite(pin0, LOW);
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
} else if (val == 'h') {
digitalWrite(pin0, LOW);
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
}
}
And Here's what I have in Processing...
import processing.serial.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
//Sound ---------------
Minim minim;
AudioPlayer song;
BeatDetect beat;
BeatListener beatListen;
float kickSize, snareSize, hatSize;
//Arduino ------------
Arduino ard;
Serial myPort;
int pin = 3;
void setup() {
size(500, 200);
smooth();
//Song
minim = new Minim(this);
song = minim.loadFile("super.mp3", 2048);
//song.play();
beat = new BeatDetect(song.bufferSize(), song.sampleRate());
beat.setSensitivity(300);
kickSize = snareSize = hatSize = 16;
beatListen = new BeatListener(beat, song);
//Arduino
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(0);
fill (255);
/**
if (beat.isKick()) {
kickSize = 32;
snareSize = hatSize = 16;
myPort.write('K');
} else if (beat.isSnare()) {
snareSize = 32;
kickSize = hatSize = 16;
myPort.write('S');
} else if (beat.isHat()) {
hatSize = 32;
kickSize = snareSize = 16;
myPort.write('H');
}
**/
if (key == 'k') {
myPort.write('k');
}else if (key == 's') {
myPort.write('s');
}else if (key == 'h') {
myPort.write('h');
}
textSize(kickSize);
text("KICK", 100, 100);
textSize(snareSize);
text("SNARE", 200, 100);
textSize(hatSize);
text("HAT", 300, 100);
}
void stop() {
song.close();
minim.stop();
super.stop();
}
class BeatListener implements AudioListener {
private BeatDetect beat;
private AudioPlayer source;
BeatListener(BeatDetect b, AudioPlayer src) {
this.source = src;
this.beat = b;
this.source.addListener(this);
}
void samples(float[] samp) {
beat.detect(source.mix);
}
void samples(float[] sampL, float[] sampR) {
beat.detect(source.mix);
}
}
Basically, my program will flash some LEDs to some music that's playing, but for now I have disabled the playing and commented out the if expr that will send different values, and I'm trying to test it sending values over serial with my keyboard. Anyway, when I send a 'k', two of my LEDs turn on, with an 'h', all of them turn off, and with an 's' two others turn on...which isn't supposed to happen, it should just be one LED per key....am I missing something basic about serial?
Thanks!