Is there any method to run a bit of code when for example a digital pin gets high?
Could always just check in the loop if theres any changes since last... but is there a more efficient way?
Yep, that's exactly what interrupts are for.
Could always just check in the loop if theres any changes since last... but is there a more efficient way?
Some hardware or software needs to do the checking. Which depends on how long the pin to be checked will be in the altered state exactly how critical it is to respond immediately when the state changes, and exactly what it to be done when the state changes.
Since you've provided none of these details, you can't really expect any good answers.
PaulS:
Some hardware or software needs to do the checking. Which depends on how long the pin to be checked will be in the altered state exactly how critical it is to respond immediately when the state changes, and exactly what it to be done when the state changes.Since you've provided none of these details, you can't really expect any good answers.
Well I am going to check button presses, I bet the human finger cant press faster then the arduino can read so I don't think it'll be a problem
I bet the human finger cant press faster then the arduino can read so I don't think it'll be a problem
As long as there are no blocking functions, like delay(), being called, or not blocking constructs being used, like while(digitalRead(somePin) == LOW), you'd win that bet. Use any blocking function/construct, and you'd loose.
PaulS:
As long as there are no blocking functions, like delay(), being called, or not blocking constructs being used, like while(digitalRead(somePin) == LOW), you'd win that bet. Use any blocking function/construct, and you'd loose.
What do you mean I'll lose?
What PaulS means is that if you're in a delay() or get trapped in a while(), you can press your buttons until your fingers wear off and the Arduino won't be watching.
JimboZA:
What PaulS means is that if you're in a delay() or get trapped in a while(), you can press your buttons until your fingers wear off and the Arduino won't be watching.
Til my fingers wear off.. Lol.
But ok I'll watch out for that, thanks all!
What do you mean I'll lose?
You said:
I bet the human finger cant press faster then the arduino can read
Well, you are right. You can't press the switch faster then the Arduino can read.
That is not the same thing as pressing the switch faster than the Arduino does read.
void loop()
{
int val = digitalRead(6);
Serial.print("val = ");
Serial.println(val);
delay(60000);
}
You could press the switch attached to pin 6 a whole bunch of times before the Arduino got around to reading the pin again, because the code uses a blocking function.
void loop()
{
int val = digitalRead(6);
Serial.print("val = ");
Serial.println(val);
while(digitalRead(7) == HIGH)
{
// do nothing until the switch connected to pin 7 is pressed
}
}
This code uses a blocking construct, so you could press the switch connected to pin 6 millions of times before the Arduino got around to noticing it.
So, before you bet that the Arduino will read the switch more often than you can press it, you need to look at the code on the Arduino. Otherwise, you just might loose the bet.
Anyhoo, now that's all out the way, did you look at interrupts yet?
PaulS:
So, before you bet that the Arduino will read the switch more often than you can press it, you need to look at the code on the Arduino. Otherwise, you just might loose the bet.
Now I ran a test and it goes through the loop in about 20 milliseconds (with serial texting which slows it down). I think that will be enough because I think that the user will press the button at max like 10 times a second.
JimboZA:
Anyhoo, now that's all out the way, did you look at interrupts yet?
What do you mean?
I am having my code on a constant loop, I am not using interruptions, frankly I can't because of the goal of the project I think.
Long story short I have a button, when you press the button a number increments. I also have stuff that increments for you.
What do you mean?
What I meant was, did you look into interrupts like I mentioned in reply #1. They are arguably a more efficient way of doing things since your sketch can get on with its "real" work while keeping one ear on the doorbell so to speak.
JimboZA:
What I meant was, did you look into interrupts like I mentioned in reply #1. They are arguably a more efficient way of doing things since your sketch can get on with its "real" work while keeping one ear on the doorbell so to speak.
JimboZA:
Yep, that's exactly what interrupts are for.
If I understand it correctly the interrupt functionality for the Arduino UNO only works on pin 2 and 3. It's possible to mix stuff up to use interrupts but I think I'll stick to normal checking each time in the loop
If I understand it correctly the interrupt functionality for the Arduino UNO only works on pin 2 and 3
Those are just the simple external interrupts, but you've also got pin change interrupts.
One of the problems with using interrupts is, well, they interrupt things !
More often than not when dealing with human input such as pressing a button you can poll the button often enough so that an interrupt is not needed. In any case, an ISR should be short and execute in the minimum amount of time. Often all that is done in an ISR is to set a global flag to indicate that the interrupt has taken place. The flag variable is then used to trigger actions in the main loop() function so the actions will occur in the same timeframe as if the input had been polled in loop() although the interrupt could occur at any time, which may be what is needed.
Whether or not using an interrupt is appropriate is a matter of how quickly the input needs to be noticed rather than how quickly it needs to be acted upon as the amount of actions that can take place in an ISR is limited by time and the functions available/reasonable to be used in the ISR. No serial input or output is possible, for instance.