2016年3月28日 星期一

RX1002 搭配arduino



左手   上下 : 油門  (第一通道)     pin 7

            左右 :  YAW (第四通道)    pin 4

 右手   上下 : PITCH (第三通道)    pin 5

             左右    ROLL (第二通道)   pin 6


接到  arduino






sample code 如下 :

#include <PinChangeInt.h>
#define THR_PIN  7
#define ROLL_PIN  6
#define PITCH_PIN  5
#define YAW_PIN  4  // PITH
#define SERIAL_BAND 9600
unsigned int thr_start_time=0, thr_time=0;
unsigned int yaw_start_time=0, yaw_time=0;
unsigned int roll_start_time=0, roll_time=0;
unsigned int pitch_start_time=0, pitch_time=0;
void setup()
{

   Serial.begin(SERIAL_BAND);
  // while (!Serial) {}
 
    Serial.println("Init done");
   pinMode(THR_PIN, INPUT);
   pinMode(YAW_PIN, INPUT);
   pinMode(PITCH_PIN, INPUT);
   pinMode(ROLL_PIN, INPUT);
   //digitalWrite(UPPER_LIMIT_PIN, HIGH);
   PCintPort::attachInterrupt(THR_PIN, THR_Function, CHANGE );
   PCintPort::attachInterrupt(YAW_PIN, YAW_Function, CHANGE );
   PCintPort::attachInterrupt(PITCH_PIN, PITCH_Function, CHANGE );
   PCintPort::attachInterrupt(ROLL_PIN, ROLL_Function, CHANGE );
}


void loop()
{
   while(Serial.available() > 0)
  {  
    char c = Serial.read();  // get it
     Serial.println(thr_time);
     Serial.println(yaw_time);
     Serial.println(pitch_time);
     Serial.println(roll_time);
  }
}

void  THR_Function()
{
    if(digitalRead(THR_PIN)==HIGH) //  rising
       thr_start_time = micros();
    else
          thr_time  = micros() - thr_start_time;
}

void  YAW_Function()
{
    if(digitalRead(YAW_PIN)==HIGH) //  rising
       yaw_start_time = micros();
    else
          yaw_time  = micros() - yaw_start_time;
}

void  PITCH_Function()
{
    if(digitalRead(PITCH_PIN)==HIGH) //  rising
       pitch_start_time = micros();
    else
          pitch_time  = micros() - pitch_start_time;
}

void  ROLL_Function()
{
    if(digitalRead(ROLL_PIN)==HIGH) //  rising
       roll_start_time = micros();
    else
          roll_time  = micros() - roll_start_time;
}

沒有留言:

張貼留言