Hi!.
I am making a motor positioner with a arduino mega 2560, the motor is a 36 volt actuator, I use a mosfet to activate it and a relay to change the direction of rotation (the electronics have all the protections, diodes, optocouplers, etc).
The actuator have a reed switch to count pulses.
The problem is that the digitalWrite instruction inside the interrupt never executes.
Test code I did:
void setup() {
pinMode(7,OUTPUT); //motor
pinMode(26,OUTPUT); // direction
pinMode(20,INPUT); //reed switch for count pulses
pinMode(14,INPUT_PULLUP); //button to activate
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(20), sumarPulsosM1, CHANGE);
}
int boton = 0;
void sumarPulsosM1() {
digitalWrite(7, LOW);
}
void loop() {
boton = digitalRead(14);
if (boton == 0){
Serial.println("pulso");
digitalWrite(7,HIGH);
delay(100);
}
}
The code works, But the pin does not change state when the digitalWrite statement is executed within the interruption, it seems that the interruption is executed 2 or 3 more times and there if the pin takes the value 0.
The problem is more evident if within the interruption I do this:
void sumarPulsosM1() {
status = false; //FLAG FOR TEST
digitalWrite(7, LOW);
detachInterrupt(digitalPinToInterrupt(20));
}
In that case, the flag is set to false, but the pin is still high and because the interrupt is not executed again because it is deactivated, the pin never changes state.
The pulses approximately have a frequency between 12 and 16 hz.
This happens always when the interrupt is in CHANGE mode, if I use RISING mode sometimes happens
I already tried several alternatives to digitalWrite:
digitalWriteFast -> not work
digitalWriteFastinterruptSafe -> not work
bitClear -> not work
sorry for my English
Thank you