Pages

Saturday, January 1, 2011

Processing/Arduino Serial Communication (LED Dimmer)

http://www.arduino.cc/en/Tutorial/Dimmer

I made some modifications to the sample to play around with it.

I have a couple of problems.

1) I cannot figure out how to send a byte (numeric) through a tty emulator -- the emulator picks up the ascii values for each character.  It would be nice to be able to just send a numeric if possible.
2) You can only have one thing at a time connected to the tty port.  Makes sense, I just didn't think about it when I tried to use the Arduino serial monitor while my Processing application was attached.
3) The 64 bit Linux compatibility with processing serialrxtx (fixed in another post.)

Arduino CODE

/*
  Dimmer
 
 Demonstrates the sending data from the computer to the Arduino board,
 in this case to control the brightness of an LED.  The data is sent
 in individual bytes, each of which ranges from 0 to 255.  Arduino
 reads these bytes and uses them to set the brightness of the LED.
 
 The circuit:
 LED attached from digital pin 9 to ground.
 Serial connection to Processing, Max/MSP, or another serial application
 
 created 2006
 by David A. Mellis
 modified 14 Apr 2009
 by Tom Igoe and Scott Fitzgerald
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Dimmer
 
 */

const int ledPin = 9;      // the pin that the LED is attached to
const int  ledFailPin = 13;
int ctr = 0;

void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledFailPin, OUTPUT);
}

void loop() {
  byte brightness;

  // check if data has been sent from the computer:
  if (Serial.available()) {

    digitalWrite(ledFailPin, LOW);
    // read the most recent byte (which will be from 0 to 255):
    brightness = Serial.read();
   
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
   
   char s[4];
   snprintf(s, 4, "%d", brightness);

   Serial.print("Success ");
   Serial.print(s);
   Serial.println();
  }else{
      if(ctr++ % 20000 == 0){
           Serial.print("Fail");
   Serial.println();
    digitalWrite(ledFailPin, HIGH);
      }
  }
 
}

Processing CODE
 // Processing code for this example
 // Dimmer - sends bytes over a serial port
 // by David A. Mellis
 //This example code is in the public domain.
 
 import processing.serial.*;
 Serial port;
 int lastSent=-1;
 
 void setup() {
 size(256, 150);
 
 println("Available serial ports:");
 println(Serial.list());
 
 // Uses the first port in this list (number 0).  Change this to
 // select the port corresponding to your Arduino board.  The last
 // parameter (e.g. 9600) is the speed of the communication.  It
 // has to correspond to the value passed to Serial.begin() in your
 // Arduino sketch.
 port = new Serial(this, Serial.list()[0], 9600); 
 
 // If you know the name of the port used by the Arduino board, you
 // can specify it directly like this.
 //port = new Serial(this, "COM1", 9600);
 }
 
 void draw() {
 // draw a gradient from black to white
 for (int i = 0; i < 256; i++) {
 stroke(i);
 line(i, 0, i, 150);
 }
 
 // write the current X-position of the mouse to the serial port as
 // a single byte
 int tmpX = mouseX;
 if(tmpX != lastSent){
   port.write(tmpX);
   lastSent = tmpX;
 }
 
   if (port.available() > 0) {
    String inBuffer = port.readString();  
    if (inBuffer != null) {
      println(inBuffer);
    }
  }

 }

No comments:

Post a Comment