Hi AWOL, below is your code:
int filtered = 0;
const int FILTER_CONST = 4;
...
...
int newVal = analogRead (potPin);
filtered = (filtered - (filtered / FILTER_CONST)) + (newVal / FILTER_CONST);
And my program is:
#include <LiquidCrystal.h>
#include <SoftwareServo.h>
SoftwareServo rudder; // create servo object to control a servo
SoftwareServo aileron; // create servo object to control a servo
SoftwareServo pitch; // create servo object to control a servo
//=====================================Accelerometer============================================
int x_pot = 0; // analog pin used to connect the potentiometer
int x_val; // variable to read the value from the analog pin
int x_display; // variable to read the value from the analog pin
int y_pot = 1; // analog pin used to connect the potentiometer
int y_val; // variable to read the value from the analog pin
int y_display; // variable to read the value from the analog pin
//=====================================Joystick=======================================================
int x_joy = 2; // analog pin used to connect the potentiometer
int z_val; // variable to read the value from the analog pin
int z_display; // variable to read the value from the analog pin
int y_joy = 3;
//===================================Switch================================================
int buttonPin = 7;
int buttonState =0;
LiquidCrystal lcd(6, 7, 8, 2, 3, 4, 5);
void setup()
{
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
//rudder.attach(12); // attaches the servo on pin 9 to the servo object
aileron.attach(11); // attaches the servo on pin 9 to the servo object
pitch.attach(10); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH){
x_val = analogRead(x_pot); // reads the value of the potentiometer (value between 0 and 1023)
y_val = analogRead(y_pot); // reads the value of the potentiometer (value between 0 and 1023)
x_val = map(x_val, 350,500 , 0, 179); // scale it to use it with the servo (value between 0 and 180)
y_val = map(y_val, 350, 500, 0, 179); // scale it to use it with the servo (value between 0 and 180)
}
else if(buttonState == LOW)
{
x_val = analogRead(x_joy); // reads the value of the potentiometer (value between 0 and 1023)
y_val = analogRead(y_joy); // reads the value of the potentiometer (value between 0 and 1023)
x_val = map(x_val, 30,140 , 0, 179); // scale it to use it with the servo (value between 0 and 180)
y_val = map(y_val, 30, 140, 0, 179); // scale it to use it with the servo (value between 0 and 180)
}
//x_val = map(x_val, 30,60 , 0, 179); // scale it to use it with the servo (value between 0 and 180)
aileron.write(x_val); // sets the servo position according to the scaled value
//aileron_right.write(x_val); // sets the servo position according to the scaled value
// waits for the servo to get there
// y_val = map(y_val, 30, 70, 0, 179); // scale it to use it with the servo (value between 0 and 180)
pitch.write(y_val); // sets the servo position according to the scaled value
//delay(15); // waits for the servo to get there
/* x_val = analogRead(z_pot); // reads the value of the potentiometer (value between 0 and 1023)
z_val = map(z_val, 300, 800, 0, 179); // scale it to use it with the servo (value between 0 and 180)
rudder.write(z_val); // sets the servo position according to the scaled value
*/
// delay_val = analogRead(delay_pot); // reads the value of the potentiometer (value between 0 and 1023)
// delay_display = map(delay_pot, 0, 1023, 0 ,100); // scale it to use it with the servo (value between 0 and 180)
delay(15);
SoftwareServo::refresh();
// Print a message to the LCD.
/*
x_display = map(x_val, 0, 179, 0, 100);
y_display = map(y_val, 0, 179, 0 ,100);
lcd.clear();
if( x_display == 50 ){
lcd.setCursor(0,0);
lcd.clear();
lcd.print("Roll:");
lcd.print(x_display-50);
lcd.print("% neutral"); }
else if( x_display >50){
lcd.setCursor(0,0);
//lcd.print("Pitch: 40%");
//lcd.setCursor(0,1);
//lcd.print("Yaw: 40%");
//delay(2000);
// lcd.clear();
lcd.print("Roll:");
lcd.print(x_display-50);
lcd.print("% left");
}
else {
lcd.setCursor(0,0);
//lcd.print("Pitch: 40%");
//lcd.setCursor(0,1);
//lcd.print("Yaw: 40%");
//delay(2000);
lcd.print("Roll:");
lcd.print(50-x_display);
lcd.print("% right"); }
if( y_display == 50 ){
lcd.setCursor(0,1);
lcd.print("Pitch:");
lcd.print(y_display);
lcd.print("% neutral");
}
if(y_display >50){
lcd.setCursor(0,1);
lcd.print("Pitch:");
lcd.print(y_display-50);
lcd.print("% down");
}
else {
lcd.setCursor(0,1);
lcd.print("Pitch:");
lcd.print(50-y_display);
lcd.print("% up");
}
*/
}
basically I'm not using the LiquidCrystal.h, its just a additional thing. in which part of my program do you insert the code?
thks