Arduino Sound Sensor: Difference between revisions
From wikiluntti
(→Code 2) |
|||
(One intermediate revision by the same user not shown) | |||
Line 81: | Line 81: | ||
# Add delay when sound is noticed (not two LEDs turning on at the same time). | # Add delay when sound is noticed (not two LEDs turning on at the same time). | ||
# turn of the last led after a while. | # turn of the last led after a while. | ||
== Code 3: test == | |||
<syntaxhighlight lang="C"> | |||
#include <FastLED.h> | |||
// Arduino's pin connected to OUT pin of the sound sensor | |||
#define SENSOR_PIN 7 | |||
#define NUM_LEDS 10 | |||
#define LED_PIN 5 | |||
int soundState = HIGH; // the previous state from the input pin | |||
int numLeds = 0; | |||
CRGB leds[NUM_LEDS]; | |||
unsigned long turnOffStart; | |||
void setup() { | |||
Serial.begin(9600); | |||
pinMode(SENSOR_PIN, INPUT); | |||
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); | |||
FastLED.setBrightness(20); | |||
for (int i=0; i<NUM_LEDS; i++){ | |||
leds[i] = CRGB::Black; | |||
FastLED.show(); | |||
} | |||
turnOffStart = millis(); | |||
} | |||
void loop() { | |||
soundState = digitalRead(SENSOR_PIN); | |||
if (soundState){ | |||
numLeds++; | |||
turnOffStart = millis(); | |||
delay(50); | |||
} | |||
if (millis() - turnOffStart > 200){ | |||
turnOffStart = millis(); | |||
numLeds = max( 0, numLeds--); | |||
} | |||
for (int i = min( numLeds, NUM_LEDS); i<NUM_LEDS; i++){ | |||
leds[i] = CRGB::Black; | |||
FastLED.show(); | |||
} | |||
for (int i=0; i<min( numLeds, NUM_LEDS); i++){ | |||
leds[i] = CRGB::Green; | |||
FastLED.show(); | |||
} | |||
} | |||
</syntaxhighlight> |
Latest revision as of 17:47, 18 November 2024
Introduction
Simple sound sensor; output True or False.
Code 1. The simple
Tutorial page: https://arduinogetstarted.com/tutorials/arduino-sound-sensor
#define SENSOR_PIN 7
int lastState = HIGH; // the previous state from the input pin
int currentState; // the current reading from the input pin
void setup() {
Serial.begin(9600);
pinMode(SENSOR_PIN, INPUT);
}
void loop() {
currentState = digitalRead(SENSOR_PIN);
if (currentState){
Serial.println( currentState );
}else{
Serial.print("Schii");
}
}
Exercises
Code 1
Turn on the one more LED if the sound is strong enough.
#include <FastLED.h>
#define SENSOR_PIN 7
#define NUM_LEDS 10
#define LED_PIN 5
int soundState = HIGH; // the previous state from the input pin
int numLeds = 0;
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
pinMode(SENSOR_PIN, INPUT);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(20);
}
void loop() {
soundState = digitalRead(SENSOR_PIN);
if (soundState){
numLeds++;
}
for (int i=0; i<min( numLeds, NUM_LEDS); i++){
leds[i] = CRGB::Green;
FastLED.show();
}
if (soundState){
Serial.println("");
Serial.println( soundState );
}else{
Serial.print("Schii");
}
}
Code 2
- Turn all the LEDS to black in the SetUp()
- Add delay when sound is noticed (not two LEDs turning on at the same time).
- turn of the last led after a while.
Code 3: test
#include <FastLED.h>
// Arduino's pin connected to OUT pin of the sound sensor
#define SENSOR_PIN 7
#define NUM_LEDS 10
#define LED_PIN 5
int soundState = HIGH; // the previous state from the input pin
int numLeds = 0;
CRGB leds[NUM_LEDS];
unsigned long turnOffStart;
void setup() {
Serial.begin(9600);
pinMode(SENSOR_PIN, INPUT);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(20);
for (int i=0; i<NUM_LEDS; i++){
leds[i] = CRGB::Black;
FastLED.show();
}
turnOffStart = millis();
}
void loop() {
soundState = digitalRead(SENSOR_PIN);
if (soundState){
numLeds++;
turnOffStart = millis();
delay(50);
}
if (millis() - turnOffStart > 200){
turnOffStart = millis();
numLeds = max( 0, numLeds--);
}
for (int i = min( numLeds, NUM_LEDS); i<NUM_LEDS; i++){
leds[i] = CRGB::Black;
FastLED.show();
}
for (int i=0; i<min( numLeds, NUM_LEDS); i++){
leds[i] = CRGB::Green;
FastLED.show();
}
}