Having trouble with the "if" function

im using a servo motor to release a parachute at a certain altitude however using the if function doesnt seem to work, i have it set to release at a certain temperature as this allows me to test it easier however it doesnt seem to want to do it, this is the code i am using

/*
ReadAnalogVoltage

Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage
*/
#include <Servo.h>
Servo servo;
int sensorValue; //integer variable for value from thermistor
float voltage; //float variable for equivalent voltage from thermistor
float temperature; //float vairable for celcius temperature
int pressureValue;
float pressure;
float pressurework;
bool printFlag = true;
void setup() {
Serial.begin(9600);
servo.attach(8);

}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
float temperature = voltage*(-28.084) + 97.767;
// print out the value you read:
Serial.print(voltage);
Serial.print(" v ");
Serial.print(temperature);
Serial.print(" C ");
delay(700);
pressureValue = analogRead (A1);
pressurework = ((pressureValue / 1024.0) + 0.095) / 0.0009;
pressure = (pressurework)-((pressurework)*0.0036);
Serial.print("CanSat_Cupertino");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println (" millibars");
if (temperature >= 28 )
{
servo.write(90);
printFlag = true;

if (printFlag == true)
{
Serial.print ("Prachute Deployed");
printFlag = false;
}
}

delay (500);

}

the servo code is towards the bottom of the code,
any help is appreciated

Read the forum guidelines. Post your test code in code tags. Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) to indent the code for readability before posting code.

You read an analogue input then do various bits of maths to the result. Break the maths down into small steps and print the result of each step. I think you will find the results are not what you expect.


[quote="morgan_reid03, post:1, topic:874229"]
if (temperature >= 28 )
{
servo.write(90);
//printFlag = true;  //      <————<<<<

if (printFlag == true)
{
Serial.print ("Prachute Deployed");
printFlag = false;
}
}
[/quote

Thats what i have in but the problem is that the servo turns as soon as the cirvuit is turned out, even when the temperature is less that 28


Add the Serial.prints below.

Serial.print ("Temperature = ");
Serial.println(temperature ); // tell us what gets printed  <———<<<<<

if (temperature >= 28 )
{
servo.write(90);
//printFlag = true;  //      <————<<<<

if (printFlag == true)
{
Serial.print ("Prachute Deployed");
printFlag = false;
}
}

The default servo position is 90 degrees when the attach() function is executed unless you command it to do something different. Use the write() function with your desired home position before you do the attach()

1 Like

the value printed is 25.54 for the temperature

What is the problem with that ?

it was still turning to 90 degrees but your previous explanation solved it, it now works as intended thank you very much :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.