Programme LED to blink 4 times only upon button press.

Joined
Sep 2, 2023
Messages
2
Reaction score
0
Hello! Could someone please help me with this problem. I have a DIY project where I would like an LED to blink 4 times eachtime a trigger button is pressed. I am trying this in MPLAB X IDE for a PIC16F1507. Your help will be highly appreciated. Thanks.
 

Attachments

  • FB_IMG_16936963035219676.jpg
    FB_IMG_16936963035219676.jpg
    179.1 KB · Views: 7
Joined
Jul 4, 2023
Messages
366
Reaction score
41
I do not have access to MPLAB X IDE for a PIC16F1507, but e.g. in Arduino Uno this can be looks like that for example.

[ on-line-emulation ]
C++:
const byte BUTTON_PIN = 2;
const byte LEDS_PIN[7] = { 7, 8, 9, 10, 11, 12, 13 };

void setup() {
  for (byte led: LEDS_PIN)
    pinMode(led, OUTPUT);
  
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

byte buttonIsPressed = false;

void loop() {
  byte buttonCurrentState = digitalRead(BUTTON_PIN);

  if (buttonCurrentState == LOW && !buttonIsPressed) {
    buttonIsPressed = true;
    ledLightsBlink4Times();   
  } else {
    buttonIsPressed = false;
  }
}

void ledLightsBlink4Times() {
  for (byte loop=0; loop<4; loop++) {
    ledLightsSolid();
    if (delayLoop(150)) return;
    ledLightsOff();
    if (delayLoop(400)) return; 
  }   
}

void ledLightsOff() {
  for (byte led: LEDS_PIN)
    digitalWrite(led, LOW);   
}

void ledLightsSolid() {
  for (byte led: LEDS_PIN)
    digitalWrite(led, HIGH); 
}

boolean delayLoop(unsigned int delayInterval) {
  unsigned long startMillis = millis();

  do {
    //if (digitalRead(BUTTON_PIN) == LOW) return true;
  } while (millis() - startMillis < delayInterval);

  return false;
}

20230904_223755_edit_edit.gif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top