Show Posts
|
|
Pages: [1]
|
|
2
|
International / Italiano / problema con touch sensor e audio
|
on: February 25, 2011, 06:29:59 am
|
|
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!!!!
|
|
|
|
|
3
|
Using Arduino / Audio / arduino+processing+touch sensor
|
on: February 25, 2011, 06:18:01 am
|
|
Hi all. I'm making a prototype with two buttons, two leds and one touch sensor. The circuit should do this: at first when the button are released nothing happens; if I press (nothing happens again) and release them, the leds starts to fade in and out. If this last condition is satisfied and I press the touch sensor, a song has to be played. All works except for the touch sensor because the song is played even if I don't touch the sensor. The problem is inside the led code because if I turn off that tab, the sensor replies in the right way. It seems that when the leds are activated they send the input to the sensor and it is read by the processing that plays the sound. Here the code:
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 sensor 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(); } }
LEDs TAB
void led() {
switch (state) { case 0: if(digitalRead(BUTTON2)==HIGH && digitalRead(BUTTON1)==HIGH){ //initial state: buttons pressed, nothing happens state=1; } ledOn = false; break;
case 1:
if(digitalRead(BUTTON2)==LOW && digitalRead(BUTTON1)==LOW){ //the two buttons are released at the same time
//the timer indicates the fade time if (timer == false){ start = millis(); timer = true; }
//fading time 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; } } ledOn = true; break;
case 2: //at first when the buttons are released nothing happens state = 0; ledOn = false; break; } }
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) { Serial.print(1, BYTE); // send 1 to Processing tempo = millis(); } else { 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); } void draw() { if (1 < port.available()) { val = port.read(); println(val); }
if (val == 1) { val = 0; prova.play(); }
} void stop() { prova.close(); minim.stop(); super.stop(); }
ANY SUGGESTION????
|
|
|
|
|
4
|
Using Arduino / Programming Questions / TOUCH SENSOR, AUDIO AND LED
|
on: February 22, 2011, 12:56:20 pm
|
|
Hi all. I'm prototyping a project and I need your help. I have a touch sensor, 2 buttons, some leds and audio to play. When I press at the same time the two buttons, the leds start to fade in and out as I want and now I need when I touch the capacitive touch sensor a sound has to be played and the leds have to turn off. Firstly, I built touch sensor behaviour and leds behaviour separetly and they are ok but when I join them in one file, something happens: according to delay I have just the leds working or the sensor. I attached the code to let you know what I did. If you are able to find why it doesn't work please help me!!
|
|
|
|
|
6
|
Forum 2005-2010 (read only) / Interfacing / Re: Multiple Button Circuit
|
on: January 18, 2011, 10:45:45 am
|
|
In fact doing just the first one is not a problem, all works fine! The problem is when I have to add the second button on the circuit. I don't know how to wire the second button.
const int BUTTON = 7; // the number of the pushbutton pin const int LED = 9; // the number of the LED pin
int i = 0; int state = 0;
//A function to see if the button has been pressed, returns an int int CheckButton() { return(digitalRead(BUTTON) == HIGH); }
void setup() { pinMode(LED, OUTPUT); pinMode(BUTTON, INPUT); }
void loop() { //Check to see if the button has been pressed. if(digitalRead(BUTTON) == HIGH) { //While the digital read statement is true (high) continue the loop while(digitalRead(BUTTON) == HIGH) continue;
//state is true as state is 0 and if we do !state then it is 1 state = !state; } //If state is 1 which it will be as we wont make it 0. If we dont use state then we have to keep pressing the button if(state) { for (i = 0; i < 255; i++)
{ //If checkbutton is true it means the button has been pressed if(CheckButton()) { //Turn off the LED and break the loop analogWrite(LED, 0); break; }
//Continue fading analogWrite(LED, i); delay(10); } for (i = 255; i > 0; i--) { //If the checkbutton is true it means the button has been pressed if(CheckButton()) { //Turn off the LED and break the loop analogWrite(LED, 0); break; }
//Continue fading analogWrite(LED, i); delay(10); } }
}
that's my code and with it, by pressing the button once, the led starts to fade in and out, when I press the button again the led turns off. What I wanna do is add another button and try give to the led different input. I know maybe for you as expert is a stupind and nonsense thing but try to think as a complete ignorant as I am!!
|
|
|
|
|
10
|
Forum 2005-2010 (read only) / Interfacing / Re: How to wire two push buttons
|
on: December 16, 2010, 06:09:00 am
|
Hi guys, I'm doing my first experiments with Arduino and I'm trying to use two buttons. I wanna my LED turns on when I press two buttons at the same time. With one button it works but I don't know how to wire the second one. Could you help me sending the fritzing scheme  ???.
|
|
|
|
|