I'm making a program for the msp430 using energia launchpad, the first code that I had is the following:
// display flag
boolean flag_display = false;
void setup() {
// define ISR to activate the display
attachInterrupt(PUSH2, display_ISR, CHANGE);
}
void display_ISR() {
clean_leds();
flag_display = !flag_display;
}
void loop() {
if(flag_display == true) {
// prints the number 45 on the display
pick_digit(1);
pick_number(4);
delay(8);
pick_digit(2);
pick_number(5);
delay(8);
}
}
Basically the program presents the number 45 on the display every time the button PUSH2 is pressed and cleans the display every time the button is un-pressed, the problem is that sometimes (when the button is un-pressed and the program is still on the first delay) the number 5 in the second digit stays present, so I used the following to bypass this problem:
// display flag
boolean flag_display = false;
// delay flag from the display
boolean flag_delay = true;
void setup() {
// define ISR to activate the display
attachInterrupt(PUSH2, display_ISR, CHANGE);
}
void display_ISR() {
flag_display = !flag_display;
}
void loop() {
if(flag_display == true) {
// prints the number 45 on the display
pick_digit(1);
pick_number(4);
delay(8);
pick_digit(2);
pick_number(5);
delay(8);
// if the code runs till the end, un-flag the delay flag
flag_delay = false;
}
// Keeps cleaning the leds until the delayed code is over processing
if(flag_display == false && flag_delay == false) {
clean_leds();
flag_delay = !flag_delay;
}
}
But I read that over using flags in your program is a bad programming practice and my solution doesn't seem elegant nor power efficient, since the line:
flag_delay = false;
Is going to keep being processed every time the display is on, and the function clean_leds() is going to keep being processed until the delayed code is over.
Is there a more efficient way of using the ISRs and flags in this particular case? Thank you.
The clean_leds() function is has follows:
void clean_leds()
{
digitalWrite(P1_7, LOW);
digitalWrite(P1_6, LOW);
digitalWrite(P2_5, LOW);
digitalWrite(P2_4, LOW);
digitalWrite(P2_3, LOW);
digitalWrite(P2_2, LOW);
digitalWrite(P2_1, LOW);
}
Best Answer
I haven't worked with this processor yet but I am 100% sure there is some way to track the push button transition states
i.e un_pressed_button(low) - > pressed_button (high) transition and the vice versa
If you do not want to use the flag you need to check for the
pressed_button(high) -> un_pressed_button(low) transition on the push-button
sudo code would be like,
you will have to keep track of the states with two variables. OR setup two ISRS that fire on the two possible transitions. Hope this helps