RC control of dc motor

Hi ppl. i'm trying to control dc motor with a RC . i'm using a wiper motor. pololu 9a H bridge. Avago encoder(500 ppr). Hi tec flash 5 RC(7 channels). using channel 7 only. i've written the code but it didn;t respond. i know the pwm pin is not in use which is the first problem. but could anyone clarify if the code makes sense. i assumed 500 pulses is -360 degrees when i hold joysticks full left and +360 full right.

int A = 2; encoder A channel
int B = 4; Encoder b channel
int INa = 7;   H -bridge A Direction
int    INb = 8;  H - bridge B direction control
int RX = 0;  // analog pin used to connect the radio receiver.
int val;

void setup()   {                
  // initialize the digital pin :
  pinMode(A, INPUT); 
  pinMode(B, INPUT);
  pinMode(INa, OUTPUT);
  pinMode(INb, OUTPUT);     
}  


void BRAKEZ() {
      digitalWrite(INa, HIGH);  
      digitalWrite(INb, HIGH); 
// INa = HIGH
// INB = HIGH
}
int Position = 0;
void ENC_update(){ /* interrupt driven encoder update */
/* Static variables for position calculation */
static unsigned char state = 0xFF;
unsigned char new_state;
new_state = (digitalRead(A) << 1) | digitalRead(B); /* Read encoder state */
/* Update position value */
if (state!=new_state) {
switch (state) {
case 0x00:
if (new_state==0x01)
Position++;
else
Position--;
break;
case 0x01:
if (new_state==0x03)
Position++;
else
Position--;
break;
case 0x03:
if (new_state==0x02)
Position++;
else
Position--;
break;
case 0x02:
if (new_state==0x00)
Position++;
else
Position--;
break;
}
state = new_state;
}
}

void MOVES(int MOVE) {
  ENC_update;
    if (MOVE != Position) {
        if (MOVE > Position) {
            do  {
                digitalWrite(INa, LOW);  
                digitalWrite(INb, HIGH);
              //   REVERSE;
                 MOVE = MOVE - 1;
    }
while (MOVE != Position);

};

    }
else  {
       if (MOVE < Position) {
           do  {
               digitalWrite(INa, HIGH);      
               digitalWrite(INb, LOW);
                // FORWARD;
                 MOVE = MOVE + 1;
                     }
while (MOVE != Position);
 
};
     
    };

}

void loop(){
  int STEP;
  ENC_update;
  
  val = analogRead(RX);            // reads the value of the radio reciever (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 255); 
  STEP=val;

if (STEP = 127) {        // JOYSTICK CENTER
 MOVES(0);
 delay(30);
 BRAKEZ;
};


 if (STEP > 128 && STEP < 150 ) {                 // RIGHT JOYSTICK 
 MOVES(100);   // MOVES FORWARD TILL POSITION = 100;
 delay(30);
 BRAKEZ;
};
if (STEP > 151 && STEP < 175 ) {
 MOVES(200);   // MOVES FORWARD TILL POSITION = 200;
  delay(30);
 BRAKEZ;
};
if (STEP > 176 && STEP < 200 ) {
 MOVES(300);   // MOVES FORWARD TILL POSITION = 300;
  delay(30);
 BRAKEZ;
};
if (STEP > 201 && STEP < 225 ) {
 MOVES(400);   // MOVES FORWARD TILL POSITION = 400;
  delay(30);
 BRAKEZ;
};
if (STEP > 226 && STEP < 255 ) {
 MOVES(500);   // MOVES FORWARD TILL POSITION = 500;
  delay(30);
 BRAKEZ;
};



if (STEP > 101 && STEP < 126 ) {                 // LEFT JOYSTICK 
 MOVES(-100);   // MOVES FORWARD TILL POSITION = -100;
  delay(30);
 BRAKEZ;
};
if (STEP > 76 && STEP < 100 ) {
 MOVES(-200);   // MOVES FORWARD TILL POSITION = -200;
  delay(30);
 BRAKEZ;
};
if (STEP > 51 && STEP < 75 ) {
 MOVES(-300);   // MOVES FORWARD TILL POSITION = -300;
  delay(30);
 BRAKEZ;
};
if (STEP > 26 && STEP < 50 ) {
 MOVES(-400);   // MOVES FORWARD TILL POSITION = -400;
  delay(30);
 BRAKEZ;
};
if (STEP > 0 && STEP < 25 ) {
 MOVES(-500);   // MOVES FORWARD TILL POSITION = -500;
  delay(30);
 BRAKEZ;
};
}

Thanks
Any help is welcome.

  1. Go back and modify that last post. Highlight it all and hit the has key so it is in a proper box.

  2. Can you point out where in the code the interrupts from the encoder are attached to the encoder reading routine.

Hi der , i'm new to arduino programmingi and not too familar with interupts as i treated it as a function used in pascal programming. i did call it up in the main loop to get the position value. I want to replace that conplicated encoder interupt with a simpler one. one mistake is that the pwm pin was not in use therefore my motor didn't respond. I cannot test my code because i'm still awaiting arrival of ny own board s at the moment i'm stumped whether the code will work again.
Thanks

You need to tell the arduino to use interrupts in the setup not the main loop.
See this page:-
http://www.arduino.cc/en/Reference/AttachInterrupt

You've probably found out by now that his
BRAKEZ; doesn't do anything useful, though helpfully, the C compiler didn't tell you anything was wrong with it, because, well, there isn't.

If you want to call a function, you need parentheses:

 BRAKEZ ();

Could you maybe do something about the formatting, like click on Auto Format, or Copy for Forum in the IDE before posting?

I updated the code. Think this should work? :-/


int encoder0PinA =2; // encoder
int encoder0PinB =3;  // encoder

int INa = 7;   //H-bridge
int    INb = 8;  // h_bridge
int pwm = 5; // h - bridge

int pin = 12;  //RX pin
unsigned long DURATION;
int STEP;

volatile unsigned int encoder0Pos = 0; 

void setup() { 
  
  pinMode(INa, OUTPUT);
  pinMode(INb, OUTPUT);
  pinMode(pwm, OUTPUT);
  
    pinMode(pin, INPUT);

  pinMode(encoder0PinA, INPUT); //encoder
  pinMode(encoder0PinB, INPUT);  //encoder
  
  
// encoder pin on interrupt 0 (pin 2) 

  attachInterrupt(0, doEncoderA, CHANGE);

// encoder pin on interrupt 1 (pin 3) 

  attachInterrupt(1, doEncoderB, CHANGE);  

} 


void doEncoderA(){ 

  // look for a low-to-high on channel A
  if (digitalRead(encoder0PinA) == HIGH) { 

    // check channel B to see which way encoder is turning
    if (digitalRead(encoder0PinB) == LOW) {  
      encoder0Pos = encoder0Pos + 1;         // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
  }

  else   // must be a high-to-low edge on channel A                                       
  { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoder0PinB) == HIGH) {   
      encoder0Pos = encoder0Pos + 1;          // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }
  }
} 


void doEncoderB(){ 

  // look for a low-to-high on channel B
  if (digitalRead(encoder0PinB) == HIGH) {   

   // check channel A to see which way encoder is turning
    if (digitalRead(encoder0PinA) == HIGH) {  
      encoder0Pos = encoder0Pos + 1;         // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
  }

  // Look for a high-to-low on channel B

  else { 
    // check channel B to see which way encoder is turning  
    if (digitalRead(encoder0PinA) == LOW) {   
      encoder0Pos = encoder0Pos + 1;          // CW
    } 
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }
  }
} 



void MOVES(int MOVE) {

    if (MOVE != encoder0Pos) {
        if (MOVE > encoder0Pos) {
            do  {
                digitalWrite(INa, LOW);  
                digitalWrite(INb, HIGH);
              //   REVERSE;
                 MOVE = MOVE - 1;
    }
while (MOVE != encoder0Pos);

};

    }
else  {
       if (MOVE < encoder0Pos) {
           do  {
               digitalWrite(INa, HIGH);      
               digitalWrite(INb, LOW);
                // FORWARD;
                 MOVE = MOVE + 1;
                     }
while (MOVE != encoder0Pos);
 
};
     
    };

}

void BRAKEZ() {
      digitalWrite(INa, HIGH);  
      digitalWrite(INb, HIGH); 
// INa = HIGH
// INB = HIGH
}

void loop(){ //control loop

analogWrite(pwm, 255);

DURATION = pulseIn(pin, HIGH);
  STEP = map(DURATION, 1222, 1680, 0, 255); 
  
 if (STEP < 126 && STEP < 129 ) {                 // centre 
 MOVES(0);
 delay(30);
 BRAKEZ();
};


 if (STEP > 130 && STEP < 150 ) {                 // RIGHT movement of JOYSTICK 
 MOVES(100);   // MOVES FORWARD TILL POSITION = 100;
 delay(30);
 BRAKEZ();
};
if (STEP > 151 && STEP < 175 ) {
 MOVES(200);   // MOVES FORWARD TILL POSITION = 200;
  delay(30);
 BRAKEZ();
};
if (STEP > 176 && STEP < 200 ) {
 MOVES(300);   // MOVES FORWARD TILL POSITION = 300;
  delay(30);
 BRAKEZ();
};
if (STEP > 201 && STEP < 225 ) {
 MOVES(400);   // MOVES FORWARD TILL POSITION = 400;
  delay(30);
 BRAKEZ();
};
if (STEP > 226 && STEP < 255 ) {
 MOVES(500);   // MOVES FORWARD TILL POSITION = 500;
  delay(30);
 BRAKEZ();
};



if (STEP > 101 && STEP < 125 ) {                 // LEFT movement of JOYSTICK 
 MOVES(-100);   // MOVES FORWARD TILL POSITION = -100;
  delay(30);
 BRAKEZ();
};
if (STEP > 76 && STEP < 100 ) {
 MOVES(-200);   // MOVES FORWARD TILL POSITION = -200;
  delay(30);
 BRAKEZ();
};
if (STEP > 51 && STEP < 75 ) {
 MOVES(-300);   // MOVES FORWARD TILL POSITION = -300;
  delay(30);
 BRAKEZ();
};
if (STEP > 26 && STEP < 50 ) {
 MOVES(-400);   // MOVES FORWARD TILL POSITION = -400;
  delay(30);
 BRAKEZ();
};
if (STEP > 0 && STEP < 25 ) {
 MOVES(-500);   // MOVES FORWARD TILL POSITION = -500;
  delay(30);
 BRAKEZ();
};
};

You're the guy with the hardware - you tell us.

Ok sure but missing the main component of the entire project-Arduino board. Flight delivery delayd cos of volcano ash cloud....wil report results 4 this project soon.