how to make the LED blink in specific frequency for specific amount of time

Hi,

I am new to programming in Arduino,

So basically I am trying to make the LEDs blink at frequency 40 Hz lasting for 1 second and repeat that every 10s for a total time of 10 minutes.

and another program that makes the LEDs blink 40 Hz for 125 milliseconds every minute for 48 hours.

can someone help with that or direct me toward the functions and the general logic that I can use.

Thanks in advance.

Welcome to the group.

What is this going to be used for?

What software/hardware experience do you have?

Let’s see what you have tried so far.
Use CTRL T to format your code.
Attach your complete sketch between code tags
[code]Paste your sketch here[/code]

The blink without delay example in the IDE has all you need to get you started.

Thanks,

I actually have no Software/ hardware experience, apart from the first few projects in the Arduino starter kit.

I am using this for photostimulation of neuronal culture with Blue light. (optogenetics project I am working on).

To be honest I failed to sketch anything! I tried to use delays and then blink without delay but I guess the problem needs me to reiterate over the function using some other functions.

Thanks again.

“So basically I am trying to make the LEDs blink at frequency 40 Hz lasting for 1 second and repeat that every 10s for a total time of 10 minutes. “

What triggers this to start?

“lasting for 1 second” 1 second on 1 second off?

“I tried to use delays and then blink without delay”

Okay, show us your attempt.

What triggers this to start? pressing the button.

"lasting for 1 second" 1 second on 1 second off? 1 second on and the 9 seconds off and then on again for 10 minutes.

I will upload the sketch once I go home and get my laptop sorry for that! but honestly, I am sure I am nowhere near answering this.

I am sure I am nowhere near answering this.

That’s okay, we can offer you suggestions.

Close to what you want?

Both tests are in this sketch. You choose which one runs by grounding (or leaving open) pin 2 when you press the reset button.

The tests are simple and block; the controller is tasked only with doing these two tests. If you need it to do other things, you’ll need to take a state machine approach.

You might want to scope the output (if you have access to one) to verify the timing overall. I don’t have access to my scope at the moment so I can’t verify it. It seems like it’s right…

const int pinLED = 13;
const int pinSelect = 2;

#define ON_STATE    HIGH
#define OFF_STATE   LOW

//LED timing is common for both tests
#define T_ON            12500ul     //uS
#define T_PER           25000ul     //uS
//burst time, frame period and test duration times for tests 1 and 2
#define T_BURST_LEN     1000ul      //mS
#define T_FRAME          9000ul     //mS
#define T_DURATION      600000ul    //mS
//
#define T_BURST_LEN_2   125000ul    //uS
#define T_FRAME_2       60000ul     //mS
#define T_DURATION_2    172800000ul //mS (48hrs; 48*3600*1000 mS)


void setup( void )
{
    //Select chooses which test runs
    //  1/open = testOne
    //  0/closed = testTwo
    pinMode( pinSelect, INPUT_PULLUP );

    //LED drive output
    pinMode( pinLED, OUTPUT );

    //select which test to run
    if( digitalRead( pinSelect ) == HIGH )
        testOne();
    else
        testTwo();
    
}//setup
     
void loop()
{
}//loop

//EDs blink at frequency 40 Hz lasting for 1 second 
//and repeat that every 10s for a total time of 10 
//minutes
void testOne( void )
{
    unsigned long
        timeFrame,
        timeDuration,
        timeBurstStart,
        timeBurst;

    timeDuration = millis();
    do
    {
            timeBurstStart = millis();
            do
            {
                timeBurst=micros();
                digitalWrite( pinLED, ON_STATE );               
                while( (micros() - timeBurst) < T_ON );         //12500uS
                digitalWrite( pinLED, OFF_STATE );              
                while( (micros() - timeBurst) < T_PER );        //25000uS
                
            }while( (millis() - timeBurstStart) < T_BURST_LEN );  //1-second
            
            timeFrame = millis();
            while( (millis() - timeFrame) < T_FRAME );            //10-seconds
        
    }while( (millis() - timeDuration) < T_DURATION );           //10minutes
    
}//testOne

//LEDs blink 40 Hz for 125 milliseconds every minute for 48 hours
void testTwo( void )
{
    unsigned long
        timeFrame,
        timeDuration,
        timeBurstStart,
        timeBurst;

    timeDuration = millis();
    do
    {
            timeBurstStart = micros();
            do
            {
                timeBurst=micros();
                digitalWrite( pinLED, ON_STATE );               
                while( (micros() - timeBurst) < T_ON );         //12500uS
                digitalWrite( pinLED, OFF_STATE );              
                while( (micros() - timeBurst) < T_PER );        //25000uS
                
            }while( (micros() - timeBurstStart) < T_BURST_LEN_2 );  //125mS
            
            timeFrame = millis();
            while( (millis() - timeFrame) < T_FRAME_2 );            //60-seconds
        
    }while( (millis() - timeDuration) < T_DURATION_2 );           //48 hours
    
}//testTwo

Blackfin:
Close to what you want?

Both tests are in this sketch. You choose which one runs by grounding (or leaving open) pin 2 when you press the reset button.

The tests are simple and block; the controller is tasked only with doing these two tests. If you need it to do other things, you’ll need to take a state machine approach.

You might want to scope the output (if you have access to one) to verify the timing overall. I don’t have access to my scope at the moment so I can’t verify it. It seems like it’s right…

const int pinLED = 13;

const int pinSelect = 2;

#define ON_STATE    HIGH
#define OFF_STATE  LOW

//LED timing is common for both tests
#define T_ON            12500ul    //uS
#define T_PER          25000ul    //uS
//burst time, frame period and test duration times for tests 1 and 2
#define T_BURST_LEN    1000ul      //mS
#define T_FRAME        10000ul    //mS
#define T_DURATION      600000ul    //mS
//
#define T_BURST_LEN_2  125000ul    //uS
#define T_FRAME_2      60000ul    //mS
#define T_DURATION_2    172800000ul //mS (48hrs; 4836001000 mS)

void setup( void )
{
    //Select chooses which test runs
    //  1/open = testOne
    //  0/closed = testTwo
    pinMode( pinSelect, INPUT_PULLUP );

//LED drive output
    pinMode( pinLED, OUTPUT );

//select which test to run
    if( digitalRead( pinSelect ) == HIGH )
        testOne();
    else
        testTwo();
   
}//setup
   
void loop()
{
}//loop

//EDs blink at frequency 40 Hz lasting for 1 second
//and repeat that every 10s for a total time of 10
//minutes
void testOne( void )
{
    unsigned long
        timeFrame,
        timeDuration,
        timeBurstStart,
        timeBurst;

timeDuration = millis();
    do
    {
            timeBurstStart = millis();
            do
            {
                timeBurst=micros();
                digitalWrite( pinLED, ON_STATE );             
                while( (micros() - timeBurst) < T_ON );        //12500uS
                digitalWrite( pinLED, OFF_STATE );             
                while( (micros() - timeBurst) < T_PER );        //25000uS
               
            }while( (millis() - timeBurstStart) < T_BURST_LEN );  //1-second
           
            timeFrame = millis();
            while( (millis() - timeFrame) < T_FRAME );            //10-seconds
       
    }while( (millis() - timeDuration) < T_DURATION );          //10minutes
   
}//testOne

//LEDs blink 40 Hz for 125 milliseconds every minute for 48 hours
void testTwo( void )
{
    unsigned long
        timeFrame,
        timeDuration,
        timeBurstStart,
        timeBurst;

timeDuration = millis();
    do
    {
            timeBurstStart = micros();
            do
            {
                timeBurst=micros();
                digitalWrite( pinLED, ON_STATE );             
                while( (micros() - timeBurst) < T_ON );        //12500uS
                digitalWrite( pinLED, OFF_STATE );             
                while( (micros() - timeBurst) < T_PER );        //25000uS
               
            }while( (micros() - timeBurstStart) < T_BURST_LEN_2 );  //125mS
           
            timeFrame = millis();
            while( (millis() - timeFrame) < T_FRAME_2 );            //60-seconds
       
    }while( (millis() - timeDuration) < T_DURATION_2 );          //48 hours
   
}//testTwo

Thank you very much it seems to be working fine.
I will try to separate it into 2 sketches since I will be using them in two different settings. that should be relatively simple hopefully otherwise I will need you again!

thank you very much, I am really greatfull to you!

@Blackfin

I believe the OP needs:

"1 second on and the 9 seconds off"
#define T_FRAME 10000ul //mS
Change to:
#define T_FRAME 9000ul //mS

Maybe make this a teachable moment too.
#define T_DURATION_2 172800000ul //mS (48hrs; 4836001000 mS)
Change to:
#define T_DURATION_2 4836001000ul

Tis blocking.

Hardly a noob piece of code but looks good!

The simplest way to separate them would be to copy the ino file to two files (testOne.ino and testTw.ino, for example) and then, in setup(), just change:

   //select which test to run
    if( digitalRead( pinSelect ) == HIGH )
        testOne();
    else
        testTwo();

to

testOne();

in testOne.ino and

testTwo();

in testTwo.ino.

It won't hurt anything to have the code for the "other" test the source. If you feel adventurous you could delete everything not used in both...

larryd:
@Blackfin

I believe the OP needs:

"1 second on and the 9 seconds off"
#define T_FRAME 10000ul //mS
Change to:
#define T_FRAME 9000ul //mS

Yep, you're right. What I posted was not what I originally coded; I blew that timing part away and used what you see instead but didn't update that time constant. Thanks.

And yes, it's blocking which makes baby Jesus weep but I figured the OP just wanted to do something very specific in a specific order and didn't care to read switches or sensors or update displays etc and so a state-machine or similar method wouldn't be warranted.

and didn’t care to read switches

"What triggers this to start? pressing the button. "

which makes baby Jesus weep

Blackfin:
Yep, you're right. What I posted was not what I originally coded; I blew that timing part away and used what you see instead but didn't update that time constant. Thanks.

And yes, it's blocking which makes baby Jesus weep but I figured the OP just wanted to do something very specific in a specific order and didn't care to read switches or sensors or update displays etc and so a state-machine or similar method wouldn't be warranted.

Thanks again,
I was also wondering if Arduino can allow me to control multiple (6 for example) output pins with the same code by putting them in a vector or something!

I know I can do that by just connecting all my 6 LEDs in series with each other to the same pin.

wahba:
Thanks again,
I was also wondering if Arduino can allow me to control multiple (6 for example) output pins with the same code by putting them in a vector or something!

I know I can do that by just connecting all my 6 LEDs in series with each other to the same pin.

If you want all 6 LEDs to do the same thing I would use the same code and use external electronics to drive them. For example, the micro pin drives a buffer or transistor arrangement to switch the LEDs.

Now, depending on how you drive them you might have to change the pin level senses in the code but it would otherwise be the same.