Hello Arduino folks,
Warning: noob.
I'm brand new to the forum, to arduino and I've never coded anything. I'm a self taught industrial designer experimenting with code for the first time. Youtube and sticky threads have been my friends thus far, and I've gone through many of the Example Sketches looking for answers. Sincere apologies in advance if I've overlooked obvious resources that might solve my issues.
Goal
I'm designing a light that is controlled by breathing on a sensor rather than flicking a switch. In an ideal world, blowing on the mic as if you were stoking a fire turns on the light, blowing some more stokes the fire (ie. the light's brightness increases) and a quick exhalation (ie. blowing out a candle) turns the light off.
Issues:
- I'm struggling with Analog Inputs, Digital Outputs, and having multiple constraints that need to be satisfied to trigger the LED.
Achievements so far:
- I've successfully hooked up the board, mic, and LED. I have cobbled together a sketch based on examples and resources found online that allows blowing on the mic to turn on the LED, increase the brightness with a Fade, and decrease the brightness with more breath.
Help please
In plain English, this is what I would like to accomplish:
Turn on: IF LED is OFF, AND IF mic senses input above 550 AND input duration is < 0.5s, LED turns ON.
Turn up brightness: IF mic senses input above 550 AND input duration is > 0.5s, INCREASE BRIGHTNESS 4 steps per second for duration of input until maximum brightness is achieved.
Turn off: IF LED is ON, AND IF mic senses input above 550 AND input duration is < 0.5s, LED turns OFF.
I really like the interaction of breathing on a mic to turn on a light.
I'm not tied to the specifics and I welcome any and all help.
I've gotten as far as understanding that Boolean statements are involved in the answer, but I got lost in parentheses trying to get my inputs to play nice together.
Gratefully,
Jake
/***********************************************
name:BreathingLight
Goal: Use a mic to control a light, with an interaction based on fire.
Blow to start the fire, blow to stoke the fire, blow to turn out the fire (aka. candle).
//Many thanks for work from
//- www.sunfounder.com
//online resources at ardx.org
//the model from Alison Kotin as seen on Vimeo
**************************************************/
const int ledPin = 3; //led plugged into pin 3, plug an led straight into 3 with smaller side into digital GRND
const int soundPin = A0; //voice sensor attach to A0
int brightness = 0; // how bright the LED is
int fadeAmount = 6; // how many points to fade the LED by
void setup()
{
pinMode(ledPin, OUTPUT); //set pin3 as OUTPUT
Serial.begin(9600); //initialize serial monitor
}
void loop()
{
int value = analogRead(soundPin); //read the value of voice sensor
Serial.println(value); //print the value (don't need for system, but good for dubugging.)
if (value > 550) //if the value is greater than 550
{
analogWrite(ledPin, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}