Ciao a tutti,
sto tentanto di maneggiare led bottoni e un touch sensor. Quello che il circuito dovrebbe fare è:
premo due bottoni contemporaneamente; al rilascio, due led vanno in fade in e out. Solo in questa condizione (quindi non se i led sono spenti), toccando il touch sensor una canzone deve essere riprodotta. Il problema è che all'accensione dei led sembra che il segnale che attivi il touch sensor venga inviato anche se io non lo sto toccando e quindi la canzone parte ugualmente. Le ho provate tutte ma non riesco a trovare l'errore. Ecco il codice:
ARDUINO
MAIN TAB
const int BUTTON2 = 7; // the number of the pushbutton pin
const int BUTTON1 = 2; // the number of the pushbutton pin
const int LED2 = 9; // the number of the LED pin
const int LED1 = 10; // the number of the LED pin
unsigned long start;
boolean timer = false;
unsigned long end_fadeIn;
unsigned long end_fadeOut;
int fadePeriod = 2000;
const int knockSensor = 5; // the piezo is connected to analog pin 5
const int THRESHOLD = 100; // threshold value to decide when the detected sound is a knock or not
int tempo = 0;
// these variables will change:
int sensorReading; // variable to store the value read from the sensor pin
int state;
int fade1 = 0;
int fade2 = 0;
int incr1 = 1;
int incr2 = 1;
boolean ledOn = false; //boolean to lets the sensor play the sound just if LEDs are turned on
void setup()
{
pinMode(LED2, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(BUTTON2, INPUT);
pinMode(BUTTON1, INPUT);
pinMode(knockSensor, INPUT); // declare the sensor as as INPUT
Serial.begin(9600); // use the serial port
}
void loop()
{
led();
if (ledOn) {
pressSensor();
}
}
LED TAB
void led() {
switch (state) {
case 0:
if(digitalRead(BUTTON2)==HIGH && digitalRead(BUTTON1)==HIGH){ //initial state: buttons released, nothing happens
state=1;
}
ledOn = false;
break;
case 1:
if(digitalRead(BUTTON2)==LOW && digitalRead(BUTTON1)==LOW){ //the two buttons are pressed at the same time
//QUI FAI PARTIRE UN TIMER CHE DETERMINA LA DURATA DEL FADING
if (timer == false){
start = millis();
timer = true;
}
//QUI GLI DICI QUANTO DURA IL FADE; SE VUOI TEMPI DIVERSI FAI fadeInPeriod e fadeOutPeriod e
//li metti come variabili int con valori diversi all'inizio, inizio inizio.
end_fadeIn = start + fadePeriod;
end_fadeOut = end_fadeIn + fadePeriod;
//Serial.println (start);
if (millis()<=end_fadeIn){
fade1 = fade2 = map(millis(), start, end_fadeIn, 0, 255);
analogWrite(LED1, fade1);
analogWrite(LED2, fade2);
}
if (millis()>=end_fadeIn && millis()<=end_fadeOut){
fade1 = fade2 = map(millis(), end_fadeIn, end_fadeOut, 255, 0);
analogWrite(LED1, fade1);
analogWrite(LED2, fade2);
}
if (millis()>end_fadeOut){
timer = false;
}
}/// tutto questo succede solo se i due pulsanti sono OFF
ledOn = true;
break;
case 2:
state = 0;
ledOn = false;
break;
}
}
TPUCH SENSOR TAB
void pressSensor() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
if ((millis() - tempo) > 100) {
// if the sensor reading is greater than the threshold:
if (sensorReading <= THRESHOLD)
{ // If the switch is not ON,
Serial.print(1, BYTE); // send 1 to Processing
tempo = millis();
}
else {
// toggle the status of the ledPin:
//Serial.print(0, BYTE); // send 0 to Processing
}
}
}
PROCESSING CODE
import ddf.minim.;
import ddf.minim.signals.;
import ddf.minim.analysis.;
import ddf.minim.effects.;
import processing.serial.*;
Serial port;
int val;
Minim minim;
AudioPlayer prova;
void setup()
{
minim = new Minim(this);
prova = minim.loadFile("01 - Fango.mp3", 2048);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
size(10, 10);
//prova.loop(1); il suono parte all'avvio
}
void draw()
{
if (1 < port.available()) {
val = port.read();
println(val);
}
if (val == 1) {
val = 0;
// prova.rewind();
// prova.loop(1);
prova.play();
}
}
void stop()
{
prova.close();
minim.stop();
super.stop();
}
Vi prego aiutatemi!!!!