10 seconds loop

Hi all

I have the question is something is possible and if answer is YES ... how can I get it

1. Power on leds
2. Listen for a button
3. On button press do 'random_led_blink' for a 10 seconds
4. After ten seconds return to point 1

I know how to lights up leds, listen to the button press but I cannot get to work blinking for a specific period of time

const int buttonPin = 2;
int value, value2 ;
int ledpin = 3;                     
int ledpin2 = 5;                  
long time=0;

int periode =1000;
int displace = 500;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledpin, OUTPUT);
pinMode(ledpin2, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop () {
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
random_led_blinking ();
} else {
    // turn LED off:
    digitalWrite(ledpin, LOW);
  digitalWrite(ledpin2, LOW);
  }
}

void random_led_blinking ()
{
 time = millis();
 value = 128+127*cos(2*PI/periode*time);
 value2 = 128+127*cos(2*PI/periode*(displace-time));
 analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
 analogWrite(ledpin2, value2);           // sets the value (range from 0 to 255)
}

Any help how to code to make 10 secons blink after button was pressed ??

Doing timing using millis() is one of the four locked posts at the top of this forum.

I have tried but [ I assume but not sure ] because using mills in "random_led_blinking" I cannot get it to work so that why I asked my question

my code was:

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds

const int buttonPin = 2;
int value, value2 ;
int ledpin = 3;                    
int ledpin2 = 5;                  
long time=0;

int periode =1000;
int displace = 500;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledpin, OUTPUT);
pinMode(ledpin2, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);


  startMillis = millis();  //initial start time

}

void loop () {
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {

  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {

random_led_blinking ();
 startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.

} else {
    // turn LED off:
    digitalWrite(ledpin, LOW);
  digitalWrite(ledpin2, LOW);
  }



}

void random_led_blinking ()
{
 time = millis();
 value = 128+127*cos(2*PI/periode*time);
 value2 = 128+127*cos(2*PI/periode*(displace-time));
 analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
 analogWrite(ledpin2, value2);           // sets the value (range from 0 to 255)
}

You are on the right track but...

There is a variable named period and a variable named periode. This may be confusing.

The period is defined as 1000 milliseconds (one second), not the ten seconds described.

This code does one thing every one second (if the button is pressed), and it will not repeat for the ten seconds described. If it did repeat random_led_blinking, it would happen so fast that a human might only perceive the average value.

Good Luck!

const int buttonPin = 2;
int value, value2 ;
int ledpin = 3;                   
int ledpin2 = 5;

Why are some pin number variables const and some not?
Why are ANY of them int? You don't have enough pins to need to use int.

unsigned long startMillis;

Start of what? Meaningful names ARE important.

  startMillis = millis();  //initial start time

The event that is supposed to start is the 10 second random blinking. That does NOT happen in setup().

Once you figure out that you need to start the blinking, and use a name for that time that makes sense, the rest should fall into place.

A bool that defines whether blinking should be happening, or not, would be useful, too. Set it when the switch BECOMES (NOT IS) pressed, and clear it when the blinky time is up.

Your code could look something like this OP. There are places I have left for you to fill in yourself. Good luck.

//define input and output pin numbers
//and global variables
bool buttonPressed = false;
bool ledBlinking = false;

void setup()
{
  //do setup things
}

void loop()
{
  readButton();
  blinkLED();
  checkTenSecondTimer();
}

void readButton()
{
  //code to check if button has been pressed
  //don't forget to debounce
  if(button has been pressed)
  {
    buttonPressed = true;
    tenSecondTimer = millis();
  }
}

void blinkLED()
{
  if(buttonPressed)
  {
    ledBlinking = true;
    buttonPressed = false;
  }
  if(it is time to turn LED on or off && ledBlinking)
  {
    turn LED on or off
  }
}

void checkTenSecondTimer()
{
  if(ten seconds has passed since you pressed the button)
  {
    ledBlinking = false;
  }
}