Thanks again Martin for taking the time to help me out and providing all the very useful information. I am sorry that I am going to bug you again for a little bit of help.
I found some code online to create a debounce button, and I attempted to incorporate it to the State Machine code hoping that it would function.
After some trials and errors, I managed to clear the errors and allowed the codes to compile. However, unfortunately, I must have done something wrong here because when I uploaded the codes, the servos were activated immediately without me pushing the button. And the servo also didn't return to the original position after being activated. I couldn't figure out what's wrong and I hope you can help me find out.
Anyways, I would like to say thank you again; because of your help, I have felt more comfortable in trying out Arduino coding for the project and (at least I feel like) my skills are slowly improving.
I hope people that are building similar projects and are new to Arduino will find this post and the responses helpful too.
#include <Servo.h>
#include <BMP280_DEV.h> // Include the BMP280_DEV.h library
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int servoPin1 = 9;
int servoPin2 = 10;
int servoPin3 = 11;
int servoPos = 0;
Servo Myservo1; //define servos
Servo Myservo2;
Servo Myservo3;
float temperature, pressure, altitude; // Create the temperature, pressure and altitude variables
BMP280_DEV bmp280; // Instantiate (create) a BMP280_DEV object and set-up for I2C operation (address 0x76)
enum StateMachine { DISARMED,
ARMED,
LAUNCH,
DEPLOY,
ABORT
} stateMachine = DISARMED;
bool armedInput = false, launchInput = false, abortInput = false;
bool servosActivated = false;
float threshold = 5.0f; // Altitude above launch site in meters
float launchSiteAltitude;
void initializeBarometer() {
bmp280.begin(); // Default initialisation, place the BMP280 into SLEEP_MODE
//bmp280.setPresOversampling(OVERSAMPLING_X4); // Set the pressure oversampling to X4
//bmp280.setTempOversampling(OVERSAMPLING_X1); // Set the temperature oversampling to X1
//bmp280.setIIRFilter(IIR_FILTER_4); // Set the IIR filter to setting 4
bmp280.setTimeStandby(TIME_STANDBY_1000MS); // Set the standby time to 1 seconds
bmp280.startNormalConversion(); // Start BMP280 continuous conversion in NORMAL_MODE
}
void initializeServos() {
Myservo1.attach(servoPin1); // Connect Servos to arduino pins
Myservo2.attach(servoPin2);
Myservo3.attach(servoPin3);
}
void debounceButtonSetup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void setup()
{
Serial.begin(115200); // Initialise the serial port
initializeBarometer();
initializeServos();
debounceButtonSetup();
pinMode(buttonPin, INPUT);
}
void airbrakeGo()
{
Myservo1.write(90);
Myservo2.write(90);
Myservo3.write(90);
}
void airbrakeClose()
{
Myservo1.write(0);
Myservo2.write(0);
Myservo3.write(0);
}
float debounceButtonPush() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
void loop() {
if (bmp280.getMeasurements(temperature, pressure, altitude)) // Check if the measurement is complete
{
Serial.print(temperature); // Display the results
Serial.print(F("*C "));
Serial.print(pressure);
Serial.print(F("hPa "));
Serial.print(altitude);
Serial.println(F("m"));
}
switch (stateMachine)
{
case DISARMED:
armedInput = digitalRead(buttonPin);
if (armedInput == true)
{
stateMachine = ARMED;
}
break;
case ARMED:
launchInput = debounceButtonPush();
bmp280.getAltitude(launchSiteAltitude);
if (launchInput == true)
{
stateMachine == LAUNCH;
}
break;
case LAUNCH:
altitude = bmp280.getAltitude(altitude) - launchSiteAltitude;
if (altitude > threshold)
{
stateMachine = DEPLOY;
}
break;
case DEPLOY:
if (servosActivated == false)
{
airbrakeGo();
servosActivated = true;
}
break;
case ABORT:
// Add abort code here...
break;
default:
break;
}
}
Thanks again ![]()