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);
}