servo signal problem

Hello!

I have a problem with sending proper signal to servo from transmitter through arduino DUE. I wrote a code like this:

#include<Servo.h>

Servo servo1;

int ch1;
int elevator;

void setup() {
// put your setup code here, to run once:

pinMode(40,INPUT);
pinMode(31,OUTPUT);
servo1.attach(31);
Serial.begin(9600);
ch1 = 2000;
}

void loop() {
// put your main code here, to run repeatedly:
ch1 = pulseIn(40,HIGH,25000);
if (ch1 <= 2800 && ch1>= 1600)
{

Serial.print(“Aileron:”);
Serial.println(ch1);

}
delay(100);
}

when I take out if statement, it gives signal of average 2200. With if statement it changes to 1800. I don’t understand why?

Thank you very much!

CheckWiring_ONLYONE.ino (430 Bytes)

That code, with or without the if() doesn't 'give' any average at all. What are you really looking at?

MorganS: That code, with or without the if() doesn't 'give' any average at all. What are you really looking at?

I know, when I open serial monitor, I'm receving numbers. It fluctuates somewhere between 2150 and 2250, but if I include the if statement (as above) numbers in serial monitor drop to around 1800.

Show us the second version of the code “without the if statement”. I don’t understand what you have removed or not removed.

Please use [ code ] tags. On the full editor (not quick-reply) the button looks like </>

#include<Servo.h>

Servo servo1;

int ch1;
int elevator;


void setup() {
 // put your setup code here, to run once:

 pinMode(40,INPUT);
 pinMode(31,OUTPUT);
 servo1.attach(31);
 Serial.begin(9600);
 ch1 = 2000;
}

void loop() {

ch1 = pulseIn(40,HIGH,25000);

 
 Serial.print("Aileron:");
 Serial.println(ch1);


  delay(100);
  }

Without if statement it looks like this. I solved the problem. Problem was function pulseIn as I understood. I used Interrupts instead. Thanks!