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!