Author Topic: Clapper project don't work  (Read 3401 times)

brunosso

  • Newbie
  • *
  • Posts: 3
Clapper project don't work
« on: March 27, 2015, 08:09:25 am »
Hi i have a digispark attint85 and i want to resize my Clapper project.
The code is this... i remove serial line

Code: [Select]
const int soglia = 550; // soglia: valore oltre al quale considerare l'impulso sonoro
float valoreSensore = 0;
double elenco[2]; // Sette battiti sono la sequenza che attiva il LED (0-2)
double lastSound; // Dopo alcuni secondi di silenzio, cancella la sequenza

void setup()
{
  pinMode(1, OUTPUT); // Dichiaro ledPin come OUTPUT
  pinMode(5, INPUT);
}

void loop()
{
  float valoreSensore = analogRead(0);
  if (valoreSensore >= soglia)
  {
    registraBattito(); // Registro in elenco il momento in cui viene avvertito il battito
    valutaSequenza(); // Controllo se la sequenza e' completa
    delay(100);  // Aspetto per non sovraccaricare il buffer della porta seriale
  }
  // Sequenza cominciata ma oltre 2 secondi di silenzio: resetto
  if ( elenco[0] && millis() - lastSound > 2000 ) {
    resetta();
  }
}

// Registro il momento in cui viene avvertito il colpo
void registraBattito()
{
  double time = millis();
  lastSound = millis();
  if ( elenco[0] == 0 ) {
    elenco[0] = time;
    return;
  }
  if ( elenco[1] == 0 ) {
    elenco[1] = time;
    return;
  }
}

void valutaSequenza()
{
  // Se c'e' il numero corretto di battiti registrati
  if ( elenco[1] > 0 )
  {
    // Verifica il "ritmo" della sequenza di battiTO
    if ( elenco[1] - elenco[0] >= 200 && elenco[1] - elenco[0] <= 450 ) // 250
      success();

    // Resetto l'elenco ogni volta che ha registrato 2 battiti
    resetta();
  }
}

void resetta()
{
  for ( int n = 0; n <= 1; n++ ) {
    elenco[n] = 0;
  }
}

// Sequenza corretta: il LED si accende
void success()
{
  if (digitalRead(1) == HIGH) {
    digitalWrite(1, LOW);
  }
  else {
    digitalWrite(1, HIGH);
  }
}

But i don't understand why it dont go!

I have a microphone from adafruit, http://www.adafruit.com/products/1063 , and a relay board.
If i do with arduino uno, the script work pretty well, if it program the digispark, none happen. Only the red led go on!

I have the analog value on microphone on the P5, and the signal to activate the relay on the P1.

Can you help me?

DeuxVis

  • Full Member
  • ***
  • Posts: 107
Re: Clapper project don't work
« Reply #1 on: March 30, 2015, 02:16:42 am »
I just took a minute to read your code and my question is probably stupid but... Why do you do an analogRead( 0 ) when your microphone is plugged into pin 5 ?

DeuxVis

  • Full Member
  • ***
  • Posts: 107
Re: Clapper project don't work
« Reply #2 on: March 30, 2015, 02:20:30 am »
Ooops sorry forgot about the mismatching when using analog. http://digistump.com/wiki/digispark/tutorials/pinguide corrected me.

exeng

  • Sr. Member
  • ****
  • Posts: 452
Re: Clapper project don't work
« Reply #3 on: March 30, 2015, 09:47:44 am »
Three questions?

1. How are you sourcing 5v to the mic?
2. Have you printed the values that you get on P5? (debug)
3. Have you tried a different analog input pin?

Steve

brunosso

  • Newbie
  • *
  • Posts: 3
Re: Clapper project don't work
« Reply #4 on: March 30, 2015, 11:12:23 am »
Three questions?

1. How are you sourcing 5v to the mic?
2. Have you printed the values that you get on P5? (debug)
3. Have you tried a different analog input pin?

Steve

Thank you man! I don't know why, but if i change the analog pin to P4, all works! Maybe  because the Pin 5 is at 3V?