Go Down

Topic: I cant get the code right on Processing, Help Please (Read 1 time) previous topic - next topic


Im trying to find away to add a new toggle to control another LED

This is the original website with the project description https://www.sparkfun.com/tutorials/152

This is the processing code

Code: [Select]
import oscP5.*;        //  Load OSC P5 library
import netP5.*;        //  Load net P5 library
import processing.serial.*;    //  Load serial library

Serial arduinoPort;        //  Set arduinoPort as serial connection
OscP5 oscP5;            //  Set oscP5 as OSC connection

int oneLED = 0;        //  redLED lets us know if the LED is on or off
int twoLED = 0;        //  redLED lets us know if the LED is on or off
int [] led = new int [2];    //  Array allows us to add more toggle buttons in TouchOSC

void setup() {
  size(100,100);        // Processing screen size
  noStroke();            //  We don't want an outline or Stroke on our graphics
    oscP5 = new OscP5(this,8000);  // Start oscP5, listening for incoming messages at port 8000
   arduinoPort = new Serial(this, Serial.list()[0], 9600);    // Set arduino to 9600 baud

void oscEvent(OscMessage theOscMessage) {   //  This runs whenever there is a new OSC message

    String addr = theOscMessage.addrPattern();  //  Creates a string out of the OSC message
    if(addr.indexOf("/1/toggle") !=-1){   // Filters out any toggle buttons
      int i = int((addr.charAt(9) )) - 0x30;   // returns the ASCII number so convert into a real number by subtracting 0x30
      led[i]  = int(theOscMessage.get(0).floatValue());     //  Puts button value into led[i]
    // Button values can be read by using led[0], led[1], led[2], etc.

void draw() {
background(50);        // Sets the background to a dark grey, can be 0-255

   if(led[1] == 0){        //  If led button 1 if off do....
    arduinoPort.write("r");    // Sends the character "r" to Arduino
    oneLED = 0;        // Sets redLED color to 0, can be 0-255
if(led[1] == 1){        // If led button 1 is ON do...
  arduinoPort.write("R");    // Send the character "R" to Arduino
  oneLED = 255;        // Sets redLED color to 255, can be 0-255
  fill(oneLED,0,0);            // Fill rectangle with redLED amount
   ellipse(50, 50, 50, 50);    // Created an ellipse at 50 pixels from the left...
                // 50 pixels from the top and a width of 50 and height of 50 pixels

The second LED is called twoLED

The Arduino sketch is ready to go im just stuck on this, any help please :)


You seem to be sending data to the arduino every time draw is called. It would be better to send it only when you have a change in state.
Also comparing for == 0 and then ==1 is unnecessary. Just replace the second test with an else.


I didnt write the code so its hard for me to change it, but thank you


I didnt write the code so its hard for me to change it

So why ask the question?


Because i was hoping for somebody more skilled than me in this subject can help me modify it, i dont even know where to start


Yes I told you where to start.

However I am not going to do it for you as that way you will learn nothing.

Set a Boolean variable call it changed, to true in the OSC function.
Then only output data to the arduino when it is true. After you output the data to the arduino then set it to false.

Go Up