Go Down

Topic: Arduino + Processing for Graphing (Read 647 times) previous topic - next topic

sparkfuntoday

I tried the following:

http://www.arduino.cc/en/Tutorial/Graph

I put the arduino sketch in the same folder/directory as the processing sketch so when I verify the arduino sketch I get an error:

variable or field 'serialEvent' declared void.

So what am I doing wrong?

sparkfuntoday

I forgot to post the code:

Arduino:

Code: [Select]
void setup() {
  // initialize the serial communication:
  Serial.begin(115200);
}

void loop() {
  // send the value of analog input 0:
  Serial.println(analogRead(A0));
  // wait a bit for the analog-to-digital converter
  // to stabilize after the last reading:
  delay(2);
}


Processing code:

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()[2], 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++;
     }
   }
}


PaulS

Quote
So what am I doing wrong?

You answered your own question:
Quote
I put the arduino sketch in the same folder/directory as the processing sketch


Don't do that. Separate applications == separate folders.

sparkfuntoday

Thank you PaulS, that fixed my problem and it works. It was a little fast so I slowed it down by making it a 20 mS delay instead to the 2 mS delay they had - thanks again PaulS

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy