Hello. I am very very new to programming so I may have committed very dumb mistakes, but I’ve been trying to make a simple code to emulate the runway and take off of a plane in an airport by using push buttons in Arduino.
In theory it should close the runway when the first button is pressed and print its state which would be 0 once. Then a plane would take off when the second button is pressed, turn on a LED for 1 second and open the runway again.
Problem is, the second task regarding the take off doesn’t even work, even though as far as I know the tasks have the same priority and the don’t even interact with each other. Also the printing of the runway state seems to happen as long as the button is held for which I guess is 1ms, even though it should only be printed once with the task delay after the function.
Any help would be greatly appreciated.
(for some reason I cannot post links, but the ID of the project in Wokwi is 384041557583023105)
#include <Arduino_FreeRTOS.h>
int ledPin = 11;
int authorization = 1;
int takeoff = 1;
static int runway = true; //state of the runway (open/closed)
const int authoPin = 12;
const int takeoffPin = 8;
void Authorization(void *pvParameters) {
authorization = digitalRead(authoPin);
if (authorization == 0) { //if the first button is pressed
runway = false; //close the runway
Serial.println(runway); //print state of the runway
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
void TakeOff(void *pvParameters) {
takeoff = digitalRead(takeoffPin);
if (takeoff == 0) { //if the second button is pressed
Serial.println(runway); //print state of the runway
digitalWrite(ledPin, HIGH);
vTaskDelay(1000/portTICK_PERIOD_MS);
digitalWrite(ledPin, LOW);
runway = true; //open the runway again
Serial.println(runway); //print state of the runway
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(authorization, INPUT_PULLUP);
pinMode(takeoff, INPUT_PULLUP);
xTaskCreate (Authorization, "Authorization", 128, NULL, 1, NULL);
xTaskCreate (TakeOff, "TakeOff", 128, NULL, 1, NULL);
vTaskStartScheduler();
}
void loop() {
}