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!

But the other end is running at 9600

I'm unclear why you're bothering with Firmata

Thanks. I changed the rates but the issue stays the same (no recognition of the sensor changes by the Arduino).
Would there be a better way than Firmata to write the code?

I don't see that you're using Firmata at all, you're simply streaming readings to the serial port.

Hi,
This might help;

Tom... :smiley: :+1: :coffee: :australia:

1 Like

I'm using Firmata because before it worked well.

I never use Firmata, and I rarely use Processing, but I'm pretty sure Processing has example code to read a simple comma-separated stream of integers.

Thanks. Finally I did manage to work out how to get Processing to read the sketch with Firmata but I'm sure you're right. I now need it to answer to multiple Arduino pins and am having problems with that. Not sure if it's due to wrong coding or incorrect circuit wiring.

Hi,

Post a circuit diagram of how you have multiple Arduino pins connected.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

The "aim" of the idea is to power the separate LED bunches, each with its own battery all whilst having them connected each to a separate analog pin on the Arduino. Basically each of them being "independent" entities. I'm pretty sure that the current wiring isn't correct for all I have done is connected the two batteries with ground. I'm not sure if my idea is realistic. Ideally there would be 6 LED bunches (around 5 LEDs per bunch). If the individual battery idea doesn't work I'm then wondering wether there is a way to still connect them to separate pins and then maybe power them with one very strong battery. Thank you :slight_smile:

Hi,
You will need to draw your circuit, a Fritzy image is not very informative.
Please label the pins like C B and E.
Are you going to use PNP transistors?
Where are the base resistors to current limit the base current?
What are the LDRs for?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.