LED ON/OFF comandat la bătaie din palme „CLAPS”
Puteți să aprindeți/stingeți lumina din camera la o bătaie din palme
Avem nevoie de :
Placă de dezvoltare UNO R3 (sau oricare)
Modulul senzor de detectare a sunetului Ca LED îl vom folosi pe cel de la placa de dezvoltare : Led13 (sau oricare LED montat în "pin" cu o rezistentă în serie de 1k)) Codul : CLAPS //------------------------------- const int sound_pin = 2; const int led_pin = 13; const unsigned long min_time_distance_between_signals = 100UL; const unsigned long max_time_distance_between_signals = 500UL; const unsigned long delay_after_triggered = 2000UL; bool led_state; unsigned long last_time_since_clap; unsigned long last_action_time; void setup () { Serial.begin(9600); pinMode (sound_pin, INPUT) ; pinMode(led_pin, OUTPUT); digitalWrite(led_pin, LOW); led_state = false; last_action_time = 0; last_time_since_clap = 0; } void update_state(bool new_state, unsigned long current_time) { led_state = new_state; last_action_time = current_time; digitalWrite(led_pin, led_state ? HIGH : LOW); Serial.print("state = "); Serial.println(led_state); } void loop () { int sound_detected_state = digitalRead(sound_pin); unsigned long current_time = millis(); unsigned long delta_time_since_last_action = current_time - last_action_time; if ((delta_time_since_last_action > delay_after_triggered) && ( sound_detected_state == HIGH )) { if (!led_state) { unsigned long current_delta_time_between_signals = current_time - last_time_since_clap; if (current_delta_time_between_signals > max_time_distance_between_signals) { last_time_since_clap = current_time; } else if (current_delta_time_between_signals > min_time_distance_between_signals) { update_state(true, current_time); } } else { update_state(false, current_time); } } }
//-------------------------------
Succes !
Colectivul Roboromania