My Processing slider

To do a preliminary check on how to collect data from the Internet and control the Arduino board, I will first build a Processing slider that will simulate the differencies on the values read from the Internet. Once everything will work, instead of the Slider I will use real data.

So here I will explain how I coded a simple slider in Processing. Here we go with the code:

First of all we declare the variables that we will need for the slider.

int rect_side = 30; //set the side size of the rectangle
float leftColor = 0.0;//set the initial color of the rectangle
int pointer = 0;//With the pointer we indicate the output value.

Than we initialize the stage and set the color mode to fill the rectangle:

void setup()
{
size(285, 100);
colorMode(RGB, 1.0);
noStroke();
}

We draw the stage and set a fill color for the rectangle

void draw()
{
background(0.0);
update(mouseX);
fill(0.0, leftColor + 0.4, leftColor + 0.6);

Now we start the conditional. If the mouse is clicked, draw a rectangle (well, is a square by the way) and position it in the same x coordinate as the mouse. Because the rect function draws a rectangle and position it starting by the top left corner, I divided the side size of the rectangle to make cohincident the mouse with the middle of the rectangle. With this bit of code we also update the pointer variable, which is the output that we are trying to send to the board.

if (mousePressed) {
rect(mouseX-(rect_side/2), height/2-(rect_side/2), rect_side, rect_side);
pointer = mouseX-(rect_side/2);
}

Here, we set a minimum and a maximum read value for the x position of the mouse. What we write here is that if the mouse goes lower than a certain value or higher, the square will not go out of the Processing stage, and will not be albe to produce negative values or higher values than 255.

if(mouseX < rect_side/2) {
mouseX = (rect_side/2);

} else if (mouseX > width - (rect_side/2)) {
mouseX = width -(rect_side/2);
}

Here we print internally the output value that you can read on the Processing console, and finally we update the rectangle color.

println(pointer);
}

void update(int x)
{
leftColor = +0.002 * x;
}

This isn’t the cleanest of all the sliders around. There is even a class that can be used in Processing to build a graphical user slider (Link).But I wanted to make my own so here it is! Next step is to pass these values to the Arduino board and manage the intensity of the light of the LED.

No Comments

No comments yet.

Comments RSS

Sorry, the comment form is closed at this time.