Require Help with LED, Button & Potentiometer

I have two sets of code i wish to combine. One allows for the button to remain on for a pre-determined amount of time (5000ms in this case) after the button has been pressed a second time. And the second Code is for a countdown based on a potentiometer reading, I wish to combine these codes so that the time the LED remains on after the second button push is based on the potentiometer reading.

Both Codes work well. Just have had no luck trying to combine them to achieve the desired outcome.

LED w/ Button Delay Code

// constants won't change
const int BUTTON_PIN = 2; // Arduino pin connected to button's pin
const int LED_PIN    = 13; // Arduino pin connected to LED's pin

// variables will change:
int ledState = LOW;     // the current state of LED
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button
int LEDflag = 0;  // set flag to detirmine the state of the LED (0 = off, 1 = on)

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(LED_PIN, OUTPUT);          // set arduino pin to output mode

  currentButtonState = digitalRead(BUTTON_PIN);
}

void loop() {
  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state

  if(lastButtonState == HIGH && currentButtonState == LOW) {
    if(LEDflag == 0){  // if LED is 'off'
      Serial.println("The button is pressed, turning LED on...");
      digitalWrite(LED_PIN, HIGH);  // control LED arccoding to the toggled state
      LEDflag = 1;  // toggle LED flag to 'on'
    } else {  // if LED is 'on'
      Serial.println("The button is pressed, turning LED off in 5 seconds...");
      delay (5000);  // wait for 5 seconds
      digitalWrite(LED_PIN, LOW); 
      LEDflag = 0;  // toggle LED flag to 'off'
    }
  }
}

Potentiometer Coundown Code

char a= 2, b=3, c=4, d=5, e=6, f=7, g=8;
int hit=0, mulai=11, count=0, pot=A0;
char digit1=9, digit2=10;
char no[10]={
  0b0111111, //Number 0
  0b0000110, //Number 1
  0b1011011, //Number 2
  0b1001111, //Number 3
  0b1100110, //Number 4
  0b1101101, //Number 5
  0b1111101, //Number 6
  0b0000111, //Number 7
  0b1111111, //Number 8
  0b1101111, //Number 9
};
void angka(int num){
  digitalWrite(a, num & 0b0000001);
  digitalWrite(b, num & 0b0000010);
  digitalWrite(c, num & 0b0000100);
  digitalWrite(d, num & 0b0001000);
  digitalWrite(e, num & 0b0010000);
  digitalWrite(f, num & 0b0100000);
  digitalWrite(g, num & 0b1000000);
} 
void puluhan(int w){
  digitalWrite(digit1, HIGH);
  digitalWrite(digit2, HIGH);
  angka(no[w]);
  digitalWrite(digit1, LOW);
  digitalWrite(digit2, HIGH);
  delay(3);
}
void satuan(int x){
  digitalWrite(digit1, HIGH);
  digitalWrite(digit2, HIGH);
  angka(no[x]);
  digitalWrite(digit1, HIGH);
  digitalWrite(digit2, LOW);
  delay(3);
}
void setup(){
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(pot, INPUT);
  pinMode(mulai, INPUT);
}  
void loop(){
  
  hit=analogRead(pot);
  hit=map(hit, 0, 1023, 0, 9);
  //hit=constrain(hit, 0,9);
  
  int s1=hit/10;
  int s2=hit%10;
  puluhan(s1);
  satuan(s2);
  
  int start=digitalRead(mulai);
  if (start==HIGH){ //countdown
   for (count=hit; count >= 0; count--) {
      unsigned long startTime = millis();
      for (unsigned long elapsed=0; elapsed < 800; elapsed = millis() - startTime) {
        int d1=count/10;
   		int d2=count%10;
        puluhan(d1);
        satuan(d2);
      }
    }
  }
}

Here is the Potentiometer Circuit (wouldnt allow me to upload more than 1 image)

Hi,
I tried to modify the second code as little as possible.
I used mySet() instead of setup() and myPot() instead of loop() in code 2.

The LED button pin has been changed to 12.

Removed delay(5000) by using millis() to avoid affecting code 2.

Test and see if it meets your needs.


// Code 1
// constants won't change
const int BUTTON_PIN = 12; // Arduino pin connected to button's pin
const int LED_PIN    = 13; // Arduino pin connected to LED's pin

// variables will change:
int ledState = LOW;     // the current state of LED
int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button
int LEDflag = 0;  // set flag to detirmine the state of the LED (0 = off, 1 = on)
unsigned long myTime1 = 0;        // Blynk with delay
bool flag = false;                // Control off time
//------------------------------------------------------------------
void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(LED_PIN, OUTPUT);          // set arduino pin to output mode
  mySet();                           // Call old setup()

  currentButtonState = digitalRead(BUTTON_PIN);
}
//---------------------------------------------------------------------
void loop() {
  lastButtonState    = currentButtonState;      // save the last state
  currentButtonState = digitalRead(BUTTON_PIN); // read new state

  if (lastButtonState == HIGH && currentButtonState == LOW)
  {
    if (LEDflag == 0)               // if LED is 'off'
    {
      Serial.println("The button is pressed, turning LED on...");
      digitalWrite(LED_PIN, HIGH);  // control LED arccoding to the toggled state
      LEDflag = 1;                  // toggle LED flag to 'on'
    }
    else                            // if LED is 'on'
    {
      Serial.println("The button is pressed, turning LED off in 5 seconds...");
      myTime1 = millis();            // recharge myTime1
      flag = true;                   //  Enable led off
    }
  }
  if (flag == true)                   // If button was pressed again
  {
    if (millis() - myTime1 > 5000)    // If 5 segs passed
    {
      digitalWrite(LED_PIN, LOW);
      LEDflag = 0;                    // toggle LED flag to 'off'
      flag = false;                   // Disable led off
    }
  }
  myPot();                            // Call old loop()
}
 //--------------------------------------------------
//  Code 2
char a = 2, b = 3, c = 4, d = 5, e = 6, f = 7, g = 8;
int hit = 0, mulai = 11, count = 0, pot = A0;
char digit1 = 9, digit2 = 10;
char no[10] = {
  0b0111111, //Number 0
  0b0000110, //Number 1
  0b1011011, //Number 2
  0b1001111, //Number 3
  0b1100110, //Number 4
  0b1101101, //Number 5
  0b1111101, //Number 6
  0b0000111, //Number 7
  0b1111111, //Number 8
  0b1101111, //Number 9
};
//--------------------------------------------------
void angka(int num) {
  digitalWrite(a, num & 0b0000001);
  digitalWrite(b, num & 0b0000010);
  digitalWrite(c, num & 0b0000100);
  digitalWrite(d, num & 0b0001000);
  digitalWrite(e, num & 0b0010000);
  digitalWrite(f, num & 0b0100000);
  digitalWrite(g, num & 0b1000000);
}
//--------------------------------------------------
void puluhan(int w) {
  digitalWrite(digit1, HIGH);
  digitalWrite(digit2, HIGH);
  angka(no[w]);
  digitalWrite(digit1, LOW);
  digitalWrite(digit2, HIGH);
  delay(3);
}
//--------------------------------------------------
void satuan(int x) {
  digitalWrite(digit1, HIGH);
  digitalWrite(digit2, HIGH);
  angka(no[x]);
  digitalWrite(digit1, HIGH);
  digitalWrite(digit2, LOW);
  delay(3);
}
//--------------------------------------------------
void mySet() {                  // It was setup()
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(pot, INPUT);
  pinMode(mulai, INPUT);
}
//--------------------------------------------------
void myPot() {                 // It was loop()
  hit = analogRead(pot);
  //Serial.println(hit);
  hit = map(hit, 0, 1023, 0, 9);
  //hit=constrain(hit, 0,9);

  int s1 = hit / 10;
  int s2 = hit % 10;
  puluhan(s1);
  satuan(s2);

  int start = digitalRead(mulai);
  if (start == HIGH) { //countdown
    for (count = hit; count >= 0; count--) {
      unsigned long startTime = millis();
      for (unsigned long elapsed = 0; elapsed < 800; elapsed = millis() - startTime) {
        int d1 = count / 10;
        int d2 = count % 10;
        puluhan(d1);
        satuan(d2);
      }
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.