Stuck in while loop after executes interrupt..!

My attempt to build a push button counter with interrupt,It didnt work though, im not good at programming please help me out, XD

What im trying to do is , ill display the initial value at 25 on the lcd display
then when i push the interrupt button, i will move to the counter function
the counter will then calculate how many time i have push the button and add the value to the intial 25, x = pushcounter +25
then i will stop the counter function and return to the main loop function and displaying the new value after being added with pushcounter values

we will have three button for return, interupt, and counter

Cheers! Thanks in advance

I have run the circuit, it seems like im stuck within the counter funtion, i cannot get out of the while loop instruction!
it keeps counting forever when i push the button counter,
i tried push the interrupt button once more, it still did not go back to the main function,
:~

first_try_counter_added.ino (2.89 KB)

Please try to refrain from locking topics.

This looks suspicious...

void count() //counter for push button function, after interupt button pushed
{
while (digitalRead(buttonset==LOW)) //button set is use to return to main, indicates that if we have done with the counter, push buttonset to HIGH and back to main loop
{
buttonstate=digitalRead(button); //these are the button counter, have tried it separately and it works

that line indicates that, i will always run the loop if the setbuttton is not pressed to high,
once it is pressed, i will take that new value and return it to the main loop,

this is where i stuck at, when i pressed the setbutton , it did not end the loop, but it keeps in the loop and not return to the main function

that line indicates that, i will always run the loop if the setbuttton is not pressed to high,

No it doesn't.
Look carefully at the parentheses.

anepzamri:
that line indicates that, i will always run the loop if the setbuttton is not pressed to high,

No that line will digitalRead from the pin represented by the result of the comparison "buttonset==LOW"...

No doubt, this is not what you meant to do.

int buttonset= 8;          //set button
...
 while (digitalRead(buttonset==LOW))

LOW is defined as zero, so the comparison "(buttonset==LOW)" will always be false (0).

So you are really saying:

 while (digitalRead(0))

Pin 0 is the Rx line which is the "receive serial data" from the USB interface. It is normally high. Thus it will not leave that loop, unless you happen to send data to the Arduino from the USB or serial interface.

Perhaps you mean:

 while (digitalRead (buttonset) == LOW)

Putting in some spaces like that helps pick up these problems.

You are mixing an interrupt and polling in the same procedure.

Interrupts routines are typically short routines that handle some input that occurs at a rate that would not be processed adaquately by a polling routine. The main program loop is interrupted briefly to do some short routine that may be time critical.

For what you describe interrupts is not an appropriate use of interrupts. You could have the counter function as an interrupt function - whenever you push the count button the interrupt would occur and add 1 to a variable and then exit.

When you press the enter button the main routine would add the numbers together and display them.

The way your code is written you could just have a function that is called and you hold it in that function with the while loop. No need for interrupts.

Hi there, thanks for the reply,

These codes is actually only parts from what i really working on,

I actually working on a digital temperature monitor which will turn on different LED according to the threshold value we set.
The function counter and mode above are the one that ill be use to control the threshold value.
Eg.
[main loop function]

If (upper threshold) temperature>25 , i will turn on LED red, and if (lower threshold) temperature<20, i will turn on LED blue.

The above code only have one mode, that is upper threshold, thus i will need several modes to work on,
Im using interrupt for this purpose, to change from one mode to another, after all values have been set,
i will return to main loop and executes the main loop as usual,

Do you guys think i need interrupt for this purpose? Or i need just to use several functions? :slight_smile:

Thank you

Do you guys think i need interrupt for this purpose?

For what purpose? Interrupts are external events that happen that you want to respond to IMMEDIATELY.

Most likely, you do not need any interrupts or interrupt handlers. If you did, you'd know it.