G'day all,
I am trying to use an external interrupt to change a variable which effects a while statement. My way is not working and I am hoping some of you can assist. A quick example of what I have is detailed below. Please assume that the relevant interrupts have been enabled. I am getting the interrupt to change the variable to be = 1 so it is entering the interrupt and performing the variable change but it has no effect on the while statements in main().
int StartSW = 0; //If 0 the start switch has not been pressed
int main()
{
while (StartSW == 0)
{
"Do something"
}
While (StartSW == 1)
{
"Do Something"
}
ISR(INT0_vect)
{
StartSW = 1;
}
I thought this would go back to where it left off in the main while loop (StartSW == 0) and as it is no longer 0 it would drop out of this loop to the next loop (StartSW== 1) and do the things there.
G'day all,
I am trying to use an external interrupt to change a variable which effects a while statement. My way is not working and I am hoping some of you can assist. A quick example of what I have is detailed below. Please assume that the relevant interrupts have been enabled. I am getting the interrupt to change the variable to be = 1 so it is entering the interrupt and performing the variable change but it has no effect on the while statements in main().
Code: [Select]
int StartSW = 0; //If 0 the start switch has not been pressed
int main()
{
while (StartSW == 0)
{
"Do something"
}
While (StartSW == 1)
{
"Do Something"
}
ISR(INT0_vect)
{
StartSW = 1;
}
I thought this would go back to where it left off in the main while loop (StartSW == 0) and as it is no longer 0 it would drop out of this loop to the next loop (StartSW== 1) and do the things there.
Declare StartSW as volatile, and do not use int, try byte or bool instead. Most arduino's use processors that can only access a single byte at a time, anything larger (int is two bytes) risks having an interrupt occur between bytes, necessitating that interrupts be temporarily disabled.
Any time a variable is used in an ISR and in the main code it needs to be declared as volatile, to let the compiler know that the variable's value may be changed by something outside the main code. In your case, the compiler sees the initial value of 0, and nothing within your code that changes that value, so it will completely remove the 2nd while statement, and likely never even check the condition for the 1st while statement, because it will always be true.
This also is most certainly a XYproblem. Is the thing connected/setting INT0_vect very fast or very time critical? Is the answer is no, this is NOT the route you want to follow
Duplicate posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting will result in a timeout from the forum.
In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.