Hi all,
I am trying to use interrupt function to copy signal from the input digital pin to the output digital pin of an Arduino. I am coding for my drone collision avoidance system. The input is from the radio receiver and the output is to the PPM encoder.
An infrared-red sensor is used to detect if there is an obstacle in front, should there be an obstacle, the Arduino will not copy the signal from the radio receiver to the PPM encoder, the Arduino will send a pulse to the PPM encoder to pitch up the drone to move it backwards.
The infrared-red sensor is GP2Y0A02YK0F. I got the distance equation from
The sensor uses analogRead function.
Below is the code with distance reading:
int FrontIRSensor = A0; //Infrared-red sensor on the front of the drone, pitch axis
float FrontIRSensorValue; //sensor detection ranges from 20cm to 150cm
const byte Encoder = 12;
const byte Receiver = 2;
int readsensor;
void setup()
{
pinMode (Encoder, OUTPUT);
pinMode (Receiver, INPUT);
}
void loop()
{
digitalWrite (Encoder, HIGH);
attachInterrupt (digitalPinToInterrupt(Receiver), off, LOW);
readsensor = analogRead (FrontIRSensor);
FrontIRSensorValue = 10650.08 * pow (readsensor, -0.935) - 10;
/*
if (FrontIRSensorValue <= 50)
{
detachInterrupt(digitalPinToInterrupt(Receiver));
digitalWrite(Encoder, HIGH);
delayMicroseconds(1400); //Pulse width of overwrite pitch output signal = 1.4 ms
digitalWrite(Encoder, LOW);
delayMicroseconds(12200); //Period of overwrite pitch output signal = 13.6 ms
}
*/
}
void off ()
{
digitalWrite (Encoder, LOW);
}
Below is the code without distance reading:
const byte Encoder = 12;
const byte Receiver = 2;
void setup()
{
pinMode (Encoder, OUTPUT);
pinMode (Receiver, INPUT);
}
void loop()
{
digitalWrite (Encoder, HIGH);
attachInterrupt (digitalPinToInterrupt(Receiver), off, LOW);
}
void off ()
{
digitalWrite (Encoder, LOW);
}
From the above 2 codes, the drone motors jitter with the distance reading code (first code), the drone motors do not jitter without the distance reading code (second code). I am not sure if the analogRead function is slowing down the interrupt function or did I code wrongly? I need help, please help me. Thanks.
Joe