Hello!!
i have problem with interrupt in my code because the interrupt works only if i use the serial.println(life). If i delete serial.println(life) in my code the interrupt seems not to work
Here is the code that i use:
#include <Servo.h>
int servoDxPin = 9;
int servoSxPin = 10;
Servo servoDx;
Servo servoSx;
int sensorTrigger = 11;
int sensorInput = 12;
int led = 13;
long duration = 0;
long distance = 0;
long rnd = 0;
volatile int life = 10;
void setup(){
pinMode(sensorInput,INPUT);
pinMode(sensorTrigger,OUTPUT);
servoDx.attach(servoDxPin);
servoSx.attach(servoSxPin);
randomSeed(analogRead(0));
attachInterrupt(0, lifeReduction, RISING);
pinMode(led,OUTPUT);
// Serial.begin(9600);
}
void loop(){
digitalWrite(sensorTrigger, HIGH); // set HIGH for 15us to trigger ranging
delayMicroseconds(15);
digitalWrite(sensorTrigger, LOW); // set pin LOW
duration = pulseIn(sensorInput, HIGH); // read in pulse length
distance = microsecondsToCentimeters(duration);
if (distance < 12){
servoDx.write(90);
servoSx.write(90);
rnd = random(10);
if (rnd>=5){
servoSx.write(180);
servoDx.write(180);
delay(1000);
servoSx.write(90);
servoDx.write(90);
}else{
servoDx.write(0);
servoSx.write(0);
delay(1000);
servoDx.write(90);
servoSx.write(90);
}
}else{
servoSx.write(180);
servoDx.write(0);
}
Serial.println(life); //<------------------------------------------------------------------------------
if(life <= 0){
digitalWrite(led,HIGH);
life = 10;
}
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
void lifeReduction(){
life--;
}

