Processing to read Arduino

Hello!

I am trying to get Processing to read LDR sensor values from Arduino to create moving images but it doesn’t seem to work. Arduino reads the changes fine but Processing keeps showing 0’s and constant numbers. I am supposedly using Firmata and a while ago the codes seemed to run fine. Wondering what may be stopping Processing from reading the Arduino values.

Here is the Processing code:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino; //creates arduino object

ParticleSystem ps;

int sensor= 0;

int read;

int volume = 0;

void setup() {
  size(1000,500,P2D);
  //fullScreen();
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[2], 57600); //sets up arduino
    arduino.pinMode(sensor, Arduino.INPUT);//setup pins to be input (A0 =0?)
    
    ps = new ParticleSystem(new PVector(width/2,height/2));
}
    void draw() {
  
   background(0);
  read=arduino.analogRead(sensor);
  
   int analogValue =  arduino.analogRead(sensor);
  read = arduino.analogRead(sensor);
  
  ps.addParticle(read);
  ps.run(read);
  println (read);
  
  {
     read = constrain(read, 1 , 40);
  float volume = map(read, 0.5, 40, -25, 20);
  println(volume);
  //println("");
 // player.setGain(volume); 
  delay(100);
}

}
void stop()
{
 // player.close();
  //player2.close();
 // minim.stop();
  //minim2.stop();
  super.stop();
}
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles 

class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;

  ParticleSystem(PVector location) {
    origin = location.get();
    particles = new ArrayList<Particle>();
  }

  void addParticle(float sensorValue) {
    Particle p = new Particle(origin);
   // p.lifespan = sensorValue*1;// + 100;
    p.lifespan = 90; // + 100;
    particles.add(p);
  }

  void run(float sensorValue) {
    for (int i = particles.size()-4; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      //p.size = sensorValue;
      p.size = 350;
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}



// A simple Particle class

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  
  //float lifespan = 248;
  //float size = 143;
  
  float lifespan = 90;
  float size = 390;
  
  
  Particle(PVector l) {
    acceleration = new PVector(0,0);
    velocity = new PVector(random(-0.5,0.5),random(-0.5,0.5));
    location = l.get();
    //lifespan = read + 100;
  }

  void run() {
    update();
    display();
  }

  // Method to update location
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 0.1;
  }

  // Method to display
  void display() {
    //read=arduino.analogRead(sensor);
    //stroke(size + 50,lifespan);
    noStroke();
    fill(200,0,0,20);
    ellipse(location.x,location.y,size/1.5,size/1.5);
    
  }
  
  // Is the particle still useful?
  boolean isDead() {
    if (lifespan < 0.1) { //
      return true;
    } else {
      return false;
    }
  }
}

Here the Arduino code:

#include <Firmata.h>

int AnalogPin0 = A0; //Declare an integer variable, hooked up to analog pin 0

void setup() {
  Serial.begin(9600); //Begin Serial Communication with a baud rate of 9600
}

void loop() {
   //New variables are declared to store the readings of the respective pins
  int Value1 = analogRead(AnalogPin0);
  
  Serial.print(Value1, DEC); 
  Serial.print(",");
  
   delay(500); // For illustration purposes only. This will slow down your program if not removed 
}

If someone could have a glimpse over what could be going wrong I would very much appreciate it!

Hello,

You should know this.

You must connect to the correct COM port with Processing.

Your BAUD rates are not matched; they should be the same.

It is not good practice to have a delay() in draw().

Look up the reference for delay() in Processing.

I get something:

And later:

I did not scrutinize your code any further.

It is always a good practice to write separate simple working code to:

  • communicate between Arduino and Processing
  • control, animate or data visualize

Once you have each of these working then integrate the two.

:)

Thank you for the answer. I double checked on the COM port, the BAUD rates and on the delay however unfortunately the issue stays the same. Processing still doesn’t recognise the sensor changes from Arduino.

Hello,

If you are using Arduino Firmata these are the Processing examples:

image

I have not used Firmata in ages so can’t comment further on this or help with Firmata issues.

Update:

I tried a scaled down version of your Processing code:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino; //creates arduino object

int sensor= 0;
int read;

void setup() {
  size(1000,500,P2D);
  //fullScreen();
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[4], 57600); //sets up arduino
  arduino.pinMode(sensor, Arduino.INPUT);//setup pins to be input (A0 =0?)  
  }
    
void draw() 
  {
  background(0);
  read=arduino.analogRead(sensor);

  int analogValue =  arduino.analogRead(sensor);
  read = arduino.analogRead(sensor);
  
  println (read);
  }

On the Arduino side I used the StandardFirmata sketch which is in the Arduino examples and it worked:

I have 3.3V (not measured) on A0.

That seems about right referencing the data sheet:

:)

@Kanuta

you are welcome

you can train on this instructions

https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all

1 Like

Hello,

If you are using the Processing Serial library these are the Processing references:

There are numerous topics on the Internet on Arduino and Processing communication.

This is a simple working example:

Arduino
int i;

void setup() 
  {
  Serial.begin(9600);  //Begin Serial Communication with a baud rate of 9600
  }
void loop() 
  {  
  Serial.print(i);
  Serial.print('\n');
  i++;
  if (i>1023) i = 0;
  delay(100);
  }

Processing side:
https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html

Also you will need to trim() myString before converting it to an int or float.

References:

:)

1 Like

Thanks a lot! Your solution works on both, the simplified sketch and the extended one. And SimpleAnalogFirmata seems to be working well as well. However I still have a problem and that is how to integrate multiple Arduino pins to communicate to Processing. Not sure if it is due to wrong circuit wiring or Processing not capturing the multiple pin signals.

There are times when the circuit can be at fault.

Try a simple working circuit with know inputs.
I connected the 3.3V to A0 for a simple test.

Use a multimeter for troubleshooting hardware and to verify signal levels.

I commented on the use of delay() before…

Try a simple frameCount() timer like this:

You can also make a timer with millis(); there are many examples of this on the forum.

You can slow down frameRate() for testing but this will also slow the animation!

:)