Hi,
this is my code
int cycle = 1;
int inPin = 3;
void setup() {
Serial.begin(9600);
Serial.println("Booting...");
delay(10000);
attachInterrupt(inPin, sayHello, HIGH);
}
void sayHello() {
Serial.println("Hello!!");
cycle = 0;
}
void loop() {
cycle++;
}
I want connect 3.3v pin on interrupt pin for a small example, but it doesn't work!!!
Don't do Serial prints in an interrupt routine. Serial uses interrupts.
Ok but with this?
float cycle = 0;
int inPin = 3;
String msg = "void";
void setup() {
Serial.begin(9600);
Serial.println("Booting...");
delay(5000);
attachInterrupt(inPin, sayHello, RISING);
}
void sayHello() {
cycle++;
msg += cycle;
}
void loop() {
Serial.println(msg);
}
Output is ever: void1.00
The ISR is called one time at setup, and the output is constant.
inPin = 3 --> Pin number 3 of DUE pinout schematics?
Tnx
On the Uno and many other Arduino variants, interrupt 0 is Arduino pin 2. Look on the pinout diagram for INT0 next to a pin.
I forget how it works on the Due as my current Due projects aren't using interrupts. This stuff is all in the documentation.
The documentation says that every IO pins they can be used as interruptions. Can you post me a simple sketch for Uno?
tnx
I tried this sketch:
float cycle = 0;
int inPin = 22;
void setup() {
Serial.begin(9600);
Serial.println("Booting...");
Serial.print("Valore pin ");
Serial.println(digitalRead(inPin));
delay(1000);
pinMode(inPin, OUTPUT);
Serial.print("Valore pin ");
Serial.println(digitalRead(inPin));
attachInterrupt(inPin, sayHello, FALLING);
delay(3000);
}
void sayHello() {
cycle++;
}
void loop() {
Serial.print("cycle: ");
Serial.println(cycle);
digitalWrite(inPin, HIGH);
Serial.print("HIGH: ");
Serial.println(digitalRead(inPin));
digitalWrite(inPin, LOW);
Serial.print("LOW: ");
Serial.println(digitalRead(inPin));
}
The output is the same:
HIGH: 1
LOW: 0
cycle: 1.00 (becomes 1 at the first round and remains fixed)
Any condition HIGH, LOW, RISING, FALLING the ISR is called one time...
Here's a simple example that works on an UNO, although it didn't do what I needed for the application I was working on at the time.
/*
Instead of using the sequential function PulseIn(),
try using interrupts to do the same thing without blocking the rest
of the main loop from running.
*Measures the time the pin stays high (internal pullup)*
Reads a digital input on pin 2, prints the result to the serial monitor
The minimum duration this can measure is 12 microseconds.
Seems to have a resolution of 4 microseconds.
With the US Digital MA3 encoder, this reduces the 10-bit
resolution (0-1023) to 8-bit (3-255 effectively.)
PulseIn can measure 1 microsecond.
The function name ISR seems to be a reserved word?
Yes, it's a macro that creates ISRs. See the Timer1 version
of this sketch for an example.
*/
// digital pin 2 has an MA3 attached to it. Give it a name:
int pushButton = 2;
// Arduino Pin#2 is interrupt #0 (INT0). Other pins can be seen on the
// harware pin map: http://arduino.cc/en/Hacking/Atmega168Hardware
int IntNum = 0;
//the result will be stored in this global variable
//make it volatile so the compiler doesn't optimise away
//the updates from the interrupt routine
volatile unsigned long buttonDur;
volatile unsigned long buttonUp;
unsigned long LastTime;
void myISR() {
//Called when the value on the pin changes
//was it going high or low?
if (digitalRead(pushButton) == LOW) {
//We have reached the end of a desired pulse
//record the duration
buttonDur = micros() - LastTime;
LastTime = micros();
} else {
//this is the start of a desired pulse
//reset the start-time
buttonUp = micros() - LastTime;
LastTime = micros();
}
}
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT_PULLUP);
//Attach our interrupt function
attachInterrupt(IntNum, myISR, CHANGE);
}
// the loop routine runs over and over again forever:
void loop() {
// print out the duration of the most recent button push:
Serial.print("Down:");
Serial.print(buttonDur);
if (buttonDur < 10) Serial.print(" ");
if (buttonDur < 100) Serial.print(" ");
if (buttonDur < 1000) Serial.print(" ");
Serial.print(" Up:");
Serial.print(buttonUp);
if (buttonUp < 10) Serial.print(" ");
if (buttonUp < 100) Serial.print(" ");
if (buttonUp < 1000) Serial.print(" ");
Serial.print(" Calc:");
unsigned long Calc = ((buttonDur*4098)/(buttonDur+buttonUp))-1;
if (Calc > 4095) Calc = 4095;
Serial.print(Calc);
Serial.println();
delay(100); //wait a tenth of a second
//Result: as expected, resolution is poor. So poor that it can miss the smallest pulses and glitch near zero
//It jumps from a calculated output of 11 to 4084 and between those numbers, when it's failing to catch
//the small ups or downs, it can show both at 11, which produces a calculation for the middle of the range.
//If we kept this version, we would have to put in a guard that Up+Down is greater than some minimum,
//otherwise return zero as the calculated position.
}
Now works!!!
volatile int cycle = 0;
int inPin = 22;
void setup() {
Serial.begin(9600);
Serial.println("Booting...");
Serial.print("Valore pin ");
Serial.println(digitalRead(inPin));
delay(1000);
pinMode(inPin, INPUT);
Serial.print("Valore pin ");
Serial.println(digitalRead(inPin));
attachInterrupt(inPin, sayHello, CHANGE);
delay(3000);
}
void sayHello() {
cycle++;
}
void loop() {
if(cycle>1){
Serial.print("cycle: ");
Serial.println(cycle);
Serial.print("STATE: ");
Serial.println(digitalRead(inPin));
}
}
cycle wasn't declared volatile!!
Why the input pin#22 is always HIGH? How can I read the high values, if these are the default? I would expect a low value...
If i don't connect the pin its value is always high, right?
The value of an unconnected pin is indeterminate. Due to static electricity it can be high or low, depending on how you breathe on it.
If you enable the internal pullup with pinMode(MY_PIN, INPUT_PULLUP)
then it will be HIGH when unconnected.