We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Serial analog read (Read 2772 times)
Serial analog read
May 18th, 2009, 12:50pm
 
I wrote this little program, and with the serial monitor in Arduino, it works. But to let it work in Processing, and continue read the analog value seems impossible. I searched allover the forum, and found no solution.
I think I am stupid not to get such simple program working in Processing.
Please help, I am a total beginner! (always 'programmed' in Liberty Basic)

Code:
int pin = 5; // analog pin
int in = 0; // in variabele

void setup()
{
Serial.begin(9600); // start serial communication
}

void loop()
{
 in = analogRead(pin);
 delay(1000);
Serial.print(in,DEC);
Serial.println(" milliVolt ");
}
Re: Serial analog read
Reply #1 - May 18th, 2009, 3:07pm
 
Since your message will be coming in as decimal (variable length) you will want to scan the serial input for a '\n'.

If you only want to print the result to the console then the example at
http://processing.org/reference/libraries/serial/Serial_readChar_.html

will do almost what you need. Change println() in draw() to print() and whatever comes in will be sent to the console.

If you want to use the number that the string represents, you will have to parse it as it comes in.

Hints: watch for '\n' character, it will be easier to parse without the " millivolt "

If you still have trouble, I have a working example. Wink
Re: Serial analog read
Reply #2 - May 19th, 2009, 11:39am
 
It works!!!
Thank you very much!
Now it also works in Liberty Basic, I just had to reset!
Re: Serial analog read
Reply #3 - May 19th, 2009, 12:47pm
 
Patric wrote on May 19th, 2009, 11:39am:
It works!!!
Thank you very much!
Now it also works in Liberty Basic, I just had to reset!


But I am very curious for your example!
Re: Serial analog read
Reply #4 - May 19th, 2009, 1:16pm
 
Kind of a slow oscilloscope
Code:

import processing.serial.*;

Serial port;
String buff = "";
int NEWLINE = 10;
int px = 0;
int threshold = 50;

int[] values = new int[512];
int val = 0;

void mouseReleased() {
px = mouseX;
}

void setup() {
size(512, 512);

textFont(createFont("Arial", 14),14);

println("Available serial ports:");
println(Serial.list());

port = new Serial(this, "COM5", 9600);
}

void draw() {
background(54);

// Graph the stored values
for (int i = 0; i < 511; i++) {
stroke(0,255,0);
line(i, height-1 - values[i], (i + 1), height-1 - values[i + 1]);
}

while (port.available() > 0)
serCap(port.read());

stroke(0,128,192);
fill(255);
line(px,0,px,height);
text(val,20,10);
}

void serCap(int serial) {

if (serial != NEWLINE) {// Keep reading characters until '\n' found
buff += char(serial);

} else {

if (buff.length() == 4) {//change this to get more channels
try {
// I used a fixed length hex string from the arduino
// Adding channels just means reading the next 4 bytes
val = unhex(buff.substring(0, 4));

} catch (Exception e) {}
}

// Clear the value of "buff" for next time
buff = "";

// Not optimized
// Shift over the existing values to make room for the new one.
for (int i = 0; i < 511; i++) {
values[i] = values[i + 1];
}

// Add the received value to the array.
values[511] = val;
}
}
Re: Serial analog read
Reply #5 - May 19th, 2009, 4:00pm
 
Thank you! I will try it.
Page Index Toggle Pages: 1