I don't quite understand what's going on with interrupts.
Here is my simple code:
#define PPM_PIN 3
void setup() {
Serial.begin(9600);
pinMode(PPM_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(PPM_PIN), readPPM, CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
}
void readPPM() {
// did the pin change to high or low?
if (digitalRead( PPM_PIN ) == HIGH) {
Serial.println("Changed to HIGH");
} else {
Serial.println("Changed to LOW");
}
I got nothing connected to the pin however I keep getting:
chnged to HIGH
Changed to LOW
Changed to HIGH
Changed to LOW
...
..and so on.
How can it be detecting changes if there is nothing connected to that pin?
juampe:
How can it be detecting changes if there is nothing connected to that pin?
That's easy:The pin as you use it (not connected, floating INPUT) IS changing randomly!
Learn about the tri-state character of microcontroller pins, learn about HIGH and LOW and pull-up and pull-down resistors!
Until you have learned something about the basic stuff, take this as granted:
If you set a microcontroller pin to INPUT and don't connect it to a defined voltage level (HIGH or LOW), the pin is "floating" and will become HIGH or LOW randomly.
BTW: Yiou better not use Serial.print() function in an interrupt handler or you might dead-lock your sketch in case the ISR is firing so fast that the Serial send buffer becomes filled completely and your program will do nothing until next reset because of the dead-lock condition:
At least one character from the send buffer must beactually sent before you can put another chacter in
While the ISR handler is active, all interrupts are blocked, so that no character can be sent.
So Serial.print(something) will wait forever while interrupts are disabled and send nothing
(Might be inappropriate with very slow interrupt rates, so that with slowly firing interrupts also Serial.print might work without dead-locking the further program execution.
How can it be detecting changes if there is nothing connected to that pin?
Stray capacitance is why it sees changes - typically if a neighbouring pin is changing it'll pick
that up easily, but stray mains voltages in the environment will also do it - hold a finger near
the pin even.
Input pins are extremely sensitive as they can take < 1pA