Problem control motor with encoder and 433mhz receiver (newremote)


I am trying to control a 24v motor with a kaku (coco) 433mhz remote. To operate the motor i use a l298n controller.
It is no problem to control the motor until i combine it with an encoder. When i connect the encoder to the shaft of the engine i can not turn it off anymore!
In my opinion it looks like a interrupt problem but i can not solve it. Thank you.

UPDATE: removing the delay(1) out of the encoderinterrupts solved the problem. The delay is for debouncing so i don’t know if this will give a new problem … Any help still appreciated. :slight_smile:

My code:

#include <Wire.h> 
#include <NewRemoteReceiver.h>

// Encoderpins
enum PinAssignments {
  motor1PinA = 20,   
  motor1PinB = 21,   };

volatile unsigned int PosMotor1 = 0;  // a counter for the dial
unsigned int lastPosMotor1 = 1;   // change management
static boolean rotatingMotor1=false;      
// interrupt service routine vars
boolean motor1_A_set = false;              
boolean motor1_B_set = false;

// connect motor controller pins to Arduino digital pins
int enA = 5; //ENA controller 1 (pwm pin Due)
int in1 = 6;   //in1 controller 1
int in2 = 7;   //in2 controller 1

// variabelen ontvangst 433 mhz zenders
long Adres;
 byte Groep; 
  byte Unit;
   byte Status;
    byte Dimstatus;
     int Periode;

void setup() {

NewRemoteReceiver::init(0, 2, Ontvanger); //interrupt pin2

pinMode(motor1PinA, INPUT), pinMode(motor1PinB, INPUT);   //encoder

// turn on pullup resistors
digitalWrite(motor1PinA, HIGH), digitalWrite(motor1PinB, HIGH); //encoder pull-up resistors
attachInterrupt(2, EncoderMot_1A, CHANGE);     // encoder pin on interrupt 0 (pin 21) 
attachInterrupt(3, EncoderMot_1B, CHANGE);     // encoder pin on interrupt 1 (pin 20)
Serial.begin(9600);  // output

void loop() { 
rotatingMotor1 = true;  // reset the debouncer

if ((Adres==11427386) && (Unit==0) && (Status==1)){

// turn on motor A  
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// set speed to 200 out of possible range 0~255. 
analogWrite(enA, 150);}

// turn off motor A  
if ((Adres==11427386) && (Unit==0) && (Status==0)){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
if (lastPosMotor1 != PosMotor1) {
lastPosMotor1 = PosMotor1;

// Interrupt on A changing state
void EncoderMot_1A(){

  // debounce
  if ( rotatingMotor1 ) delay (1);  // wait a little until the bouncing is done
  // Test transition, did things really change? 
  if( digitalRead(motor1PinA) != motor1_A_set ) {  // debounce once more
    motor1_A_set = !motor1_A_set;
  // adjust counter + if A leads B
    if ( motor1_A_set && !motor1_B_set ) 
      if (PosMotor1<200){PosMotor1 += 1;}
       rotatingMotor1 = false;  // no more debouncing until loop() hits again

// Interrupt on B changing state, same as A above
void EncoderMot_1B(){
  if ( rotatingMotor1 ) delay (1);
  if( digitalRead(motor1PinB) != motor1_B_set ) {
    motor1_B_set = !motor1_B_set;
    //  adjust counter - 1 if B leads A
    if( motor1_B_set && !motor1_A_set ) 
      if (PosMotor1>0){PosMotor1 -= 1;}
       rotatingMotor1 = false;

void Ontvanger(NewRemoteCode receivedCode) {

  Adres = (receivedCode.address);
  if (receivedCode.groupBit) {
  Groep = (receivedCode.groupBit); }
  else {
  Unit = (receivedCode.unit); }
  switch (receivedCode.switchType) {
   case NewRemoteCode::off:
   Status = 0;
   case NewRemoteCode::on:
   Status = 1;
   case NewRemoteCode::dim:
   Status = 2;
  Dimstatus = (receivedCode.dimLevel);
  Periode = (receivedCode.period);

You must never call delay in an interrupt routine, it deadlocks the system completely.

Waiting in an interrupt (except for a few microseconds) is usually bad news for everything.

With quadrature encoders you don't need to debounce since a bounce only pings you back and forth
along the state cycle by one place, you don't lose track (be sure to fully implement the statemachine for
quadrature though).

Thank you Mark.
Everything is working ok now.