two buttons in the same place that act like a switch.

edited February 2016 in Arduino

I am trying to build a login interface for an arduino that has several sensors, I was trying to create a button that would allow me to record and log to file and when clicked the button would become hidden run the associated code then the STOP button would become visible. Once that is clicked the logging would stop, hide the button and bring the REC button to the front.

The code kind of works but I am getting a bunch of errors. Here is a copy of what I have so far.

`
import controlP5.*;
import java.util.*;


ControlP5 cp5;

Chart myChart1;
Chart myChart2;
Chart myChart3;
Chart myChart4;

Button b1;
Button b2;

Textlabel b1Lbl;
boolean logging = false;
String bevent;

void setup() {
  size(800, 600);
  cp5 = new ControlP5(this);
  
  List l = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h");
  /* add a ScrollableList, by default it behaves like a DropdownList */
  cp5.addScrollableList("SELECT COM PORT")
     .setPosition(10, 10)
     .setSize(200, 100)
     .setBarHeight(20)
     .setItemHeight(20)
     .addItems(l)
     .close()
     // .setType(ScrollableList.LIST) // currently supported DROPDOWN and LIST
     ;
  // create a new button with name 'REC'
  b1 = cp5.addButton("REC")
     .setValue(0)
     .setPosition(740,10)
     .setSize(50,20)
//     .setLabel("REC")
     .setVisible(true)
     ;
     
  b2 = cp5.addButton("STOP")
     .setValue(0)
     .setPosition(740,10)
     .setSize(50,20)
//     .setLabel("STOP")
     .setVisible(false)
     ;
     
  myChart1 = cp5.addChart("sensor1")
     .setPosition(10, 40)
     .setSize(385, 200)
     .setRange(-10, 120)
     .setView(Chart.LINE) // use Chart.LINE, Chart.PIE, Chart.AREA, Chart.BAR_CENTERED
     .setStrokeWeight(1.5)
     .setColorCaptionLabel(color(40))
     .setLabel("")
     ;

//  myChart1.addDataSet("sensin1");
//  myChart1.setData("sensein1", new float[100]);

  myChart2 = cp5.addChart("sensor2")
     .setPosition(405, 40)
     .setSize(385, 200)
     .setRange(-10, 120)
     .setView(Chart.LINE) // use Chart.LINE, Chart.PIE, Chart.AREA, Chart.BAR_CENTERED
     .setStrokeWeight(1.5)
     .setColorCaptionLabel(color(40))
     .setLabel("")
     ;

//  myChart2.addDataSet("sensin2");
//  myChart2.setData("sensin2", new float[100]);

  myChart3 = cp5.addChart("sensor3")
     .setPosition(10, 250)
     .setSize(385, 200)
     .setRange(-10, 120)
     .setView(Chart.LINE) // use Chart.LINE, Chart.PIE, Chart.AREA, Chart.BAR_CENTERED
     .setStrokeWeight(1.5)
     .setColorCaptionLabel(color(40))
     .setLabel("")
     ;

//  myChart3.addDataSet("sensin3");
//  myChart3.setData("sensein3", new float[100]);

  myChart4 = cp5.addChart("sensor4")
     .setPosition(405, 250)
     .setSize(385, 200)
     .setRange(-10, 120)
     .setView(Chart.LINE) // use Chart.LINE, Chart.PIE, Chart.AREA, Chart.BAR_CENTERED
     .setStrokeWeight(1.5)
     .setColorCaptionLabel(color(40))
     .setLabel("")
     ;

//  myChart4.addDataSet("sensin4");
//  myChart4.setData("sensin4", new float[100]);
}

void draw() {
  background(240);
}

public void REC(){
  b1.hide();
  b2.show();
  b2.bringToFront();
  logging = true;
  println("Logging Data to File");
}

public void STOP(){
  b2.hide();
  b1.show();
  b1.bringToFront();
  logging = false;
  println("Stop Logging Data");
}

void dropdown(int n) {
  /* request the selected item based on index n */
  println(n, cp5.get(ScrollableList.class, "dropdown").getItem(n));
  
  /* here an item is stored as a Map  with the following key-value pairs:
   * name, the given name of the item
   * text, the given text of the item by default the same as name
   * value, the given value of the item, can be changed by using .getItem(n).put("value", "abc"); a value here is of type Object therefore can be anything
   * color, the given color of the item, how to change, see below
   * view, a customizable view, is of type CDrawable 
   */
  
   CColor c = new CColor();
  c.setBackground(color(255,0,0));
  cp5.get(ScrollableList.class, "dropdown").getItem(n).put("color", c);
  
}

void keyPressed() {
  switch(key) {
    case('1'):
    /* make the ScrollableList behave like a ListBox */
    cp5.get(ScrollableList.class, "dropdown").setType(ControlP5.LIST);
    break;
    case('2'):
    /* make the ScrollableList behave like a DropdownList */
    cp5.get(ScrollableList.class, "dropdown").setType(ControlP5.DROPDOWN);
    break;
    case('3'):
    /*change content of the ScrollableList */
    List l = Arrays.asList("a-1", "b-1", "c-1", "d-1", "e-1", "f-1", "g-1", "h-1", "i-1", "j-1", "k-1");
    cp5.get(ScrollableList.class, "dropdown").setItems(l);
    break;
    case('4'):
    /* remove an item from the ScrollableList */
    cp5.get(ScrollableList.class, "dropdown").removeItem("k-1");
    break;
    case('5'):
    /* clear the ScrollableList */
    cp5.get(ScrollableList.class, "dropdown").clear();
    break;
  }
}`

The errors I am getting are as follows...

Feb 22, 2016 9:16:32 PM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller event, please check your code at REC
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at controlP5.ControlBroadcaster.invokeMethod(Unknown Source)
    at controlP5.ControlBroadcaster.callTarget(Unknown Source)
    at controlP5.ControlBroadcaster.broadcast(Unknown Source)
    at controlP5.Controller.broadcast(Unknown Source)
    at controlP5.Button.setValue(Unknown Source)
    at TemperatureGUI_001.setup(TemperatureGUI_001.java:67)
    at processing.core.PApplet.handleDraw(PApplet.java:2377)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
Caused by: java.lang.NullPointerException
    at TemperatureGUI_001.REC(TemperatureGUI_001.java:140)
    ... 13 more
Feb 22, 2016 9:16:32 PM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller event, please check your code at STOP
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at controlP5.ControlBroadcaster.invokeMethod(Unknown Source)
    at controlP5.ControlBroadcaster.callTarget(Unknown Source)
    at controlP5.ControlBroadcaster.broadcast(Unknown Source)
    at controlP5.Controller.broadcast(Unknown Source)
    at controlP5.Button.setValue(Unknown Source)
    at TemperatureGUI_001.setup(TemperatureGUI_001.java:75)
    at processing.core.PApplet.handleDraw(PApplet.java:2377)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
Caused by: java.lang.NullPointerException
    at TemperatureGUI_001.STOP(TemperatureGUI_001.java:148)
    ... 13 more

The unusual thing is that the code actually seems to work and the button acts the way I would have expected it but would rather not have the errors.

Answers

Sign In or Register to comment.