

//this code is for the 6 channel transmitter

#include <Streaming.h>



#include <SerialPort.h>





SerialPort<1,128,128> port1;

SerialPort<2,128,128> port2;



const byte hexZero = 0x00;

uint8_t endianArray[14] = {1,0,3,2,5,4,7,6,9,8,11,10,13,12};



typedef union{

struct{

uint16_t aileron;

uint16_t flap;

uint16_t elevator;

uint16_t gear;

uint16_t rudder;

uint16_t unused;

uint16_t throttle;

}

variables;

byte buffer[14];

}

Spektrum_t;





Spektrum_t spektrum;



byte inByte;



int bufferIndex=0;

int readState = 0;

boolean dataReady = false;





void setup(){

port1.begin(115200);

port2.begin(115200);

}





void loop(){

while(port2.available() > 0){



inByte = port2.read();

switch (readState){

case 0:

if (inByte == hexZero || inByte == 0x2D || inByte == 0x5A || inByte == 0x87 || inByte == 0xB4 || inByte == 0xE1 || inByte == 0xFF){

readState = 1;

}

break;

case 1:

if (inByte == 0xA2){

readState = 2;

bufferIndex = 0;

}

break;

case 2:

if (bufferIndex % 2 == 0){



spektrum.buffer[endianArray[bufferIndex]] = inByte & 0x07;

}else{

spektrum.buffer[endianArray[bufferIndex]] = inByte;

}

bufferIndex++;

if(bufferIndex == 14){

readState = 0;

dataReady = true;

}

break;

default:

break;



}

}

if (dataReady == true){

dataReady = false;

port1 << "Time: " << millis() << "\r

";

port1 << "Aileron: "<< spektrum.variables.aileron << "\r

";

port1 <<"Flap: "<<spektrum.variables.flap<<"\r

";

port1 << "Elevator: "<<spektrum.variables.elevator<<"\r

";

port1 << "Gear: " <<spektrum.variables.gear<<"\r

";

port1 << "Rudder: " <<spektrum.variables.rudder<<"\r

";

port1 << "Empty channel: "<<spektrum.variables.unused<<"\r

";

port1 << "Throttle: " <<spektrum.variables.throttle << "\r

";

}



}

For my current project I wanted to use Spektrum serial. That is the signal that comes out of the little satellite receiver. DIY drones Mbed Desert RC and others had some great info on the subject. But clearly Spektrum has gotten wise to this and changed up the protocol. Reference pages:http://diydrones.com/profiles/blogs/24ghz-how-to-read-7-channels?id=705844%3ABlogPost%3A64228&page=3#commentshttp://www.desertrc.com/spektrum_protocol.htmhttp://mbed.org/users/offroad/notebook/spektrum-satellite-rx-driver/DIY and mbed both have a preamble of 0x0301. Desert RC found 0x039b. The preamble I found was 0x00A2. Unless I power cycled the transmitter. Than the first byte of the preamble changes. This was causing me to lose a large number of the data frames. So in the first state for parsing the values I have included all the preambles. Once I get my hands on a 7 channel reciever I am going to update the code. The 7 channel supposedly puts the channels in a different order. I also am going to try a JR satasatellite lite as well as one of the cheap hobby king knock offs. So I will update this post after all the hardware arrives and I have time to test it. So here is updated Spektrum serial handling for arduino. Be warned if you are going to use the code do a reality check and make sure your preamble is the same. And if you have a different preamble please post on this thread to let me know. Hope this helps: