Hy,i have digispark ,microphone,and 10w led with mosfet.
I have 3 codes that are working well by them selves .
But i need help(i am noob) to make it into 1 code that will have all 3 funcions ,2 on loop,
so i need code that when i clap one time i switches the function to next one.
It needs 3 function :1 candle efect ,2 lighthouse effect and 3 off.
here is clap code:
//---------------------------------------------------------------------
// Program: clap switch
//
// Description: Switches on an LED when hands are clapped.
// Electret microphone connected to A2, LED and series
// resistor connected to digital pin 2
//
// Date: 6 April 2016 Author: W.A. Smith
// http://startingelectronics.org
//---------------------------------------------------------------------
void setup() {
Serial.begin(9600); // using serial port to check analog value
pinMode(2, OUTPUT); // LED on digital pin 2
}
void loop() {
int analog_val; // analog value read from A2
static bool led_state = false; // current state of LED
analog_val = analogRead(A2);
if (analog_val > 10) { // trigger threshold
// toggle LED
if (led_state) {
led_state = false; // LED was on, now off
digitalWrite(2, LOW);
Serial.println(analog_val); // print analog value for debug purposes
}
else {
led_state = true;
digitalWrite(2, HIGH); // LED was off, now on
Serial.println(analog_val);
}
delay(50); // wait for clap noise to subside
}
}
any codes that are even similar will help,thank you
Do your lighthouse and candle functions prevent loop() running freely, perhaps by using delay() and/or while, or do they rely on loop() for repeating their effects, probably using millis() ?
The reason that I ask is that if they allow loop() to run freely then you can put the detection code in loop(), otherwise a different technique will be required
int FLICKER_LED_PIN = 0;
// the start of the flicker (low)
static int flicker_low_min = 200;
static int flicker_low_max = 240;
// the end value of the flicker (high)
static int flicker_high_min = 230;
static int flicker_high_max = 256;
// delay between each low-high-low cycle
// low->high |flicker_hold| high->low
static int flicker_hold_min = 40; // milliseconds
static int flicker_hold_max = 80; // milliseconds
// delay after each low-high-low cycle
// low->high->low |flicker_pause| low->high...
static int flicker_pause_min = 100; // milliseconds
static int flicker_pause_max = 200; // milliseconds
// delay low to high and high to low cycle
static int flicker_speed_min = 900; // microseconds
static int flicker_speed_max = 1000; // microseconds
void setup() {
pinMode(FLICKER_LED_PIN, OUTPUT);
}
int flicker_random_low_start = 0;
int flicker_random_low_end = 0;
int flicker_random_high = 0;
int flicker_random_speed_start = 0;
int flicker_random_speed_end = 0;
void loop() {
// random time for low
flicker_random_low_start = random(flicker_low_min, flicker_low_max);
flicker_random_low_end = random(flicker_low_min, flicker_low_max);
// random time for high
flicker_random_high = random(flicker_high_min, flicker_high_max);
// random time for speed
flicker_random_speed_start = random(flicker_speed_min, flicker_speed_max);
flicker_random_speed_end = random(flicker_speed_min, flicker_speed_max);
// low -> high
for (int i = flicker_random_low_start; i<flicker_random_high; i++) {
analogWrite(FLICKER_LED_PIN, i);
delayMicroseconds(flicker_random_speed_start);
}
// hold
delay(random(flicker_hold_min, flicker_hold_max));
// high -> low
for (int i = flicker_random_high; i>=flicker_random_low_end; i--) {
analogWrite(FLICKER_LED_PIN, i);
delayMicroseconds(flicker_random_speed_end);
}
// pause
delay(random(flicker_pause_min, flicker_pause_max));
}
int FLICKER_LED_PIN = 0;
// the start of the flicker (low)
static int flicker_low_min = 200;
static int flicker_low_max = 240;
// the end value of the flicker (high)
static int flicker_high_min = 230;
static int flicker_high_max = 256;
// delay between each low-high-low cycle
// low->high |flicker_hold| high->low
static int flicker_hold_min = 40; // milliseconds
static int flicker_hold_max = 80; // milliseconds
// delay after each low-high-low cycle
// low->high->low |flicker_pause| low->high...
static int flicker_pause_min = 100; // milliseconds
static int flicker_pause_max = 200; // milliseconds
// delay low to high and high to low cycle
static int flicker_speed_min = 900; // microseconds
static int flicker_speed_max = 1000; // microseconds
void setup() {
pinMode(FLICKER_LED_PIN, OUTPUT);
}
int flicker_random_low_start = 0;
int flicker_random_low_end = 0;
int flicker_random_high = 0;
int flicker_random_speed_start = 0;
int flicker_random_speed_end = 0;
void loop() {
// random time for low
flicker_random_low_start = random(flicker_low_min, flicker_low_max);
flicker_random_low_end = random(flicker_low_min, flicker_low_max);
// random time for high
flicker_random_high = random(flicker_high_min, flicker_high_max);
// random time for speed
flicker_random_speed_start = random(flicker_speed_min, flicker_speed_max);
flicker_random_speed_end = random(flicker_speed_min, flicker_speed_max);
// low -> high
for (int i = flicker_random_low_start; i<flicker_random_high; i++) {
analogWrite(FLICKER_LED_PIN, i);
delayMicroseconds(flicker_random_speed_start);
}
// hold
delay(random(flicker_hold_min, flicker_hold_max));
// high -> low
for (int i = flicker_random_high; i>=flicker_random_low_end; i--) {
analogWrite(FLICKER_LED_PIN, i);
delayMicroseconds(flicker_random_speed_end);
}
// pause
delay(random(flicker_pause_min, flicker_pause_max));
}
her are codes
Is this not a duplicate of switch between 2 loops - Programming Questions - Arduino Forum ?
Why did you not continue it there ?
i put microphone in so its different code.
and this is second code i duplicated the first one in my previous post
int cycle=30;
int strobe=cycle*10; // calculate strobe delay
int maxFade=100; // maximum brightness before strobe
int ledPin = 11; // MOSFET connected to digital pin 11
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 2 points:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
analogWrite(ledPin, 255); // simulate a rotating beacon catching your eye
delay(strobe); // hold full brightness for strobe delay
analogWrite(ledPin, maxFade);
// fade out from maxFade to min in increments of 2 points:
for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
}
Do as I suggested in the other thread. Copy the code from each of the 2 loop() functions into their own function in a new program. Add in a setup() function to set up pinMode()s etc and put the global variable declarations at the top of the program. Expect problems and/or clashes when you compile the program. Resolve them.
Now make the calling of one or other of the new functions dependant on the value of a variable. Set the variable so that one of them is called, compile the program. Does it do what you expect ? Change the variable so that the other function is called, compile the program. Does it do what you expect.
Come back when that is working and you can add code to change the variable when microphone input is detected.