How do I control the duration of flashing leds with potentiometers?

So, I got this ide of having a push-button trigger 2 events.

1 When the button is pushed, I want a led to flash two times with an off period in between. I want to control the duration of the flashes (on periods) and the off period in-between with 3 potentiometers.
2 The second led should light up when I push the button and stay on as long as the complete period of the first two flashes and the paus in-between.

Button pushed -> led2 goes on. After 1000ms led1 goes on and stays on for the duration decided by the potentiometer. It goes off and the off period is also controlled by a second potentiometer. It goes on again after the off period and the second on period is controlled by a third potentiometer. When the last on period is over the led2 shuts off. So, the duration of the on period of led2 follows the complete time it takes for led1 to flash two times included the brake in between.

I manage to write the code to control the duration of one led with a potentiometer using millis but I can’t figure out how to implement a second variable with the break and second on time.

Could some one please help me out, I am such a newbie I’ve spent days trying to figure this one out.
I attached the code for the first led below.
Regards Nils

const int buttonPin = 2;
const int ledPin = 12;
const int analogPin = 0;  // sets pin for analog read 1   
const int analogPin1 = 1; // sets pin for analog read 2
const int analogPin2 = 2; // sets pin for analog read 3

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

pinMode(ledPin,OUTPUT); // LED output
pinMode(buttonPin,INPUT); // Button input
pinMode (analogPin, INPUT); // Pot read resistance
pinMode (analogPin1, INPUT); // Pot read for second value, the break in beween pulses
pinMode (analogPin2, INPUT); // Pot read for third value 

}
void loop()
{
  static unsigned char ledState = LOW;
  static unsigned char buttonState = LOW;
  static unsigned char lastButtonState = LOW;
  static unsigned long ledCameOn = 0;

  int analogValue2 = map(analogRead(analogPin2),0,1023,2000,5); // this is going to be the paus period in between flashes
  int analogValue1 = map(analogRead(analogPin1),0,1023,4000,0); // this is  going to be my second on period
  int analogValue = map(analogRead(analogPin),0,1023,2000,0);   // this is the first on period
     
 // if the led has been on for more than the analogValue turn it off 
 
  if(ledState == HIGH)
  {
    if(millis()-ledCameOn > analogValue)
    {
      digitalWrite(ledPin,LOW);
      ledState = LOW;
    }
  }

  // If the button's state has changed, then turn the LED on IF it is not on already.
  buttonState = digitalRead(buttonPin);
  if(buttonState != lastButtonState)
  {
    lastButtonState = buttonState;
    if((buttonState == HIGH) && (ledState == LOW))
    {
      digitalWrite(ledPin,HIGH);
      ledState = HIGH;
      ledCameOn = millis();
    }

if 

 
  }
  // print out the value you read:
  Serial.println(analogValue);
}

You are dealing with a series of events against time axis, and everything is in your head. You need to bring them down on paper in the form of diagram, flow chart, and pseudo codes. This approach will help you to do the job yourself without being dependent so much on others.

1. Be sure the Button is connected with DPin-2 with internal pull-up resistor connected.

2. Be sure LED2 is connected with DPin-12 with a 2.2k series resistor.

3. Consider the L (built-in LED of UNO) as LED1.

4. Be sure you have connected the wiper of Pot1 (5k/10k) with A0-pin; terminal-1 with 5V and terminal-2 with GND. Be sure that when you turn the Pot CW, the voltage at the wiper-pin increases.

5. Follow Step-4 and connect Pot2 with A1-pin and Pot3 with A2-pin of UNO.

6. When you complete all the above steps, you will come up with the following hardware setup:
ledSwx.png

7. Decide that the ON-period of LED1 (at the first flash/blink) would be 5-sec (max) when the wiper-pin of Pot1 is set at 5V. The mapping would be like this:

int onPeriodF1 = map(analogRead(A0), 0, 1023, 0, 5000); //5000 ms

8. Decide that the OFF-period of LED1 (at the first flash/blink) would be 5-sec (max) when the wiper-pin of Pot2 is set at 5V. The mapping would be like this:

int offPeriodF1 = map(analogRead(A1), 0, 1023, 0, 5000); //5000 ms

9. Decide that the ON-period of LED1 (at the 2nd flash/blink) would be 5-sec (max) when the wiper-pin of Pot3 is set at 5V. The mapping would be like this:

int onPeriodF2 = map(analogRead(A2), 0, 1023, 0, 5000); //5000 ms

10. Now, formulate the Control Structure/Pseudo Code for the system:
L1: Initialize everything as needed. Set Po1 Pot2, and Pot3 at approx. middle positions.
L2: Wait here until Button (K1) is closed. Closed condition of K1 is indicated by LOW-state at DPin-2.
L3: ON LED2
L4: Wait here until 1000 ms has elapsed.
L5: ON LED1
L6: Wait here until ON-period of LED1 (flash/blink 1) is elapsed as has been determined by Pot1.
L7: OFF LED1.
L8: Wait here until OFF-period of LED1 (flash/blink 1) is elapsed as has been determined by Pot2.
L9: ON LED1.
L10: Wait here until ON-period of LED1 (flash/blink 2) is elapsed as has been determined by Pot3.
L11: OFF LED1 and LED2.
L12: Repeat cycle from L1 after changing the wiper positions lof Pot1 - Pot3. If you like you can connect LCD to see how much ON and OFF period you are setting for LED1 before the process begins/repeats.

11. Convert the tasks of Step-10 into Arduino/C++ Language and accordingly revise your codes. Compile and upload the sketch. Check that the events occur as expected.

(12. This is the Sketch for myself)

#define BUtton 2
#define LED2 12
#define LED1 13

void setup()
{
  Serial.begin(9600);
  pinMode(BUtton, INPUT_PULLUP);
  pinMode(LED2, OUTPUT); 
  pinMode(LED1, OUTPUT); 
  digitalWrite(LED2, LOW);
  digitalWrite(LED1, LOW);
}

void loop()
{
  while (digitalRead(Button) != LOW)
  {
    ;   //wait until Button is closed
  }
  //--------------------------
  digitalWrite(LED2, HIGH);
  delay(1000);
  //--------------
  digitalWrite(LED1, HIGH); //LED1 is ON
  int onPeriodF1 = map(analogRead(A0), 0, 1023, 0, 5000);
  delay(onPeriodF1);
  //---------------
  digitalWrite(LED1, LOW); //LED1 is ON
  int offPeriodF1 = map(analogRead(A1), 0, 1023, 0, 5000);
  delay(offPeriodF1);
  //------------------------------------------------------
  digitalWrite(LED1, HIGH); //LED1 is ON
  int onPeriodF2 = map(analogRead(A2), 0, 1023, 0, 5000);
  delay(onPeriodF2);
  //--------------------------
  digitalWrite(LED1, LOW);  //LED1 OFF
  digitalWrite(LED2, LOW);  //LED2 OFF
}

ledSwx.png

13. Give descriptive names to the pins, so that the sketch is easier to modify and maintain.

AWOL:
13. Give descriptive names to the pins, so that the sketch is easier to modify and maintain.

The directive has been executed.

Thank you very much for the good advices and the time spent on showing me examples. It seemed like every one was cursing the delay function so I taught i needed to implement the millis() and that got me off track.

I'm working on a Arduino Nano. I tried to use the base code of your example but I'm facing a problem with the execution of the functions from the the push-button. Do this has to do with the

pinMode(Button, INPUT_PULLUP); and the

while (digitalRead(Button) != LOW) command ?

Ive only used to read the buttonState as HIGH or LOW before and if the buttonState was HIGH call a function

Ive changed the code you wrote because I could not get it to work with the button but now the functions (led's) just keep on going. I hope you could shine some light over this because i don't seem to find the answer myself.

const int Button = 2;
const int LED1 = 12;
const int LED2 = 11;
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  Serial.begin(9600);
  pinMode(Button, INPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED1, OUTPUT);
  digitalWrite(LED2, LOW);
  digitalWrite(LED1, LOW);
}

void loop()
{


buttonState = digitalRead(Button);

if(buttonState == HIGH) {
    
     
     //--------------------------
  digitalWrite(LED2, HIGH);
  delay(1000);
  //--------------
  digitalWrite(LED1, HIGH); //LED1 is ON
  int onPeriodF1 = map(analogRead(A0), 0, 1023, 5000, 0);
  delay(onPeriodF1);
  //---------------
  digitalWrite(LED1, LOW); //LED1 is ON
  int offPeriodF1 = map(analogRead(A1), 0, 1023, 5000, 0);
  delay(offPeriodF1);
  //------------------------------------------------------
  digitalWrite(LED1, HIGH); //LED1 is ON
  int onPeriodF2 = map(analogRead(A2), 0, 1023, 5000, 0);
  delay(onPeriodF2);
  //--------------------------
  digitalWrite(LED1, LOW);  //LED1 OFF
  digitalWrite(LED2, LOW);  //LED2 OFF   //wait until Button is closed
  
     }
}

Ive changed the code you wrote because I could not get it to work with the button

  pinMode(Button, INPUT);

How IS your button sewn on? In the absence of any detail, I'm going to suspect that you are not using a pullup or pulldown resistor, so the results of digitalRead() will be unpredictable.

Break the map and read statements into two parts. Are you getting good values from the pots? Are you mapping those values correctly? Debugging by guesswork sucks, and is unnecessary when there are easy to use tools to get useful data.

Nils_s:
Ive changed the code you wrote because I could not get it to work with the button but now the functions (led's) just keep on going.

PaulS:

  pinMode(Button, INPUT);

How IS your button sewn on?

Please, connect a pull-down resistor (2.2k or close) at DPin-2 side of the Button (K1); connect 5V at the other side of Button. And now, my original program and your version of my program are equivalent.

There is a reason why I have used this spelling 'BUtton' instead of 'Button' -- in the IDE the 'Button' comes in red-color for which I have no explanation; but, Button is a variable/identifier for me.

in the IDE the 'Button' comes in red-color for which I have no explanation;

It is in a keywords.txt file in a library on your system, possibly more than one. Key words in those files are coloured in the IDE according to the setting in the file but the colour or lack of it has no significance. This happens whether or not you #include the associated library in your code.

UKHeliBob:
It is in a keywords.txt file in a library on your system, possibly more than one. Key words in those files are coloured in the IDE according to the setting in the file but the colour or lack of it has no significance. This happens whether or not you #include the associated library in your code.

You have been always gentle and tidy (K+) to avail the chance of educating others!!

Nils_s:
It seemed like every one was cursing the delay function so I taught i needed to implement the millis() and that got me off track.

Sometimes it does feel like there's an Animal Farm-like "millis() good, delay() bad" paradigm.

Nothing wrong with using delay() in a case like this, as the requirement stands right now. The potential problem of course is that one day your requirements might change to (let's say) include another button that needs to be read at any time once the pattern you describe has started, perhaps to do something with a 3rd led. Your blocking code will prevent that new button being read responsively, and in that case a millis() based approach would be indicated.

So it all depends how confident you are that a delay() based approach is ok for the long term.

I got it to work! After a few frustrating hours I found out that it was a cable on my breadboard that was broken. Good lesson learned. Next time I am going to spend a few seconds checking the conductivity of the wires that is being used so I don't spend hours later scratching my head not making any sense of anything. Thank you all for you help guiding me trough this. I actually learn a lot about about other things researching this. So it´s not all waste :slight_smile:

Nils_s:
So it´s not all waste :slight_smile:

As long as you’re not charging a client 100’s of for your learning curve :wink:

But seriously, when you do another project, or update this one, you will find that time spent getting to grips with millis() and blinking without delay, is also not a waste, it’s an investment.

@not_a_noob Yeah, haha! no need to worry about me charging clients. But I like to learn and I would love to see and example of using millis in my case if you are up for it? I got it to work with the first led but I had a hard time understanding how I should call out the functions from all the different timestamps and have something counting the time from all three potentiometers. But don't feel any pressure from an unknown learner. The delay function does what I need it to do for now. :slight_smile:

Nils_s:
I would love to see and example of using millis in my case if you are up for it?

I'll try to get some time on that, probably only at the weekend though.

I have found that I need to abort the functions as soon as the button is released. So the functions can only go on as long as the button is pushed down. Any suggestions on how to do that? I’m guessing that I cant not do that with the code as it is suggested above.

It feels like I am back to trying to get the millis to work again. Am i right?

Regards Nils

It feels like I am back to trying to get the millis to work again. Am i right?

It certainly sonds like it