Go Down

Topic: Controling LED with Processing (Read 3353 times) previous topic - next topic

jdk199

When I put the values into the serial monitor everything works, it's when I try to send the values with the processing program that I have difficulties.

PaulS

#16
Nov 27, 2014, 02:02 am Last Edit: Nov 27, 2014, 02:03 am by PaulS
OK. Try this:

Arduino code:
Code: [Select]
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(115200);
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    if(inData[0] == 'A')
    {
      inData[0] = ' ';
      int brightness = atoi(inData);
      Serial.print("Brightness: ");
      Serial.println(brightness);
      analogWrite(13, brightness);
    }

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}


Processing code:
Code: [Select]
import processing.serial.*;

Serial myPort;            // the serial port

void setup()
{
  // list all the available serial ports:
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 115200);
  myPort.bufferUntil('\n');
  size(510, 510);    // sets the size of the applet window
}

void draw()
{
//  print("mouseX: ");
//  println(mouseX);
//  print("width: ");
//  println(width);
  int x = (mouseX * 255) / width;
//  print("x = ");
//  println(x);
 
  String toSend = "<A" + x + ">";
//  print("toSend = ");
//  println(toSend);

  myPort.write(toSend);
}


void serialEvent(Serial myPort)
{
  boolean validString = true;  // whether the string you got is valid
  String errorReason = "";     // a string that will tell what went wrong

  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');

  // make sure you have a valid string:

  if (myString != null)
  {
    // trim off the whitespace (linefeed, carriage return) characters:
    myString = trim(myString);
  }
  println(myString);
}


The Processing code displays a box. Move the mouse to the right in the window, and the LED gets brighter. Move it to the left and the LED gets dimmer.

Note that parseInt() is nowhere to be seen in the Arduino code.
The art of getting good answers lies in asking good questions.

jdk199

I tried what you put in and it didn't work for some reason.  The same thing that has been happening happened again, the light lit up and wouldn't change when I moved the mouse around.  I then changed everything to pin 12 and tried it, but it didn't even light up.  Like before, I moved the positive led into pin 13 and the led lit up even though pin 13 was not mentioned in the program.  I'm not sure why it's doing this, would you recommend trying to this with the Firmata library?

maxhere

#18
Nov 27, 2014, 05:14 am Last Edit: Nov 27, 2014, 05:15 am by maxhere
hi, i use the following code on Arduino to vary a RGB led,

Arduino Code
Code: [Select]
int RED = 10;
int BLUE = 9;
int GREEN = 11;
int i=0;
int colorData=0;
int greenData=0;
int prevGreenData=255;
int j=0;

void setup()
{
  // declare the serial comm at 57600 baud rate
  Serial.begin(57600);

  // output pins
  pinMode(GREEN, OUTPUT); // green
  pinMode(RED, OUTPUT); // red
  pinMode(BLUE, OUTPUT); // blue


  digitalWrite(RED, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(GREEN, HIGH);
}

void loop()
{
  // call the returned value from GetFromSerial() function
  switch(GetFromSerial())
  {
  case 'R':
    colorData = GetFromSerial();

    analogWrite(RED, colorData);

    break;
  case 'G':
    greenData=  GetFromSerial();

    analogWrite(GREEN,greenData);

    break;
  case 'B':
    analogWrite(BLUE, GetFromSerial());
    break;
  }

}

// read the serial port
int GetFromSerial()
{
  while (Serial.available()<=0) {
  }
  return Serial.read();
}





and this is what i send from Processing on serial
Mine is a common Annode RGB, you may probably send the actual color value.
Code: [Select]

void SendData(int r, int b, int g)
{
  myPort.write("R");
  myPort.write(255-r);
  myPort.write("G");
  myPort.write(255-g);
  myPort.write("B");
  myPort.write(255-b);
}

PaulS

Quote
would you recommend trying to this with the Firmata library?
Not in a million years.

The code I posted DID make my LED change brightness as I moved the mouse back an forth. Obviously, I tested it on my Mega before I posted it.

One question. Which version of Processing are you using? I don't know of anyone that has been happy with the 2.0 version of Processing. I'm using 1.5.1.
The art of getting good answers lies in asking good questions.

jdk199

I had been using 2.2.1, I changed to 1.5.1 and now everything is working.  Thank you very much for all the help.

Go Up