Hello Im trying to do my 1st project so Im really new to this but I'm stuck on this one problem. Basically what I have is two different functions that transmit IR pulses on pin 3, one is really short and other one is very long code. What Im trying to achieve is use a switch / push button to switch back and forthbetween each code and have each one be able to repeat non stop, as in a loop.. What I tried to do was run my 1st code in loop, then call interrupt isr routine to run 2nd code and use goto statement to make that repeat non stop. While this does work the problem I have is I cant come back to my 1st code. If I don't use goto then it does come back to loop but I need it to keep repeating.
For this code I want only these 4 lines to keep repeating without checking for any conditions, flags, (that's why I used interrupt) because if I add more line(s) to this it disturbs the timing and it affects the IR led range. The other code also emits IR light but its very long, so how can I make both these to loop separately depending on switch position?? It will be great if someone can post a code for example.
I'll look into how millis() or micros() is used, but I still need a way to switch between both codes.
Using miilis() or micros() for timing allows you to manage the timing without blocking code execution so that you can check the state of an input pin very frequently and execute different code when the state changes.
kashi786:
Thanks for replying, I'll look into how millis() or micros() is used, but I still need a way to switch between both codes.
Using millis() for non-blocking timing will facilitate that.
Every iteration through loop() check the button. Something like this
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != prevButtonState) {
if (buttonState == HIGH and chosenFunction == 'B') {
chosenFunction = 'A';
}
else if (buttonState == HIGH and chosenFunction == 'A') {
chosenFunction = 'B';
}
}
prevButtonState = buttonState;
if (chosenFunction == 'A) {
// call one function
}
else {
// call the other function
}
}
ok i get it now, thanks guys for explaining, I will try to test my code using millis or micros... I guess if I can continously listen for switch, then i don't have to use interrupt also.