Pages: [1]   Go Down
Author Topic: Processing returning 0, but arduino works.  (Read 691 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i managed sucessfully to connected both arduino and processing.I was getting results on my graph , but i tried to organize stuff,and i change some suff on the sketchbook, now everytime i run my processing code i dont any result.
I have no idea how to fix, some insight on how it works would help.


Arduino serial monitor says like: 165, 167, 167,  etc....
Programming graph return : 0, 0, 0, 0, ........

http://gyazo.com/b0966c463337257d5c1b56dba57f6fcc Arduino serial monitor
http://gyazo.com/08264f05aed77ba21fc82b74707fb232 Processing result


ArduinoSketch Code
Code:
int sensorValue;

void setup()
{
  Serial.begin(9600);      // sets the serial port to 9600
}

void loop()
{
  sensorValue = analogRead(0);       // read analog input pin 0
  Serial.println(sensorValue, DEC);  // prints the value read
  delay(500);                        // wait 100ms for next reading
}
Processing Sketch Code

Code:
import processing.serial.*;
import cc.arduino.Arduino;


Arduino arduino;
PFont font;
int xPos=0;
boolean firstLoop = true;
String arduinoPort = "COM4";

void setup(){
  size(1000,300);
  smooth();
  String ports[];
  ports = Arduino.list();
  for(int i=0;i<ports.length;i++) {
      if (ports[i].equals(arduinoPort)) {
 arduino = new Arduino(this, ports[i]);
      }
  }
  if (arduino == null) {
      throw new RuntimeException("could not find Arduino port");
  }

  font = loadFont("MinimaSSK-96.vlw");
  textFont(font, 48);

  background(0);
}

void draw(){
    if (firstLoop) {
delay(1500);
firstLoop = false;
    }

    int value = arduino.analogRead(0);
    int yPos = (int)map(value, 0, 1023, height, 20);
    xPos++;

    if (xPos > width) {
fill(0);
noStroke();
rect(min(xPos+30, width-100), max(yPos-70, 0), 100, 45);
xPos = 0;
    }

    fill(30, 5);
    noStroke();
    rect(0, 0, width, height);

    fill(0);
    rect(xPos, 1, 160, height-2);
    rect(min(xPos+30, width-100), max(yPos-70, 0), 100, 45);

    plot(xPos, yPos, value);

    fill(255);
    text(""+value, min(xPos+30, width-100), max(yPos-30, 40));

    noFill();
    stroke(128);
    rect(0, 0, width-1, height-1);

}

void plot(int x, int y, int value) {
    
    fill(255, map(value, 0, 1023, 255, 0), 0);
    ellipse(x, y, 30, 30);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    int value = arduino.analogRead(0);
This statement implies that the Arduino is supposed to be running a Firmata sketch. It isn't, so it's no wonder that the result is incorrect.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks bro, i uploaded the StandardFirmata from the example and ran processing and now everything works smiley-grin
But where is my code for the sensor now, i coulndt get my head around this.
I have the code for the sensor the firmata one and one for the graph in processing.
If i upload the firmata code after my sensor code on arduino dont it get lost?







 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Didn't help but thanks for wasting my time too
Yes, it gets overwritten. But, the StandardFirmata code turns the Arduino into an idiot. That's what Processing is expecting. It sends a command to the Arduino, telling it to return the value of the analog or digital pin that Processing needs the value of. The Arduino responds in the correct way, after calling the same function your sketch calls.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, i don't get the quote but, definitely helped me.
Logged

Pages: [1]   Go Up
Jump to: