x8R RX and arduino nano v3.0

144 views
Skip to first unread message

Marcelo Correa

unread,
Aug 24, 2014, 5:18:28 PM8/24/14
to arduino-pincha...@googlegroups.com
Hi There,
A Newbie question.
Just trying to interface one port of a FrSky X8R with an arduino Nano (see pictures)
Tried the sample (CaptureX8R.png) sketch without sucess.
Using the interrupt0 on pin2 works OK (CaptureWorking.png) in yellow stick up/down position.
and my setup picture.
How to debug this situation? Missing something?

regards and congrats for the hard work.
Marcelo
CaptureX8R.PNG
CaptureWorking.PNG
CapturePICT.PNG

Michael Schwager

unread,
Aug 24, 2014, 11:50:10 PM8/24/14
to arduino-pincha...@googlegroups.com
Hi,
Thanks for the props. So I have never heard of this device, and don't know what it does. Please explain that, and also explain what your numbers mean. What do you expect to see? How does our output differ from your expectations?

When you attachInterrupt(), your first argument should be the Arduino pin. What is the first argument that you gave to that function in your sketch? It looks like something else.

Remember that once you enter the ISR, the Global Interrupt Enable flag is disabled, so no more interrupts take place during the ISR's execution. Thus the variable that is incremented to keep track of your microseconds, is not then incremented. Know that the microseconds timer then is not precise.

The Global Interrupt Enable flag is enabled after you exit the ISR, and the microseconds counter will be updated as before.


--
You received this message because you are subscribed to the Google Groups "arduino-pinchangeint-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to arduino-pinchangeint...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-Mike Schwager

Marcelo Veras Correa

unread,
Aug 25, 2014, 4:07:45 PM8/25/14
to arduino-pincha...@googlegroups.com
Hi Michael,Thanks for you fast answer,

In my sketch I used pin2 (equivalent to int0) see
usage(http://arduino.cc/en/Reference/attachInterrupt &
http://arduino.cc/en/Main/arduinoBoardNano).
We need to use int number not pin number.. I think the problem lies in
this situation.
I have on the stick a range from 2000 to 1000 being the 1500 the RX
stick on the center.
I read about PWM encoding and its just the change of the duty cycle of
a signal. So I consider a valid input.
I understand that while "interrupted" everything is stopped. but in
this case I am able to detect the positions.

When using the code with the pinchangeint I only see the static value
"110-112" range (tried different pins and different channels).
With all channels I am able to move a servo without problem (pwm is working)

If I could help in anything more?!


Regards,
> You received this message because you are subscribed to a topic in the
> Google Groups "arduino-pinchangeint-discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/arduino-pinchangeint-discuss/spgI67zCTms/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
Marcelo Veras Correa
mvco...@gmail.com

Old Perl programmers never die, they just slowly iterate away
Perl Monk

Michael Schwager

unread,
Sep 5, 2014, 8:53:15 AM9/5/14
to arduino-pincha...@googlegroups.com
Sorry, Marcelo- I have been busy. Remember that on the Nano, you have two types of Interrupts: External interrupts (available on only 2 pins), and PinChangeInterrupts. In your second sketch, CaptureWorking, you are using External Interrupts. Remember that PCIntPort.attachInterrupt() != attachInterrupt().

In your CaptureX8R.PNG sketch, your interrupt_count[] array is not declared volatile. That might be an issue. If you move your input line from D3 to another pin, does that other pin show the same behavior? If you put a manual switch on the pin, do your results look correct? The sketch basically looks ok, so I'm not sure where the problem could lie.

Also, beware of what version of PinChangeInterrupt you use, because older versions had bugs. :-(

Marcelo Veras Correa

unread,
Sep 11, 2014, 7:38:05 PM9/11/14
to arduino-pincha...@googlegroups.com
Hi Michael,

I did everything again downloaded the version 2.3beta, tried with a
push button and is working perfect(You gave perfect suggestions).
My problem now is decoding the PWM signal sent from the RX to the nano.
Seeing the wave in a logic analyzer(picture) its length is 18ms and
the peak is between 1ms(min) and 2ms(max).
Doing "PCintPort::attachInterrupt(PIN2, &quicfunc, FALLING);" I need
to wait the 18ms for the next interrupt right?
There is a way to interrupt in the same pin when "rising" and
"falling" to only got the ON time(2ms max)??
I know there is a way to interrupt in a "change" in the pin state, the
problem is if I miss a change I will get the off value right?
Do you have an example sketch managing this situation?

Thanks again,
CaptureANALOG1.PNG

Marcelo Veras Correa

unread,
Sep 11, 2014, 9:29:32 PM9/11/14
to arduino-pincha...@googlegroups.com
Hi Michael,

Trying to code anyway, but retrieving garbage :(

Could you help-me in what I'm doing wrong?

regards,

#include <PinChangeInt.h>
#define PIN1 2
#define PIN2 3
volatile uint8_t latest_interrupted_pin;
volatile uint8_t interrupt_count[20]={0}; // 20 possible arduino pins
void quicfunc() {
  latest_interrupted_pin=PCintPort::arduinoPin;
  interrupt_count[latest_interrupted_pin]++;
};

void setup() {
  pinMode(PIN1, INPUT); digitalWrite(PIN1, LOW);
  PCintPort::attachInterrupt(PIN1, &quicfunc, CHANGE);
  pinMode(PIN2, INPUT); digitalWrite(PIN2, LOW);
  PCintPort::attachInterrupt(PIN2, &quicfunc, CHANGE);
  Serial.begin(115200);
  Serial.println(LOW);
}

uint8_t i;
void loop() {
  unsigned long startTime[20]={0}; // signal rising time array
  unsigned long onTime;            // period the signal stays on
  boolean edge = false;  // goes true when a rising edge is detected
  Serial.print("> ");
  delay(1000);
  for (i=0; i < 20; i++) { // for each available pin
    onTime = 0;
    boolean sig = digitalRead(i);
     if((sig == HIGH) && (edge == false)) { // rising edge detected and start time goes to array
        startTime[i] = millis();
        edge = true;
     } else {
       if(sig == LOW){
        onTime = micros() - startTime[i];
        startTime[i] = 0;
        edge = false;
       }
     }
     Serial.print(i);
     Serial.print(":");
     Serial.print(onTime);
     Serial.print(" ");
  }
  Serial.println();
}

Michael Schwager

unread,
Sep 12, 2014, 1:00:49 PM9/12/14
to arduino-pincha...@googlegroups.com
What do you mean by "retrieving garbage"? What do you see? What do you expect to see?

Marcelo Correa

unread,
Sep 13, 2014, 11:01:26 AM9/13/14
to arduino-pincha...@googlegroups.com
Hi Michael,

I was getting random numbers on the channels.
But I got it working today. Nothing as a weekend day :)   (open to suggestions to improve the code)
This new sketch grabs all 4 channels from a remote control TX/RX( FrySky TARANIS/X8R).
Next step is to interface with a H-Bridge chip (L293D/SN754410NE) and drive a toy car. 
I will post the final sketch here.
Regards,


#include <PinChangeInt.h>
// Detecting PWM signal's in arduino
struct sig {
  volatile boolean isOn;        // signal LOW->HIGH detected
  volatile unsigned long start; // time in microseconds when "isOn" was detected
  volatile int len;             // the length of the signal on HIGH (NOW-start)
};
typedef struct sig Sig;
Sig signals[20] = {0};
void setup() {
  Serial.begin(9600);
  PCintPort::attachInterrupt(3, &calcInput, CHANGE); // throttle (1ms)->2ms
  PCintPort::attachInterrupt(4, &calcInput, CHANGE); // ailerons 1ms<-(1.5ms)->2ms
  PCintPort::attachInterrupt(5, &calcInput, CHANGE); // elevator 1ms<-(1.5ms)->2ms
  PCintPort::attachInterrupt(6, &calcInput, CHANGE); // rudder   1ms<-(1.5ms)->2ms
}
void loop(){
  delay(1000);
  for(uint8_t pin=0; pin<20; pin++){
    if(signals[pin].isOn){
      printStruct(pin);
      signals[pin].isOn = false;
    }
  }
  Serial.println();
}
void calcInput(){
  int pin = PCintPort::arduinoPin;
  if(digitalRead(pin) == HIGH){
    signals[pin].start = micros();
  }else{
    if(signals[pin].start && (signals[pin].isOn == false)){
      signals[pin].len = (int)(micros() - signals[pin].start);
      signals[pin].start = 0;
      signals[pin].isOn = true;
    } 
  }  
}
void printStruct(int pin){
    Serial.print(pin);
    Serial.print(":");
    Serial.print(signals[pin].isOn);
    Serial.print(":");
    Serial.print(signals[pin].start);
    Serial.print(":");
    Serial.print(signals[pin].len);
    Serial.print(" ");
}


>> >>
>> >> For more options, visit https://groups.google.com/d/optout.
>> >
>> >
>> >
>> >
>> > --
>> > -Mike Schwager
>> >
>> > --
>> > You received this message because you are subscribed to a topic in the
>> > Google Groups "arduino-pinchangeint-discuss" group.
>> > To unsubscribe from this topic, visit
>> >
>> > https://groups.google.com/d/topic/arduino-pinchangeint-discuss/spgI67zCTms/unsubscribe.
>> > To unsubscribe from this group and all its topics, send an email to

>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>> Marcelo Veras Correa
>> --
>> You received this message because you are subscribed to the Google Groups
>> "arduino-pinchangeint-discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an

>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> -Mike Schwager
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "arduino-pinchangeint-discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/arduino-pinchangeint-discuss/spgI67zCTms/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "arduino-pinchangeint-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to arduino-pinchangeint-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-Mike Schwager

Michael Schwager

unread,
Sep 13, 2014, 12:45:12 PM9/13/14
to arduino-pincha...@googlegroups.com
Marcelo,
I notice you are using digitalRead() in your interrupt, which contains a bunch of extra code that could slow down your interrupt routine (if you don't need to check that your port is being used for anything besides digital pin i/o). There are ways to read the pin faster; check out the digitalReadFast() and digitalReadFast2() routines in the attached .h file... (It's called "digitalWriteFast.h" but it contains read routines as well). I picked those up from somewhere. The routines that end in "2" make more assumptions about what kind of pin you have configured and so are theoretically less safe, but they're faster. ...The perfect thing for an interrupt routine.


To unsubscribe from this group and stop receiving emails from it, send an email to arduino-pinchangeint...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-Mike Schwager
digitalWriteFast.h
Reply all
Reply to author
Forward
0 new messages