Another supplier of sensors.

Probably used for medical applications, but here is the link: Shopembla

A nice demonstration of sound propagation

An interactive java applet depicting the propagation of sound and its response to reflecting materials.

See it here: Link.

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!

Processing to Arduino Slider v1
Slider almost on the absolute left
Processing to Arduino Slider
Slider at the center
Processing to Arduino Slider
Slider slightly on the right
Please check the video as well: Processing to Arduino video

and forgive me for the bad quality

I am struggling on how to get an analog output from a serial message

I have spent all the afternoon trying but no luck at all…

Here is the code that I have written:

int test;
void setup()
{
Serial.begin(9600);
}

void loop(){
while (serialAvailable()){
test = serialRead(); //read Serial
serialWrite(test);
analogWrite(9,test);
}
}

For some reason the analogWrite function does not want to read the test variable value which is the one that I send through serial communication.

I have written an help request on the Arduino forum, let’s see how it goes.

Here is the link to the Arduino Forum Thread: Link

Let me cross my fingers…

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.

Computers can’t talk analog, so somebody invented PWM (Pulse width modulation)

Computers are not able to output analog signal, or at least they can’t while talking through a serial port and a serial communication. At the same time they cannot read analog signals too. This is the reason why modems exist and what they do is to transform analog signal to digital and viceversa.

How do they do this? Trough the use of Pulse-width modulation, which is a method to codify the information with the use of the duration of a signal impulse.
Take an Arduino Board, for example. The only signals that the board can receive are the “on” and “off” instruction for one of its pin. If you instruct the board to switch one pin “on”, that pin will output the maximum voltage permitted, which is 5 Volts approximately. If the instruction received is to switch the pin off, the output voltage would obviously be zero.

So how do you make the board output a value of 3.5 Volts?

If the computer switches the pin on and off with a certain velocity, and you read the output values with a tester, you will see that magically, as faster as the switching becomes, the output voltage will increase from zero to five Volts gradually! So we are in some way faking an analog voltage, and with the tester readings, I must say that it can get very accurate.

Below you will find a representation of what I have tried to explain:

Pulse width modulation image

Image from Wikipedia

The image shows source signals and the corrispective PWM signals. But to understand in an easier way, invert the grafic. With Arduino, you can decide how long the single signal should stay in an “on” status. The longer it stays “on”, the higher is the voltage read by the tester. This is a method to remedy at the problem of not having intermediate steps.

As I said before, you can control the Arduino Board by telling, time after time, how long should the impulse stay on the “on” status. Arduino has a built in function that does set the leght of time that a signal should be on, to fake the desidered voltage. This function is called “analogWrite()”. You can find more information at this address: Arduino analogWrite
Here is the code (to be loaded into the Arduino Board):

As we did before, we set up first which pin should output the desidered current. Be carefull as PWM outputs are only avalaible from pin 11 to 13 into the Arduino board.

int ledPin = 11; // TESTER connected to digital pin 11

We declare that we want the pin as an output

void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
}

We set the output current. The first value of the funcion is the output pin, the second is the output Voltage. A value of 255 will give you an output of 5 Volts approximately, while 0 will give you 0 Volts output.

void loop()
{analogWrite(ledPin, 255); // analogWrite values from 0 to 255
}

A supplier of piezoelectric pressure sensors

Unfortunately it is in America but here is the link: XactMan.

There is one which is particularly interesting and is the one on the picture below:

Piezo sensor

Image from XactiMan

It is quite cheap and it will probably be good for Arduino projects.

Very quick, the last post for today

A very interesting website, showing all, or almost all website and online applications that offer a developer API.
Here is the link: ProgrammableWeb. It would be nice to interface any of these with processing.

So finally i undestood why Arduino has its own software which is different from Processing.

With the Arduino platform, all you do is to write code that will be loaded into the Arduino board.
You tell to the board how to react to signals received.

So what kind of signals can the Arduino board receive?
Even if the Arduino board has a USB interface, to make the life of developers easy, the communications that the board makes, either in reception or transmission, are serail communications. This means that any language or any software that can transmit serial data, could be understood by the Arduino Board.

Considering these new discoveries, (it took me a while to understand) I will create two more categories to diversify code for the Arduino Interface and code for Processing and I will give a practical example on how two make Processing interact with the Arduino board.

So why should you use Processing instead of the Arduino software?
Even if the Arduino software can communicate in serial with the board, beside writing code that should be uploaded to the board itself, it lacks of all the libraries that Processing has, and interaction with the web for example could not be possible. Also, the Processing software can receive serial data sent by the Arduino board and can translate this information into graphical representation.

From now on, I will be carefull to indicate which one is the code for the Arduino software interface, and which is for Processing, and I will give you a practical example on how to make the two interact one with each other.

This example will guide you through the experience of switching on an LED on the Arduino Board using Processing. I know this is quite simple to achive, but it was difficoult for me to understand, so probably it will be of help to other peoples encountering my difficoultis as well.

So we will start with the Arduino Software first. We will write a code to be uploaded into the Arduino board that will put the serial port into listening and that will switch on the LED when it receives the character “A” capital from Processing.
You can find the original code at this address: Link, which will switch on and off an LED withing a time routine, but I have made my modifications so that it will only switch on when it receive the “A” character form Processing.

Arduino Code
/*Arduino Code
* Switch an LED on
* —————–
*
* turns on an LED when receiving an “A” character with any serial software
*
*Code modified by Francesco Saitta
*
* Code originally created 1 June 2005
* copyleft 2005 DojoDave
* http://arduino.berlios.de
*
* based on an orginal by H. Barragan for the Wiring i/o board
*/

We will assing now the pin number of the board that should be affected by the script and we declare the val variable that is the variable modified when the “A” character is received. We must declare this and assign to it a dummy value because otherwise the script will go on error. We also start the Serial port and we assign the BAUD rate. Here is the code, copy it into the Arduino software

int ledPin = 13; // LED connected to digital pin 13
int val = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
}

Now we start the loop. A loop os needed because it will put the board on a listening status, and unless it receives the “A” character, than it will do nothing.

void loop()

{

Here we assing the value read from the serial port to the variable called “var”

val = Serial.read();

and now we start the conditional. As you can easily read, if the value of the “val” variable is “A” than the board will send electricity to the pin declared in the start with the variable “ledPin”

if (val == ‘A’) {

digitalWrite(ledPin, HIGH); // sets the LED on
}

Here I have set another conditional. If the variable “val” equals a lowercase “A” than switch off the LED.

if (val == ‘a’) {

digitalWrite(ledPin, LOW); // sets the LED off
}

And finally we close the loop.

}

Now you are ready to upload it into your Arduino Board. Press the reset button on the board and then Click on the UPLOAD button into your Arduino Software interface. For more information on how to connect your computer to your board and how to upload code into it you may refer to this page on the Arduino website. Is time now to put an LED into your board. So put the longer LED leg into the pin number 13 (you can modify this by changing the ledPin variable in the script, altought I am not sure it will work) and the shorter one into the GND pin. Remember that LED have a polarity so be carefull about this.

So now we will go into the Processing code. Again for this the code was originally found at this address: Link. I have cut out a lot of the script to make it simpler but that is for reference for anyone who would like to go a bit further.

So here is the code. We import the Serial Library first and declare the Serial variable:

// Processing code
// Example by Tom Igoe

import processing.serial.*;

// The serial port:
Serial myPort;

We print out a list of the Serial Ports for debugging

// List all the available serial ports:
println(Serial.list());

and select the right one for us by changing the value on he Serial.list() variable inside the square brackets. Remember that arrays on the processing software start from zero so if your option is the second listed you will have to write 1 and not 2 as you would probably expect. My port set for communication with the Arduino Board is COM3 which comes listed after COM1 and COM2 in the list so I will write 2 inside the suqare brackets.

// Note from Tom Igoe
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[2], 9600);

Finally we send the capital A in ASCI format so is the number 65. You can find a conversion Table at this address:
Ascii table - Characters from 0 to 127
Ascii table - Characters from 128 to 256

// Send a capital A out the serial port
myPort.write(65);

If everything is ok, you should see you LED switch on! If you want to switch it off, you will have to change the code as follows
putting 97 ( the ASCII code for the lowercase “a” character) insted of 65.

// Send a capital A out the serial port
myPort.write(97);

I hope that this may be usefull to anyone, and I am ready to accept suggestion or mdification of the above article to make it more simple to understand or to correct any error committed.

PHILIPS, the simplicity event.

Philips has presented in London the simplicity event, a show where interoperable by humans technologies have been presented to the public. Many of the finished designs did start from concept and are now manufactured and sold to the public. Altough it is not properly phisycal computing I think that is interesting to mention some of the project presented at the show.

.
.
Artificial sun
.
.

Sunshine – a wall-mounted ‘artificial sun’ uses natural light rhythms to give an energy boost and help control your body clock.

.
.

wake up alarm

.

.

Light – a new, medically proven wake-up lamp, which emits light that gradually increases to the intensity you have selected, simulating the rising sun in your bedroom thus gently preparing your body to wake up. The light falls on your eyes and sends your brain a message to reduce the production of melatonin, the sleep-inducing hormone. Over 30 minutes, the natural light gradually increases to reach the optimal intensity to wake you up at the set time, in a pleasant manner that leaves you feeling energized and ready to wake up. The light intensity can also be adjusted to your own personal preference.

.

.

weighter
.
.

In Form – a set of motivational body measurement tools, linked to an information display for people to understand better their physical condition – weight, fat, hydration and body shape – through personal history records, and tailored advice (based on the integration of the data).

Pictures and info from: http://www.presslink.nl