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


hello,

i trying control 24v motor kaku (coco) 433mhz remote. operate motor use l298n controller.
it no problem control motor until combine encoder. when connect encoder shaft of engine can not turn off anymore!
in opinion looks interrupt problem can not solve it. thank you.

update: removing delay(1) out of encoderinterrupts solved problem. delay debouncing don't know if give new problem .. still appreciated. :)

my code:
code: [select]

#include <wire.h>
#include <newremotereceiver.h>

// encoderpins
enum pinassignments {
  motor1pina = 20,  
  motor1pinb = 21,   };

volatile unsigned int posmotor1 = 0;  // counter 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 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 debouncer

if ((adres==11427386) && (unit==0) && (status==1)){

// turn on motor  
serial.println("on");
digitalwrite(in1, low);
digitalwrite(in2, high);
// set speed 200 out of possible range 0~255.
analogwrite(ena, 150);}

// turn off motor  
if ((adres==11427386) && (unit==0) && (status==0)){
serial.println("uit");
digitalwrite(in1, low);
digitalwrite(in2, low);
}
  
if (lastposmotor1 != posmotor1) {
serial.print("index:");
serial.println(posmotor1);
lastposmotor1 = posmotor1;
}
}

// interrupt on changing state
void encodermot_1a(){

  // debounce
  if ( rotatingmotor1 ) delay (1);  // wait little until bouncing done
  // test transition, did things change?
  if( digitalread(motor1pina) != motor1_a_set ) {  // debounce once more
    motor1_a_set = !motor1_a_set;
  // adjust counter + if 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 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;
   break;
   case newremotecode::on:
   status = 1;
   break;
   case newremotecode::dim:
   status = 2;
   break;}
  dimstatus = (receivedcode.dimlevel);
  periode = (receivedcode.period);
}

you must never call delay in interrupt routine, deadlocks system completely.

waiting in interrupt (except few microseconds) bad news everything.

with quadrature encoders don't need debounce since bounce pings , forth
along state cycle 1 place, don't lose track (be sure implement statemachine for
quadrature though).


Arduino Forum > Topics > Robotics (Moderator: fabioc84) > Problem control motor with encoder and 433mhz receiver (newremote)


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'