Pages: [1]   Go Down
Author Topic: How to specify analog pin number in Processing  (Read 522 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

I found this code on the internet:
Code:
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!
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: June 26, 2012, 02:21:11 am by dxw00d » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: