Smoothing pulsein

Hi,

im trying to map the input from my transmitter and make it into degrees for my rodders. The problem is that my transmitter doesn't give exact input (it varies with about 40) so when I map it to degrees the degrees varies with about 1-2.

int degree = map(ch2, 1000, 2000, 0, 90);

servo3.write(degree);
servo4.write(-degree);
Serial.print (", Degree:"); Serial.print(degree);

I want to make the degrees stable, do you guys have any ideas how it can be done?

Thanks!

Option one, average it.

Option two, use a hysteresis.

Hi,
What is the application, what format does your “transmitter” output a signal.
I presume you want to AVERAGE the signal to reduce jitter/noise.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks … Tom… :slight_smile:

Hi Tom!

Here is my full code:

int ch1;
int ch2;
int ch3;
int ch4;

#include <Servo.h>

Servo servo1;
Servo servo2;           
Servo servo3;           
Servo servo4;            // create servo object to control a servo
                        // twelve servo objects can be created on most boards

int pos1 = 0;           // variable to store the servo position
int pos2 = 90;



void setup() {

  pinMode(3, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(9, INPUT);



  Serial.begin(115200);

  servo1.attach(2); 
  servo2.attach(4);
  servo3.attach(7);
  servo4.attach(8);


}

void loop() {

Serial.println("Table");

  ch2 = pulseIn(5, HIGH, 30000);
   Serial.print (", Ch2:"); Serial.print (ch2);

  ch3 = pulseIn(6, HIGH, 30000);
   Serial.print (", Ch3:"); Serial.print (ch3);
      
  ch4 = pulseIn(9, HIGH, 30000);
   Serial.print (", Ch4:"); Serial.print (ch4);   
   

// Framåtdrivning

int hast = map(ch3, 1550, 2000, 8, 3);

if (ch3 >= 1550 && 1400 <= ch4 && ch4 <= 1600) {
 for (int pos = 0; pos <= 90; pos ++)  {
    servo1.write(pos);
    servo2.write(90 - pos);
  delay(hast);
  }
  for (int pos = 90; pos >= 0; pos --) {
    servo1.write(pos);
    servo2.write(90 - pos);
    delay(hast);           
  }
}

//Sväng höger

if (ch4 <= 1400 && ch4 >=1000) {

  for (int pos = 0; pos <= 90; pos++)  {
    servo1.write(pos);
    servo2.write(90);
  delay(hast);
  }
  for (int pos = 90; pos >= 0; pos --) {
    servo1.write(pos);
    servo2.write(90);
    delay(hast);           
  }
}


//Sväng vänster
if (ch4 >= 1600) {

  for (int pos = 0; pos <= 90; pos++)  {
    servo1.write(-90);
    servo2.write(90-pos);
  delay(hast);
  }
  
  for (int pos = 90; pos >= 0; pos --) {
    servo1.write(-90);
    servo2.write(90-pos);
    delay(hast);           
  }
}

//Stiga och dyka

int degree = map(ch2, 1000, 2000, 0, 90);

servo3.write(degree);
servo4.write(-degree);
Serial.print (", Degree:"); Serial.print(degree);

Here’s the circuit, except the receiver… I couldn’t find it in fritzing.

It is the last part I’m having problem with. Im using a receiver and transmitter from hobbyking: http://www.hobbyking.com/hobbyking/store/_9042__Hobby_King_2_4Ghz_6Ch_Tx_Rx_V2_Mode_2.html

Cool, a powerless Arduino...

Are you really using a 3.7V battery to run 4 servos? Servos usually need 6V.

Mapping to 90 for high range yields what resolution in degrees?

septillion:
Cool, a powerless Arduino...

Oh I'm so sorry, I hope you still understand.......

PaulS:
Are you really using a 3.7V battery to run 4 servos? Servos usually need 6V.

No, I have a 7.4V but couldn't find it either in fritzing.

dlloyd:
Mapping to 90 for high range yields what resolution in degrees?

What do you mean? I'm not very experienced in programming.

simgra:
Oh I'm so sorry, I hope you still understand.......

I don't know if I understand your drawing because you only now give us additional information. That's why we asked for a complete drawing for example by hand. Heck, it's even faster then Frukzing.

But I think I still understand it better then you understood the comments. But hé, let's just ignore things you don't get, right? :wink:

Hi,

A picture of a hand drawn circuit in jpg, png?
Showing how you have your power supplies connected an the RC Rx.
Have you got the servo gnd connected to the UNO gnd?

Thanks.. Tom.... :slight_smile: