Go Down

Topic: quick help -- button pressing and timing (Read 1 time) previous topic - next topic

scales11

I did understand what he meant, I dealt with it a little when I coded in labview.  But you are correct, and I did not know the proper term.  I was thinking more along the lines of the button, ie. latched, etc.

Thanks, I may need to add that functionality into my code when it progresses.

daatse

#6
Jun 07, 2012, 05:36 pm Last Edit: Jun 07, 2012, 05:38 pm by daatse Reason: 1
dear scales,

---explanation why to use the rising edge/uprising flank---
when you use tact switches (switches where contact is made when pushed and lost when released) or any kind of singlepole toggle switch there is a problem you should watch out for. because you hold the switch at very least for 40-100 ms(random guess) and your program's looptime is much less. Your program will register the button you pushed only once, as if it was pushed many times in a row. this will result in that the action you wanted to link to the button will be executed many times. one of the solutions is to program your arduino so that the button will only be registerd as pushed when the in previous loop it wasn't pushed. thus when the INPUT rises from '0' to '1'. <--thats the rising flank for ya

Also i'm still not quite sure what the question is you were asking?!

and ofcourse a litle tip to close the deal:
i think this is much more logical (and probably faster) it also removes a neatness bug that when 2 opposite buttons are pushed the screen goes back and forth rapidly
Code: [Select]
void loop(){
//check for button press
    if (digitalRead(btnR)){/*change my menu*/}
    else if (digitalRead(btnL)){/*change my menu*/}
    else if (digitalRead(btnU)){/*change my variable*/}
    else if (digitalRead(btnD)){/*change my variable*/}
    update screen;

    if (/*variables are XX*/){/*run loop for 5 iterations*/}
}


sidenote: Arrch is right about the correct names, my excuses (i'm dutch and i used a litteral translation)

scales11

Thanks Daatse,
I was able to get some response from my buttons with my initial code, though I did have the problem of dealing with one button press being registered as many as you described. My question was "Am I dealing with button presses in a proper and efficient way".  It has now become clear that I was not.

I think you are making a good point bringing up the latched issue (you call it flanking).  I think I need to incorporate that into my code.  Shall I look for a library, or function to use?  Perhaps I will make one.  I do not know if this will change things, but I may want to use a rotary encoder rather than buttons in my final design, will that still present the latching/flanking issue?

Also, is it a problem to refresh the screen that many times?  At the moment I am using the 16x2 LCD, but I may move to a few alphanumeric segmented displays.

daatse

I think you can pull it of by yourself
the code that Arrch provided will do the trick but when using more then one button u must use array's to keep everything neat
Code: [Select]

#define NOB 4 // number of buttons

boolean currentReading;
boolean status[NOB];

void loop() {
for(int i = 1; i++;  i<NOB;){

int currentReading = digitalRead(i); // only use this when using pin 1 to 4 when using for example pin9 to 13 use digitalRead((i+9));
if ((status[i]==LOW)&&(currentReading==HIGH)) {
//the behavior that that switch schould provoke
}
status[i] == currentReading;
}
}

something like the above, the above code is only to help you it will probably not work

also that much refreshes are not really a problem but when holding 2 buttons it will rapidly show oneframe and then a other (much like nowadays videoclips)

Arrch

You can also use an array of function pointers to keep the code down as well. For example:

Code: [Select]
void btn1Action() { // stuff }
void btn2Action() { // stuff }
void btn3Action() { // stuff }
void btn4Action() { // stuff }

void (*btnActions)[4]() = { btn1Action, btn2Action, btn3Action, btn4Action };


This would allow a single for loop to check for the edge, run the function if it's found, and update the last state variable.

Go Up