Skip to content

Instantly share code, notes, and snippets.

@suadanwar
Created August 1, 2019 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suadanwar/cc65fd93fa67ee7f4b709f3da479fe92 to your computer and use it in GitHub Desktop.
Save suadanwar/cc65fd93fa67ee7f4b709f3da479fe92 to your computer and use it in GitHub Desktop.
This sample code is for Control Servo and Display Sensor's Reading with the GUI on Arduino's Tutorial. [Processing]
import processing.serial.*;
import controlP5.*;
Serial myPort;
ControlP5 cp5;
ControlGroup messageBox;
String data = "0";
void setup() {
size(500,430);
myPort = new Serial(this, "COM5", 9600); // Change this to your port
myPort.bufferUntil('\n');
cp5 = new ControlP5(this);
createMessageBox();
}
void draw() {
Textlabel distanceValue = ((Textlabel)cp5.getController("distanceValue"));
background(#20acd6);
// Update distance to UI
distanceValue.setValue(data);
}
void createMessageBox() {
PFont pfont = createFont("Arial",20,true); // use true/false for smooth/no-smooth
ControlFont font = new ControlFont(pfont,14);
// create a group to store the messageBox elements
messageBox = cp5.addGroup("messageBox",50,50,400);
messageBox.setBackgroundHeight(330);
messageBox.setBackgroundColor(color(0,200));
messageBox.hideBar();
Button cytron = cp5.addButton("buttonA");
cytron.setImages(loadImage("cytronSmall.png"), loadImage("cytronSmall.png"), loadImage("cytronSmall.png"));
cytron.setPosition(45,20);
cytron.moveTo(messageBox);
// add a TextLabel to the messageBox.
Textlabel label = cp5.addTextlabel("label","Drag the slider to control the position of the servo",45,160);
label.setFont(font);
label.moveTo(messageBox);
Slider slider = cp5.addSlider("slider");
slider.setLabel("");
slider.setSize(300,30);
slider.setPosition(50,180);
slider.setRange(0,180);
slider.setColorForeground(color(0,113,220));
slider.setColorBackground(color(255,255,255));
slider.showTickMarks(true);
slider.setNumberOfTickMarks(5);
slider.snapToTickMarks(false);
slider.setValue(90);
slider.moveTo(messageBox);
Textlabel distanceLabel = cp5.addTextlabel("distanceLabel","Distance:",270,240);
distanceLabel.setFont(font);
distanceLabel.moveTo(messageBox);
Textlabel distanceValue = cp5.addTextlabel("distanceValue","VALUE",270,270);
distanceValue.setFont(font);
distanceValue.moveTo(messageBox);
Textlabel unitLabel = cp5.addTextlabel("unitLabel","cm",300,270);
unitLabel.setFont(font);
unitLabel.moveTo(messageBox);
}
void serialEvent(Serial myPort)
{
data=myPort.readStringUntil('\n');
}
void slider(int slider)
{
myPort.write(slider);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment