auto pitch

I am new in Arduino and have a project for my ultralight aircraft:is to maintain, once at a certain altitude, the selected altitude, with the barometric pressure.
How? with input on the electrical trim pitch with 2 relays.
Once the Arduino is on power, that the pressure is mesured and fixed so long the arduino on power.
If the plane is going down, the pressure increases and there is a input on relay up (1 sec) with a delay of 5 sec due to the react time of the plane. If the plane is going up, the pressure decreases and there is a input on relay down (1 sec) with a delay of 5 sec and so one in the loop.

The project is based on a BMP 085 sensor and Arduino UNO with 2 relays

I wrote a sketch but I have some questions:

  1. The barometric pressure must be fixed at once time in the void setup but I do not know how?
  2. in the the void loop: if (pressure>threshold); I have the message : "pressure was not declared in this scope".

I think that the "variables" and the void setup are wrong but i can not find an answer.

This is the sketch I wrote:

#include "Wire.h"
#include "Adafruit_BMP085.h"

Adafruit_BMP085 bmp;
int threshold= pressure ,HPa// threshold to hold the bar pressure
int relayup = 2;// relay up on pin 2
int relaydown = 3;// relay down on pin 3

void setup() {
Serial.begin(9600);
bmp.begin();
int thresold = pressure,HPa // pressure read is threshold

}

void loop()
{
if( pressure > threshold);
digitalWrite(relayup,HIGH);
delay(1000);
digitalWrite(relayup,LOW);
delay(5000);
}
if( pressure < threshold );
digitalWrite(relaydown,HIGH);
delay(1000);
digitalWrite(relaydown,LOW);
delay (5000);

Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");

Serial.println();
delay(4000);
}

Do somebody have an idea? Thnaks for reply and suggestions.

I wrote a sketch for mesuring pressure,temp and is it OKE.
Only the pressure is always in Pa. How can I change that in HPa ?
This is the sketch:

#include "Wire.h"
#include "Adafruit_BMP085.h"

Adafruit_BMP085 bmp;

void setup() {
Serial.begin(9600);
bmp.begin();
}

void loop() {
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");

Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa ");

Serial.println();
delay(4000);
}

Thanks - jc

The barometric pressure must be fixed at once time in the void setup but I do not know how?

Read the pressure as you do in the loop() function and assign it to a variable

What is HPa in 2 places in your program ?

It’s worth noting that software for avionics platforms is some of the most structured, inspected, and tested in the world. It’s written by very experienced programmers. There’s a reason for that. Just Say’in.

Good question! I hesitated a long time before asking. I bought a lot of books regarding to C++. I am 70 and before I can manage everything I will be 100.
I took here and there commands I thought it was correct, but it is not.
So, why twice HPa. Honnestly, I can not give you an answer.
I am still learning, but without any help, it is very difficult.
I realised already small programs, but I think that this one is too difficult for me. It is the reason why I took the risk to make a demand.
If somebody can understand what I want to make and perhaps can give me a sketch that I can try?
If not, so far so good.
Thanks for your reply.

if( pressure < threshold );The pressure variable does not appear to be declared anywhere in the program but it would not work even if it was due to the semicolon on the end of this line.

Declare a variable named pressure at the start of the program, remove the semicolon on the comparison line and try compiling again.

int threshold= pressure ,HPa

You don't have "pressure" declared anywhere, let alone assigned a value.
You don't end that line with a ;
,HPa doesn't make sense

You then try to create another variable with the same name (in the same manner) in your setup()
Leaving off the "int" for that one would use the global variable "threshold" (if it was correctly declared).

Maybe something more like:

int threshold;
int presssure;

void setup() 
{
  Serial.begin(9600);
  bmp.begin();
  pressure = bmp.readPressure()
  threshold = pressure// pressure read is threshold
}

Thnaks,
It is what I was loooking for the void setup.
Now i will try

This is now my sketch.
It seems oke but I have an error in the second line of the void loop line : if( pressure < threshold-50 ); "expected unqualified -id before "if"
What is that? Thanks for advice. jc

#include "Wire.h"
#include "Adafruit_BMP085.h"

Adafruit_BMP085 bmp;
int threshold;
int pressure;
int relayup = 2;// relay up on pin 2
int relaydown = 3;// relay down on pin 3

void setup() {
Serial.begin(9600);
bmp.begin();
pressure = bmp.readPressure();
int thresold = pressure; // pressure read is threshold

}

void loop()
{
if( pressure > threshold+50);
digitalWrite(relayup,HIGH);
delay(1000);
digitalWrite(relayup,LOW);
delay(5000);
}

if( pressure < threshold-50 );
digitalWrite(relaydown,HIGH);
delay(1000);
digitalWrite(relaydown,LOW);
delay (5000);

Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");

Serial.println();
delay(1000);
}

if( pressure < threshold-50 );

if( pressure > threshold+50);

Delete the semicolons

Thanks, you are right.
The compiling is done.
It is grreat !

Hello
Here i am back.
I improved a lot in my project and in understanding C++ programming. Thanks a lot to everybody.
I have 2 questions/problems
At least i wrote a sketch, compiling oke.
When testing I saw that the input on the relays was the same even when in the void loop the "threshold is for instance + or - 1 or 400 was.
I think that the programm do not take these differences in consideration. Why ???

My second question:

I connected the 2 relays. Velleman relays.

  • to 5 V from Arduino
  • to ground
    pin 2 and 3 from arduino to S of both relays.
    When input, the red led of the relays are on but the relays do not make contact.
    After testing; there is 5V on the pins of the relays.
    What can be the reason ?
    and this is the sketch:
    #include "Wire.h"
    #include "Adafruit_BMP085.h"

Adafruit_BMP085 bmp;
int threshold;
int pressure;
int relayup = 2;// relay up on pin 2
int relaydown = 3;// relay down on pin 3

void setup() {
Serial.begin(9600);
bmp.begin();
pressure = bmp.readPressure();
int thresold = pressure; // pressure read is threshold

}

void loop()
{
if( pressure > threshold+1);
digitalWrite(relayup,HIGH);
delay(1000);
digitalWrite(relayup,LOW);
delay(5000);

if( pressure < threshold-400 );
digitalWrite(relaydown,HIGH);
delay(1000);
digitalWrite(relaydown,LOW);
delay (5000);

Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");

Serial.println();
delay(1000);
}

if( pressure > threshold+1);

  if ( pressure < threshold - 400 );

Why have you still got semicolons at the end of these lines ?

I bought several C++ programming books before I started and as I understood, there are always semicolons after each command. Am I wrong ?
Thanks for your advice.
JC

there are always semicolons after each command. Am I wrong ?

You are not wrong.

However

if( pressure > threshold+1);

is not a command

The code immediately following an if statement is executed if the test returns true. The code immediately following the tests above is a semicolon. Code after the semicolon will be executed unconditionally.

Do you see that might cause a problem ?

What I do not understand in programming is why the programm do no take in consideration the differences between for instance +1 or - 40 after threshold in the void loop
In the void loop, the pressure reference is the mersured pressure in the void setup (because it is only once)
Am I right?

Since you still have the semicolons on your if statements they say in effect "if ( pressure < threshold - 400 ) take no notice, just carry on regardless. That's what the semicolon does...it says "everything I wanted to happen is finished now".

What you really need are some braces {} to enclose what you want to happen when the if statement is true. Just like it shows in the reference for if https://www.arduino.cc/en/Reference/If

Computers, like people, are fussy. You can't just make your own language up and expect it to be understood.

Steve

You

What I do not understand in programming is why the programm do no take in consideration the differences between for instance +1 or - 40 after threshold in the void loop

Me

Code after the semicolon will be executed unconditionally.

So

 digitalWrite(relayup,HIGH);
  delay(1000);
  digitalWrite(relayup,LOW);
  delay(5000);

and

digitalWrite(relaydown,HIGH);
  delay(1000);
  digitalWrite(relaydown,LOW);
  delay (5000);

will both be executed

Without braces around the code block to be executed when the condition is true how would the program know which statements to execute ? By default it executes one statement, ie the semicolon.

Hey Steve,
Thanks for your comments. I did the connections with 2 relays,but there is a problem and i think that my sketch is not correct. I change the {} but whitout result.
It is only relay down which the LED is burning 1 sec.The other one is doing nothing
At this moment the Pa are going from 100529,100525,100539.So ,both have to react.
Now i am lost.I am missing something
I also do not understand why the LED of relay down is burning and the realy do not closed (make contact)
jc

Sigh...

If you do not post your current program then it is difficult to provide help with it.

Sorry, you are right.
Well, I have an ultralight and the most difficult thing is to maintain an altitude. My ultralight has a electric pitch trim that I manage continous during flying with short impuls on the up/down bracket.
The target of my project is parallel to make an "autopitch" based on the Pa pressure
How: arriving at a selected altitude, power on on Arduino. The void setup mesure the pressure and hold it so long the Arduino is power on.
In the void loop
If the pressure is going down (plane is climbing), input of 1 sec on relay down and when the plane is going up (plane is descending),input on relay up of 1 sec. The margin will be about 10 m (is about 40 Pa).
It is the reason I write threshold +- 2 (In the test it is +- 2 for testing on my desk). In reality it will be 40 (about 10 m)
I also included a rest time (relay down) of 5 sec (it is the react time of the plane)
At the beginning, i tought it will be easy, but ????
Now, this is the program:

#include "Wire.h"
#include "Adafruit_BMP085.h"

Adafruit_BMP085 bmp;
int threshold;
int pressure;
int relayup = 2;// relay up on pin 2
int relaydown = 3;// relay down on pin 3

void setup() {
Serial.begin(9600);
bmp.begin();
pressure = bmp.readPressure();
int thresold = pressure; // pressure read is threshold

}

void loop()
{

if( pressure > threshold+2)
digitalWrite(relayup,HIGH);
delay(5000);
digitalWrite(relayup,LOW);
delay(5000);

if ( pressure < threshold-2)
digitalWrite(relaydown,HIGH);
delay(1000);
digitalWrite(relaydown,LOW);
delay (5000);

Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");

Serial.println();
delay(1000);
}

There is something wrong in the void loop ????