CIP5 dropdown and Arduino

edited November 2013 in Arduino

Hello,

I've created a dropdown menu using the ControlIP5 library, alongside Arduino Firmata to control some rgb led strip. The dropdown menu are some functions like blink single led or all of the them. The communication between Processing and Arduino works fine, however there is some sort of "lag" when choosing what function to run. My guess is that the dropdown waits for the function and serial communication to execute first. Is there a way to just chose an option without waiting? Here is the code:

import processing.serial.*;
import cc.arduino.*;
import controlP5.*;

Arduino arduino;
ControlP5 cp5;
DropdownList d1;

int cnt = 0;
int redPin = 9;
int greenPin = 11;
int bluePin = 10;

void setup() {
  size(300, 300, P3D);
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);

  cp5 = new ControlP5(this);
  // create a DropdownList
  d1 = cp5.addDropdownList("led_controls_functions")
          //.setSize(100, 70)
          .setPosition(100, height/2);

  customize(d1); // customize the first list
}

void customize(DropdownList ddl) {
  // a convenience function to customize a DropdownList
  ddl.setBackgroundColor(color(0));
  ddl.setItemHeight(30);
  ddl.setBarHeight(15);
  ddl.captionLabel().set("Select LED Function");
  ddl.captionLabel().style().marginTop = 3;
  ddl.captionLabel().style().marginLeft = 3;
  ddl.valueLabel().style().marginTop = 3;

  /* Adds items to the dropdown list
  for (int i=0;i<40;i++) {
    ddl.addItem("item "+i, i);
  } */

  ddl.addItem("Red Blink", 0);
  ddl.addItem("Green Blink", 1);
  ddl.addItem("Blue Blink", 2);
  ddl.addItem("White Blink", 3);
  //ddl.scroll(0);
  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(255, 128));
}

void redBlink(){
  arduino.analogWrite(redPin, 255);
  delay(1000);
  arduino.analogWrite(redPin, 0);
  delay(1000);
}

void greenBlink(){
  arduino.analogWrite(greenPin, 255);
  delay(1000);
  arduino.analogWrite(greenPin, 0);
  delay(1000);
}

void blueBlink(){  
  arduino.analogWrite(bluePin, 255);
  delay(1000);
  arduino.analogWrite(bluePin, 0);
  delay(1000);
}

void whiteBlink(){
  arduino.analogWrite(redPin, 255);
  arduino.analogWrite(greenPin, 255);
  arduino.analogWrite(bluePin, 255);
  delay(1000);
  arduino.analogWrite(redPin, 0);
  arduino.analogWrite(greenPin, 0);
  arduino.analogWrite(bluePin, 0);
  delay(0);
}

void controlEvent(ControlEvent theEvent) {
  // DropdownList is of type ControlGroup.
  // A controlEvent will be triggered from inside the ControlGroup class.
  // therefore you need to check the originator of the Event with
  // if (theEvent.isGroup())
  // to avoid an error message thrown by controlP5.

  if (theEvent.isGroup()) {
    // check if the Event was triggered from a ControlGroup
    println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());

    int value = int(theEvent.getGroup().getValue());

    switch(value) {
      case(0):
      redBlink();
      break;
      case(1):
      greenBlink();
      break;
      case(2):
      blueBlink();
      break;
      case(3):
      whiteBlink();
      break;
    }
  }
  else if (theEvent.isController()) {
    println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
  }
}

void draw() {
  background(0);
}

Thanks

Sign In or Register to comment.