Hello everyone,
I am trying to convert iBus signal to PWM using Arduino. I have got the code and it is working as intended. I am using 8 channels which are mapped to pin 2 to 9 on Arduino Nano. The values are mapped in between 1000-2000ms. I am using Flysky iA6B receiver its iBus pin is connected to RX on Nano.
What I want now is, when channel 5 is less than 1200 (ch_width_5 < 1200) the pulse of channel 8 should change between 1000-2000ms as per throttle input from TX else it should remain 1000ms.
What change do I need to make in the sketch?
Here's the code
#include <string.h>
#define IBUS_BUFFSIZE 32 // Max iBus packet size (2 byte header, 14 channels x 2 bytes, 2 byte checksum)
#define IBUS_MAXCHANNELS 8 // My TX only has 10 channels, no point in polling the rest
#include <Servo.h>
static uint8_t ibusIndex = 0;
static uint8_t ibus[IBUS_BUFFSIZE] = {0};
static uint16_t rcValue[IBUS_MAXCHANNELS];
static boolean rxFrameDone;
int ch_width_1;
int ch_width_2;
int ch_width_3;
int ch_width_4;
int ch_width_5;
int ch_width_6;
int ch_width_7;
int ch_width_8;
Servo ch1;
Servo ch2;
Servo ch3;
Servo ch4;
Servo ch5;
Servo ch6;
Servo ch7;
Servo ch8;
void setup()
{
Serial.begin(115200);
Serial.println("setup done!");
ch1.attach(2);
ch2.attach(3);
ch3.attach(4);
ch4.attach(5);
ch5.attach(6);
ch6.attach(7);
ch7.attach(8);
ch8.attach(9);
}
void loop()
{
readRx();
}
void readRx()
{
rxFrameDone = false;
if (Serial.available())
{
uint8_t val = Serial.read();
// Look for 0x2040 as start of packet
if (ibusIndex == 0 && val != 0x20)
{
ibusIndex = 0;
return;
}
if (ibusIndex == 1 && val != 0x40)
{
ibusIndex = 0;
return;
}
if (ibusIndex == IBUS_BUFFSIZE)
{
ibusIndex = 0;
int high=3;
int low=2;
for(int i=0;i<IBUS_MAXCHANNELS; i++)
{
//left shift away the first 8 bits of the first byte and add the whole value of the previous one
rcValue[i] = (ibus[high] << 8) + ibus[low];
high += 2;
low += 2;
}
ch_width_1 = map(rcValue[0], 1000, 2000, 1000, 2000);
Serial.print(ch_width_1);
Serial.print(" ");
ch1.writeMicroseconds(ch_width_1);
ch_width_6 = map(rcValue[0], 1000, 2000, 1000, 2000);
ch6.writeMicroseconds(ch_width_6);
ch_width_2 = map(rcValue[1], 1000, 2000, 1000, 2000);
Serial.print(ch_width_2);
Serial.print(" ");
ch2.writeMicroseconds(ch_width_2);
ch_width_7 = map(rcValue[1], 1000, 2000, 1000, 2000);
ch7.writeMicroseconds(ch_width_7);
ch_width_3 = map(rcValue[2], 1000, 2000, 1000, 2000);
Serial.print(ch_width_3);
Serial.print(" ");
ch3.writeMicroseconds(ch_width_3);
ch_width_4 = map(rcValue[3], 1000, 2000, 1000, 2000);
Serial.print(ch_width_4);
Serial.print(" ");
ch4.writeMicroseconds(ch_width_4);
ch_width_5 = map(rcValue[4], 1000, 2000, 1000, 2000);
Serial.print(ch_width_5);
Serial.print(" ");
ch5.writeMicroseconds(ch_width_5);
Serial.print(ch_width_6);
Serial.print(" ");
Serial.print(ch_width_7);
Serial.println(" ");
//if (ch_width_5 < 1200)
//{ch8.writeMicroseconds(ch_width_8);
//ch_width_8 = map(rcValue[2], 1000, 2000, 1000, 2000);
//Serial.print(ch_width_8);
//Serial.println(" ");
//ch8.writeMicroseconds(ch_width_8);
//}
rxFrameDone = true;
return;
}
else
{
ibus[ibusIndex] = val;
ibusIndex++;
}
}
}