Go Down

Topic: How to specify analog pin number in Processing (Read 562 times) previous topic - next topic

claudiu_mirescu

Hello!

I found this code on the internet:
Code: [Select]
import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph

void setup () {
// set the window size:
size(400, 300);       

// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my  Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);

// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}

it is just the basic for making a graph in processing. Well, I want to take this to a next step and draw two graphs at the same time and I do not understand two things:
1. Where does it specify which analog pin to listen to ?
2. What is the variable which stores the input value (from 0 to 1023) read from the analog pin?
I am not especially interested in drawing the graphs, I want to use them in other graphic issues, but I am mainly interested in returning the values from the two pins.

Thank you very much!

dxw00d

#1
Jun 26, 2012, 09:19 am Last Edit: Jun 26, 2012, 09:21 am by dxw00d Reason: 1
Quote
1. Where does it specify which analog pin to listen to ?

It doesn't. It just receives a number, sent via serial, from the sketch running on the Arduino.

Quote
2. What is the variable which stores the input value (from 0 to 1023) read from the analog pin?

Code: [Select]
String inString = myPort.readStringUntil('\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);


The code reads into a String, from serial until a newline character arrives, then converts the String to a float. I'm not sure why it's a float, as it will only ever be an integral value.

claudiu_mirescu

And is there any way to collect data from two input pins simultaneousl in processing? And to specify which pins is all about.

claudiu_mirescu

To be more specific, I want to read data from two LM35 at the same time and, probably, graph it (inside temp and outside temp). My weather station burned out and I want to make one myself.

dxw00d

Quote
And is there any way to collect data from two input pins simultaneousl in processing? And to specify which pins is all about.


Yes. Define a protocol for sending the data from your Arduino sketch to a Processing sketch. For example, you could use '<Innn,Onnn>'. You would need to modify the Processing sketch to read the whole string, and split it up.

Go Up