Go Down

Topic: Problems with Serial Communication and Processing (Read 350 times) previous topic - next topic

chrisd1891

Sep 30, 2010, 09:34 am Last Edit: Sep 30, 2010, 09:35 am by chrisd1891 Reason: 1
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...
Code: [Select]
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...
Code: [Select]
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!

Grumpy_Mike

#1
Sep 30, 2010, 10:52 am Last Edit: Sep 30, 2010, 10:56 am by Grumpy_Mike Reason: 1
You are doing the comparison in the arduino whether you need to or not.

While all this is not right it doesn't explain what you say you are seeing. However as you don't explain how you have wired up the LEDs I can't tell what the correct behavioured should be.

Try this at the arduino end:-
Code: [Select]
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);
    }
 if (val == 's') {
   digitalWrite(pin0, LOW);
   digitalWrite(pin1, HIGH);
   digitalWrite(pin2, LOW);
     }
  if (val == 'h') {
   digitalWrite(pin0, LOW);
   digitalWrite(pin1, LOW);
   digitalWrite(pin2, HIGH);
    }
 }
}

Go Up