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;
};
}
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'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?
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.