Hi, I’m having difficulties in reaching my goal, which is with the Arduino Esplora, when I point more light to the light sensor my waveform in the processing increases and when the sensor receives less this waveform disappears. However, with these codes I got, that doesn’t happen. What it does is when the light sensor receives more or less light, the volume of the music that is playing increases and decreases respectively. Can someone help me resolve this situation? Thanks
//Biclioteca
import processing.serial.*;
import processing.sound.*;
Serial myPort;
float lightAmp;
PGraphics pg;
//Variáveis de som do processing
SoundFile sample;
Waveform waveform;
Amplitude rms;
SoundFile file;
Sound s;
//Variável de suavização para suavizar mudanças repentinas de amplitude
float smoothingFactor = 0.25;
//Variável para armazenar o valor de amplitude suavizada
float sum;
//Amostras da forma de onda que irão ser lidas de uma vez
int samples = 100;
//Variáveis para as cores e para os circulos
float r;
float g;
float b;
float a;
float diam;
float x;
float y;
//Janela
void setup() {
size(2000, 2000);
myPort = new Serial (this, Serial.list()[0], 9600);
myPort.bufferUntil ( '\n' );
pg = createGraphics(2000, 2000);
pg.beginDraw();
pg.background(0);
pg.endDraw();
smooth();
//Local para a música
//sample = new SoundFile(this, "Happy Little Elves - Audionautix.wav");
//sample.loop();
//s = new Sound(this);
waveform = new Waveform(this, samples);
waveform.input(sample);
rms = new Amplitude(this);
rms.input(sample);
}
void serialEvent (Serial myPort) {
lightAmp = float (myPort.readStringUntil ( '\n' ) ) ;
}
void draw() {
image(pg, 0, 0);
pg.beginDraw();
//Cores aleatórias
r = random(255);
g = random(255);
b = random(255);
//Posição e dimensões dos círculos
a = random(255);
diam = random(15);
x = random(width);
y = random(height);
//Características dos círculos
pg.noStroke();
pg.fill(r,g,b,a);
pg.ellipse(x,y,diam,diam);
pg.endDraw();
strokeWeight(1);
//Suaviza as formas de onda
sum += (rms.analyze() - sum) * smoothingFactor;
float rms_scaled = map(lightAmp,0,1024,0,255);
waveform.analyze();
//Forma de onda
beginShape();
{
fill(r,g,b,a);
for(int i = 0; i < samples; i++){
vertex(
map(i, 1, samples, 0, width),
map(waveform.data[i]*(map(lightAmp, height, 0, 0, 1)), -1, 1, 0, height)
);
}
endShape();
}
//Rato controla a intensidade do som
float amplitude = map(lightAmp, 0, 1024, 7, 1000);
//Volume
s.volume(amplitude);
}
Arduino IDE
#include <Esplora.h>
void setup() {
Serial.begin(9600);
while (!Serial) {
}
}
// The loop routine runs over and over again forever:
void loop () {
// Read the input on analog pin 0:
int sensorValue = Esplora.readLightSensor(); // Esplora.readLightSensor() replaces analogRead()
// Print out the value you read:
Serial.println(sensorValue);
}