Halloween project: (pulsing Led in a Pumpkin )

Hello,

I want to run pulsing led (breading led) for 10 minutes.
After a push on a button

How can i do this?

I want to use the pulsing led code below:

I hope you guys can help me.
Thanks in advance.

// 

// Power save
#include <avr/sleep.h>
#include <avr/wdt.h>
 
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
 
int startNc =3;
int nc; 
int pinLed = 4;
volatile boolean f_wdt = 1;
 
void setup(){
setup_watchdog(6); // approximately 1 seconds sleep
pinMode(0, OUTPUT); // Led 1 On/Off knop GSM
pinMode(2, OUTPUT); // Led 2 Bel knop GSM
pinMode(pinLed,OUTPUT);
digitalWrite(3, HIGH); //turn on internal pull up resistors
nc=digitalRead (startNc);

}
 
void loop(){
if (f_wdt==1) {  // wait for timed out watchdog / flag is set when a watchdog timeout occurs
f_wdt=0;       // reset flag
 
digitalWrite(pinLed,HIGH);  // let led blink
delay(5);
digitalWrite(pinLed,LOW);

pinMode(pinLed,INPUT); // set all used port to intput to save power
system_sleep();
pinMode(pinLed,OUTPUT); // set all ports into state before sleep
 
}

if(digitalRead(startNc) == HIGH){

float in, out;
 
  for (in = 0; in < 6.283; in = in + 0.00060)
  {
    out = sin(in) * 127.5 + 127.5;
    analogWrite(LED,out);
    in = in + .0001 * out /255;


}
}


 
// set system into the sleep state
// system wakes up when wtchdog is timed out
void system_sleep() {
cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF
 
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
 
sleep_mode();                        // System sleeps here
 
sleep_disable();                     // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON
}
 
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
 
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
 
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDTIE);
}
 
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
f_wdt=1;  // set global flag
}

What is the problem?

No need whatsoever to use float variables and a sinewave is inappropriate.

Read this discussion before you proceed.

However you achieve your "breading" effect (I think you meant "breathing"), you need to repeat it for 10 minutes, which is 600,000 milliseconds. You can use a while() loop and the millis() function to achieve this.

To wait for the button to be pressed, you can make another while() loop and use digitalRead() to check if the button has been pressed.

Also, have a look in the IDE examples at the Fade sketch. You’ll probably find something useful in it.

PaulRB:
However you achieve your "breading" effect (I think you meant "breathing"), you need to repeat it for 10 minutes, which is 600,000 milliseconds. You can use a while() loop and the millis() function to achieve this.

Well, no, the loop() itself is the only "while" loop you should need.

PaulRB:
To wait for the button to be pressed, you can make another while() loop and use digitalRead() to check if the button has been pressed.

What have you been smoking today? :astonished:

This is not how you do things - if only because you will sooner or later want to do other things as well. In loop(), you perform one step of the up or down ramp (which should be exponential), test the button and check millis(). For each of these actions, you make decisions and set switches (state variables) which may determine how the other actions proceed.

Paul, the OP is an absolute beginner. We should keep it simple for them this time. If they don't get their circuit completed before Halloween, the defeat may put them off electronics/Arduino as a hobby. If we can get it working in time for Halloween, they may be keen to try something a little more advanced for Christmas. Then there is time to teach the better ways. Think of this Halloween sketch as an "MVP".

Function:

  1. push on a button
  2. pulsing led for 10 minutes
  3. Sleep mode

The code below is not working.
What i do wrong?

I hope you guys can help me.
Thanks in advance.

// Power save
#include <avr/sleep.h>
#include <avr/wdt.h>
 
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
 
int startNc =3;
int nc;
int pinLed = 4;
volatile boolean f_wdt = 1;
 
void setup(){
setup_watchdog(6); // approximately 1 seconds sleep
pinMode(0, OUTPUT); 
pinMode(2, OUTPUT);
pinMode(pinLed,OUTPUT);
digitalWrite(3, HIGH); //turn on internal pull up resistors
nc=digitalRead (startNc);

}
 
void loop(){
if (f_wdt==1) {  // wait for timed out watchdog / flag is set when a watchdog timeout occurs
f_wdt=0;       // reset flag
 
digitalWrite(pinLed,HIGH);  // let led blink
delay(5);
digitalWrite(pinLed,LOW);

pinMode(pinLed,INPUT); // set all used port to intput to save power
system_sleep();
pinMode(pinLed,OUTPUT); // set all ports into state before sleep
 
}

if(digitalRead(startNc) == HIGH){

float in, out;
 
  for (in = 0; in < 6.283; in = in + 0.00060)
  {
    out = sin(in) * 127.5 + 127.5;
    analogWrite(LED,out);
    in = in + .0001 * out /255;


}
}


 
// set system into the sleep state
// system wakes up when wtchdog is timed out
void system_sleep() {
cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF
 
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
 
sleep_mode();                        // System sleeps here
 
sleep_disable();                     // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON
}
 
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
 
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
 
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDTIE);
}
 
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
f_wdt=1;  // set global flag
}