मैं एक अल्ट्रासोनिक दूरी सेंसर के लिए एक साधारण लाइब्रेरी लिख रहा हूं और सोचा कि मैं इंटरप्ट्स का उपयोग करने का प्रयास करूंगा।लाइब्रेरी में attachInterrupt का उपयोग नहीं कर सकता
हालांकि मैं अपने कार्यों को attachCallback
विधि में ठीक से सेट नहीं कर सकता।
मुझे HCSR04Interrupt::echoHigh()
और HCSR04Interrupt::echoLow()
कहा जाता है जब पिन क्रमशः उच्च और निम्न हो जाता है।
मैंने इसका कोई फायदा नहीं हुआ है। Ardiuno आईडीई कहते हैं निम्नलिखित:
#ifndef _HCSR04Interrupt_
#define _HCSR04Interrupt_
#include "Arduino.h"
#define HCSR04_CM_FACTOR 58.0
#define HCSR04_IN_FACTOR 148.0
#define HCSR04_CM_MODE 0
#define HCSR04_IN_MODE 1
class HCSR04Interrupt {
public:
double distance;
HCSR04Interrupt(int trigger_pin, int echo_pin, void (*callback)());
void setUnits(int units);
void getDistance();
private:
int _trigger_pin;
int _echo_pin;
int _units;
unsigned long _micros_start;
void (*_callback)();
void initialize();
void echoHigh();
void echoLow();
};
#endif
और मेरे कार्यान्वयन (पूरा नहीं कर सकते, क्योंकि मैं अतीत attachInterrupt कदम मिल):
./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp: In member function 'void HCSR04Interrupt::getDistance()':
./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp:31: error: argument of type 'void (HCSR04Interrupt::)()' does not match 'void (*)()'
./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp: In member function 'void HCSR04Interrupt::echoHigh()':
./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp:47: error: argument of type 'void (HCSR04Interrupt::)()' does not match 'void (*)()'
यहाँ मेरी शीर्ष लेख है
#include "Arduino.h"
#include "HCSR04Interrupt.h"
HCSR04Interrupt::HCSR04Interrupt(int trigger_pin, int echo_pin, void (*callback)()) {
_trigger_pin = trigger_pin;
_echo_pin = echo_pin;
_callback = callback;
initialize();
}
void HCSR04Interrupt::setUnits(int units) {
_units = units;
}
void HCSR04Interrupt::initialize() {
pinMode(_trigger_pin, OUTPUT);
pinMode(_echo_pin, INPUT);
digitalWrite(_trigger_pin, LOW);
}
void HCSR04Interrupt::getDistance() {
//Listen for the RISING interrupt
attachInterrupt(_echo_pin - 2, echoHigh, RISING);
//The trigger pin should be pulled high,
digitalWrite(_trigger_pin, HIGH);
//for 10 us.
delayMicroseconds(20);
//Then reset it.
digitalWrite(_trigger_pin, LOW);
}
void HCSR04Interrupt::echoHigh() {
_micros_start = micros();
detachInterrupt(_echo_pin - 2);
attachInterrupt(_echo_pin - 2, echoLow, FALLING);
}
void HCSR04Interrupt::echoLow() {
detachInterrupt(_echo_pin - 2);
unsigned long us = micros() - _micros_start;
distance = us;
(*_callback)();
}
attachInterrupt कि समारोह में यह बताते हैं होने के लिए आवश्यकता होती है स्थैतिक। और उसके बाद सभी सदस्य कार्य संलग्नक से ऊपर की ओर स्थिर होते हैं। मैंने यह सफलतापूर्वक किया है। – mpflaga