Pages: [1]   Go Down
Author Topic: Problems with Serial Communication and Processing  (Read 283 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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!
« Last Edit: September 30, 2010, 02:35:28 am by chrisd1891 » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 504
Posts: 31324
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
     }
  }
}
« Last Edit: September 30, 2010, 03:56:40 am by Grumpy_Mike » Logged

Pages: [1]   Go Up
Jump to: