โค้ดนี้ เป็นโค้ดที่การใช้ interrupt กับ เซ็นเซอร์ 1 ตัว
#define MAX_DISTANCE_IN_MM (4000) // max. valid value for distance
#define DURATION_TO_DISTANCE(x) ((17*(x))/100) // usec -> mm.
const int ECHO_PIN = 2; // D2 pin (External Interrupt 0)
const int TRIG_PIN = 4; // D4 pin
const int LED_PIN = 13; // D13 pin
// global variables
volatile uint32_t tH, tL, pulse_width = 0;
void setup(){
pinMode( LED_PIN, OUTPUT );
digitalWrite( LED_PIN, LOW );
pinMode( ECHO_PIN, INPUT );
pinMode( TRIG_PIN, OUTPUT );
digitalWrite( TRIG_PIN, LOW );
// use the External Interrupt 0
attachInterrupt( 0, eint_isr, CHANGE ); // D2 pin (EINT0)
Serial.begin( 115200 ); // initialize the Serial port
delay( 1000 );
}
uint16_t dist_mm; // distance in mm.
char sbuf[32]; // used for sprintf()
uint16_t read_ultrasonic_sensor() {
digitalWrite( LED_PIN, HIGH );
digitalWrite( TRIG_PIN, HIGH );
delayMicroseconds( 12 );
digitalWrite( TRIG_PIN, LOW );
pulse_width = 0;
while (pulse_width == 0 ) {} // wait until pulse_width > 0
digitalWrite( LED_PIN, LOW );
return DURATION_TO_DISTANCE( pulse_width );
}
void loop() {
dist_mm = read_ultrasonic_sensor();
if ( dist_mm > MAX_DISTANCE_IN_MM ) {
Serial.println( "Out of range." );
} else {
sprintf( sbuf, "Distance: %d. cm", (dist_mm/10), (dist_mm) );
Serial.println( sbuf );
}
delay( 200 );
}
volatile uint32_t timestamp;
void eint_isr() { // ISR for Ext. Interrupt
timestamp = micros(); // read the timestamp (in microseconds)
if ( digitalRead( ECHO_PIN ) ) { // HIGH
tH = timestamp;
} else { // LOW
tL = timestamp;
pulse_width = (tL-tH);
}
}
แล้วถ้าผมมี เซ็นเซอร์ hc sr04 สัก 8 ตัว ผมจะเขียน interrupt ยังไงครับ
อยากถามพี่ๆที่รู้เรื่อง arduino หน่อยครับ เกี่ยวกับการใช้ interrupt
#define MAX_DISTANCE_IN_MM (4000) // max. valid value for distance
#define DURATION_TO_DISTANCE(x) ((17*(x))/100) // usec -> mm.
const int ECHO_PIN = 2; // D2 pin (External Interrupt 0)
const int TRIG_PIN = 4; // D4 pin
const int LED_PIN = 13; // D13 pin
// global variables
volatile uint32_t tH, tL, pulse_width = 0;
void setup(){
pinMode( LED_PIN, OUTPUT );
digitalWrite( LED_PIN, LOW );
pinMode( ECHO_PIN, INPUT );
pinMode( TRIG_PIN, OUTPUT );
digitalWrite( TRIG_PIN, LOW );
// use the External Interrupt 0
attachInterrupt( 0, eint_isr, CHANGE ); // D2 pin (EINT0)
Serial.begin( 115200 ); // initialize the Serial port
delay( 1000 );
}
uint16_t dist_mm; // distance in mm.
char sbuf[32]; // used for sprintf()
uint16_t read_ultrasonic_sensor() {
digitalWrite( LED_PIN, HIGH );
digitalWrite( TRIG_PIN, HIGH );
delayMicroseconds( 12 );
digitalWrite( TRIG_PIN, LOW );
pulse_width = 0;
while (pulse_width == 0 ) {} // wait until pulse_width > 0
digitalWrite( LED_PIN, LOW );
return DURATION_TO_DISTANCE( pulse_width );
}
void loop() {
dist_mm = read_ultrasonic_sensor();
if ( dist_mm > MAX_DISTANCE_IN_MM ) {
Serial.println( "Out of range." );
} else {
sprintf( sbuf, "Distance: %d. cm", (dist_mm/10), (dist_mm) );
Serial.println( sbuf );
}
delay( 200 );
}
volatile uint32_t timestamp;
void eint_isr() { // ISR for Ext. Interrupt
timestamp = micros(); // read the timestamp (in microseconds)
if ( digitalRead( ECHO_PIN ) ) { // HIGH
tH = timestamp;
} else { // LOW
tL = timestamp;
pulse_width = (tL-tH);
}
}
แล้วถ้าผมมี เซ็นเซอร์ hc sr04 สัก 8 ตัว ผมจะเขียน interrupt ยังไงครับ