Hi everyone, i'm on a project where i need to control a 2 wheels robot using an IR remote.
My materials :
-Arduino Uno
-IR receiver
-IR remote
-2*FS90R servomotor (gnd, 5V and a pwm command pin )
I have issues using the Servo librairy. I have 3 commands for now :
-ON/OFF
-go to the left by a certain amount of degrees
-go to the right by a certain amount of degrees.
but for now, when i click on ON/OFF, i execute the next case without pressing "left" and two more click on it and i execute the other command "righ".
In addition, the wheels are both going in the same direction for left and right but one should be at 85 degrees and the other one at 95.
The goal for left and right orientation is to reduce the left wheel speed by a certain amount and increase the right wheel speed by the same amount.
My code is here :
#include <IRremote.h>
#include <Servo.h>
int RECV_PIN = 2;
int outputmotG = 9; //cmd moteur gauche
int outputmotD = 10; //cmd moteur droite)
int Vmot = 90;
int isON = 0;
int inaction = 0;
Servo ServoG;
Servo ServoD;
#define codeG 0x2FD //aller à gauche
#define codeD 0xC23D// aller à droite
#define ONOFF 0xA25D
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(outputmotG,OUTPUT);
pinMode(outputmotD,OUTPUT);
ServoG.attach(9);
ServoD.attach(10);
}
void loop(){
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case ONOFF :
if (isON == 0){
isON = 1;
ServoG.write(Vmot); //
ServoD.write(Vmot);}
else {
isON = 0;
}
case codeG :
inaction = 1;
if(isON == 1) { //
ServoD.write(Vmot*19/18);
ServoG.write(Vmot*17/18); //
Serial.println(Vmot*17/18);
delay(1000);
}
inaction = 0;
break;
case codeD :
inaction = 1;
if(isON == 1) { //
ServoD.write(Vmot*17/18); //
ServoG.write(Vmot*19/18);
delay(1000);
}
inaction = 0;
break;
}
ServoD.write(Vmot);
ServoG.write(Vmot);
Serial.println(value,HEX); // you can comment this line
irrecv.resume(); // Receive the next value
}
}
This is bound to cause unexpected results in integer math. Force the use of floating point math in the calculation (note: much slower calculation & much larger code):
Thank you wvmarle,
it seems like a part of my problem as been solved. But it's still strange because my 2 wheels are not independant. They are both heading in clockwise for left and in counter clockwise for the right button. But with values 85 for left wheel and 95 for right wheel they should go both side each others.
..., Seems like you're right!
They are inding both heading toward different direction. So the problem is solved. But the last strange thing is that when i click on ON/OFF, the program start by making the robot go forward it must be because of my switch() function. I'm going to fix this and go back to you with the final code.
Thanks again!