hy
i am using digispark, mosfet 540 and 10w led to create nice light effect in lantern.
I have tested 2 codes that are working OK, and now i would like to update 2 codes into 1 by switching them in loop with on off switch ,later i will use microphone as switch ,to create blowing candle effect,
here are codes,can anyone help
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));
}
Start by putting each of your current loop() functions in their own named functions, common code such as pinMode()s in a single setup() function and common global variable declarations at the top of the program. Put calls to both new functions in a new loop() function.
Resolve any issues with multiple variable declarations if they exist and you will have made a start.
Then you can start to tackle the problem of running one new function or the other based on an input. Start by declaring a boolean variable and calling one of the new functions when it is true and the other when it is false. When that works you can move on to changing its value using an input.
Note that in order to be able to switch from one mode to another in a timely way you may need to completely change your programs to aviod blocking response to the input.
ok i tried to copy some code from older post ,and came with this tipe of code
but i get error at flicker_random_low_start = random was not declared in this scope
int cycle=30;
int strobe=cycle*30; // calculate strobe delay
int maxFade=150; // maximum brightness before strobe
int ledPin = 0; // MOSFET connected to digital pin 11
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() {
// nothing happens in setup
}
void loop() {
lights1();
lights2();
}
void lights1() {
// fade in from min to max in increments of 1 points:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 1) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
analogWrite(ledPin, 200); // 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);
}
digitalWrite(ledPin, LOW);
delay(1400);
}
void lights2() {
// 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));
}
i get error at flicker_random_low_start = random was not declared in this scope
In fact it is not declared at all. Declare it as a global int and see what happens, but there are other such errors too
ok so i made some corrections on code
int cycle=30;
int strobe=cycle*30; // calculate strobe delay
int maxFade=150; // maximum brightness before strobe
int ledPin = 0; // MOSFET connected to digital pin 11
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 lights1() {
// fade in from min to max in increments of 1 points:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 1) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
analogWrite(ledPin, 200); // 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);
}
digitalWrite(ledPin, LOW);
delay(1400);
}
void lights2() {
// 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));
}
and now i get
Error compiling for board Arduino Nano.
Error compiling for board Digispark (Default - 16.5mhz).
Where is the loop() function ?
here it is,i have put value of analog read pin 3 to determine what will loop,is it ok
int cycle=30;
int strobe=cycle*30; // calculate strobe delay
int maxFade=150; // maximum brightness before strobe
int ledPin = 0; // MOSFET connected to digital pin 11
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
int val = analogRead(5);
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() {
int val = analogRead(5);
if (val = analogRead(5) == 1){
// fade in from min to max in increments of 1 points:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 1) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
analogWrite(ledPin, 200); // 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);
}
digitalWrite(ledPin, LOW);
delay(1400);
}
if (val = analogRead(5) == 3)
{
// 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));
} }
if (val = analogRead(5) == 3)
i have put value of analog read pin 3 to determine what will loop,is it ok
No.
That is not how you read a value from a pin, assign the value returned to a variable and test its value. Neither should you use analogRead() to read a digital (on/off) switch.
Whist you don't have to do what I suggested, I see that you have not put the code for the 2 effects in separate function.
man,if i knew how to do it i would not be on this forum,
this is my first code
Keep it simple and do it in 2 steps.
val = digitalRead(5); //read the state of pin 5
if (val == LOW)
{
//run the code here
}
else
{
//run the code here
}
How is the input to pin 5 wired ?
its not,but i can edit my post ,i must wait 5 min
that why i have so many mistakes on posting earlier on
ok, after some reading...
i made code with 2 functions ,and call them,the rere function will delay 1000,and keep looping but the rer function will not be called.
If i only call rer function it will loop without delay.
what is the problem there
int FLICKER_LED_PIN = 1;
// 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
int cycle=30;
int strobe=cycle*10; // calculate strobe delay
int maxFade=100; // maximum brightness before strobe
int ledPin = 1; // MOSFET connected to digital pin 11
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 rere() {
// 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);
}
}
void rer() {
// 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));
}
void loop()
{
rere();
delay (1000);
rer();
delay (1000);
}
and another questions ,when i put the microphone to act as switch between functions,
do i need to use break; statement or some other.
and tricky question is,for me to test code with switch statement on digispark i need analog read from some pin.
because i don't yet have microphone(SHIPPING FROM CHINA) and i don't have the opportunity to solder some switch.Is it possible that digispark will read some static from screwdriver or some wire on designated pin,to be able to trigger switch effect,for me to be able to test code.
thank you
Both functions are called when I run your code.
How do I know ? Because I put a print statement at the start of each function telling me that it was being executed.
Sample output
In rer
In rere
In rer
In rere
In rer
In rere
In rer
i see,well its hard to see difference on small led on digistark,and the function rer is probably different when run for more time ,because of random .do you have any ideas on other questions plz
What do you see if you do the same as me ?
Are both functions being executed ?
i cant call serial read on digispark its complicated for that type of adruino
OK. Maybe blink the LED at full brightness for a number of times at the start of each function, different for each function, to prove that they are being called.
and another questions ,when i put the microphone to act as switch between functions,
do i need to use break; statement or some other.
and tricky question is,for me to test code with switch statement on digispark i need analog read from some pin.
because i don't yet have microphone(SHIPPING FROM CHINA) and i don't have the opportunity to solder some switch.Is it possible that digispark will read some static from screwdriver or some wire on designated pin,to be able to trigger switch effect,for me to be able to test code.
thank you