Hi, I'm currently working on a project that I am going to be controlling with a FlySky FS-I6 controller. I currently have the receiver hooked up to a small tank drive bot for testing purposes. The data transfer works fine once connected. However, it does take the transmitter a few seconds to connect and in that time the Arduino seems to be reading junk data and runs the motors at random speeds most commonly seeming like max speed. This is not an issue for the small-scale bot but would be when I attempt to use this on my full-sized project so I attempted to use delay() and then millis() to create a wait in my code to give the transmitter time to connect. These functions however just seem to break the code if they are active. Does using IBUS for communication break these functions somehow? If so, are there any alternative solutions to this problem? Thanks.
#include <IBusBM.h>
#include <AFMotor.h>
IBusBM IBus;
double ly = 0;
double lx = 0;
double ry = 0;
double rx = 0;
double ld = 0;
double rd = 0;
double leftMotorDmd;
double rightMotorDmd;
double startTime;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
void setup() {
delay(3000);
IBus.begin(Serial);
//startTime = millis();
}
void loop() {
ly = (double)map(IBus.readChannel(2), 1000, 2000, -1024, 1024) / (double)1024;
lx = (double)map(IBus.readChannel(3), 1000, 2000, -1024, 1024) / (double)1024;
ry = (double)map(IBus.readChannel(1), 1000, 2000, -1024, 1024) / (double)1024;
rx = (double)map(IBus.readChannel(0), 1000, 2000, -1024, 1024) / (double)1024;
ld = (double)map(IBus.readChannel(4), 1000, 2000, 0, 1024) / (double)1024;
rd = (double)map(IBus.readChannel(5), 1000, 2000, 0, 1024) / (double)1024;
leftMotorDmd = ld * 255 * (ry + lx);
rightMotorDmd = ld * 255 * (ry - lx);
//if(millis() > startTime + 3000){
runMotorDmd();
//}
}
void runMotorDmd(){
int d;
if(leftMotorDmd < 0){
double dmd;
d = 2;
dmd = leftMotorDmd * -1.00;
motor2.run(d);
motor2.setSpeed(dmd);
}else{
double dmd;
d = 1;
dmd = leftMotorDmd;
motor2.run(d);
motor2.setSpeed(dmd);
}
if(rightMotorDmd < 0){
double dmd;
d = 2;
dmd = rightMotorDmd * -1.00;
motor1.run(d);
motor1.setSpeed(dmd);
}else{
double dmd;
d = 1;
dmd = rightMotorDmd;
motor1.run(d);
motor1.setSpeed(dmd);
}
}
type or paste code here
type or paste code here