Problem solved (How to control a PWM Arduino output with a Processing Slider)
Thanks to Carlo, a very good friend of mine, I finally made it work. Using the slider that I coded (you can find it here)
I managed to control one of the PWM Arduino output, using a slider coded with Processing. With this code, for example, you could control the intensity of an LED or for istance the rotation velocity of a DC motor. In my case I will use it to control the RC of my flying UFO toy.
I will try to explain the code bit by bit starting with the one to send at the board:
We set up an integer variable an we assing a 0 value to it, so that the LED (or motor, or whatever) does not light up when the board start to run the code inside itself.
// Arduino code
int incomingByte = 0;
We start by calling the setup function, we initialize the serial port, and set up the communication speed rate:
void setup() {
Serial.begin(9600);
}
We put the board in listening for any serial transmission. To avoid overload of the serial port we insert a conditional, that will listen to the serial communication and continue with the action only if the serial communication is bigger than zero. We assign the read value from the serial port to the “val” variable:
void loop() {
if (Serial.available() > 0) {
val = Serial.read();
Than we print the value read to the board back to the serial port just to check that transmissions are ok. After this we send the value received from the serial port (the one sent by the slider) into the pin number 9 of the Arduino Board:
Serial.println(val, DEC);
analogWrite(9, val);
Than we clode the if conditional and we close the loop as well:
}
}
At this point we load the code into the Arduino Board and open up the Procesing interface. Again bit by bit I will try to explain what it does:
We need to import the serial library first as otherwise Processing will not be able to communicate with the Arduino board. Then we initialize the variables that we need to run the code:
rect_side is the size of the rectangle that will simulate the slider on our stage;
leftColor is the initial color assigned to the slider. To make it more clear the slider becomes lighter as we increase its value;
pointer is the final output value to be sent to the Arduino board;
myPort will be used by the Serial library to open a serial connection:
// Processing code
import processing.serial.*;
int rect_side = 30; //set the side size of the rectangle
float leftColor = 0.0;
int pointer = 0;
Serial myPort;
At this point we setup the stage that will accomodate the slider, set the color mode and open a serial connection with com port number 3, returned by the key number 2 of the array “Serial.list()”; we also setup the communications speed:
void setup()
{
size(285, 100);
colorMode(RGB, 1.0);
noStroke();
myPort = new Serial(this, Serial.list()[2], 9600);
}
Now we start the Processing loop, we set up the background color, we update the listener that returns the X coordinate of the mouse, and we fill the rectangle with the specified color.
void draw()
{
background(0.0);
update(mouseX);
fill(0.0, leftColor + 0.4, leftColor + 0.6);
At this point we put a listener for the status of the mouse. We want the rectangle to be dragged only when the mouse is pressed, and we want the X coordinate of the mouse to perfectly fit with the absolute middle of the rectangle. To do this we must divide the side of the rectangle when we assign starting coordinates to it. This is requested because Processing start to draw a rectangle from its left top corner and not from its center:
if (mousePressed) {
rect(mouseX-(rect_side/2), height/2-(rect_side/2), rect_side, rect_side);
pointer = mouseX-(rect_side/2);
}
Now we encounter a little problem. Because Processing reads the X coordinate of the mouse even if it goes outside the Processing stage, we must instruct Processing to force the mouseX value anytime that it pass over the limit of the stage. Again we need to take care of the width of the rectangle so that the X from the mouse cohincide with the one of the rectangle. We must write the same code for the “pointer” variable as well, that is the variable that stores the ultimate value to be sent to the board:
if(mouseX < rect_side/2) {
mouseX = (rect_side/2);} else if (mouseX > width - (rect_side/2)) {
mouseX = width -(rect_side/2);
}
if (pointer > 255) {
pointer = 255;
} else if (pointer < 0 ){
pointer = 0;
}
Now we declare another variable. The big mistake that was keeping me from achieving such a result was the method of sending values to the board. Before I did try to send them as integers and then as characters, but my good friend Carlo reminded me that the board is set up to read ASCII code, so in here I convert the value of the “pointer” variable which is an integer variable into a byte variable that stores ASCII values:
byte byte_pointer= byte(pointer);
In here I print the final value to the Processing console just to make sure that I am not sending strange characters to the board, and finally I send the final value to the serial port and obviously to the Arduino board:
println(pointer);
myPort.write(byte_pointer);
}
Finally I declare the function that updates the color of the rectangle:
void update(int x)
{
leftColor = +0.002 * x;
}
Is time for some picture now, and a horrible video too!



and forgive me for the bad quality
No Comments
No comments yet.
Sorry, the comment form is closed at this time.
