You didn't answer my questions so far.
It's difficult to help you as your "Requirements" are not describing what your code does. So if you need help. please answer our questions.
If it is an assignment for school or similar, post the FULL assignment because there is information missing in your entry post.
If you don't need any help anymore - fine - just let us know.
untested
#include <Servo.h>
Servo boom_gate;
constexpr uint8_t TRIG_PIN = 13;
constexpr uint8_t ECHO_PIN = 12;
long duration;
int distance;
constexpr uint8_t greenPin = 3;
constexpr uint8_t redPin = 11;
constexpr uint8_t bluePin = 10;
constexpr uint8_t button = 4;
int Asecond = 3;// button pressed wait time
int lastbuttontime = 4;
int Bsecond = 5; //ped cross time
int Csecond = 4;
bool checkpedestrian = 0;
unsigned long carpassed;
int buttonLast = 1;
int gatelast;
unsigned long buttontime = 0;
unsigned long period = 500;
unsigned long currentMillis = 0;
unsigned long startMillis = 0;
bool ledOn = false;
const int LDR = A0;
int inputval = 0; //variable for values to be read from A1
int outval = 0; //variable for output value from A1
enum class State {
CLOSED_RED, // 1When the boom gate is down and pedestrians are not crossing, the LED shall display red (not blinking). The LED shall be initialised in this state.
UPWARDS, // 2While the boom gate is in motion and opening, the LED shall blink red at 5Hz.
OPEN_CAR, // 3When the boom gate is fully open and a car is present, the LED shall display green (not blinking).
OPEN_FREE, // 4 For the C seconds that the boom gate remains open after the car leaves the sensor, the LED shall blink green at 5Hz.
DOWNWARDS, // 5 While the boom gate is closing, the LED shall blink red at 5Hz.
//bullshit // 6 When the boom gate is closed, and pedestrians are allowed to cross:
CLOSED_BLUE, // o For the B seconds that the pedestrians are allowed to cross the road, the LED shall display blue (not blinking) for the first two-thirds of the time (i.e., 2B/3 seconds), then
CLOSED_WARNING, // o the LED shall blink blue at 5Hz for the remaining B/3 seconds. o If 2B/3 is not an integer, round up to the nearest second, and if B/3 is not an integer, round
WAITING, // WAITING after button was pressed
} state;
State previousState = State::CLOSED_WARNING;
void setup()
{
boom_gate.attach(9);
pinMode (TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(button, INPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
Serial.println("enter password");
}
void setColour(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
bool buttonRead() {
if (digitalRead(button) != buttonLast)
{
buttonLast = digitalRead(button);
if (digitalRead(button) == HIGH)
{
Serial.println(F("button"));
return 1;
}
else
{
return 0;
}
}
}
void gateUp()
{
Gate(1);
}
void gateDown()
{
Gate(0);
}
void Gate(bool position) {
if (position) {
boom_gate.write(0);
}
if (!position) {
boom_gate.write(90);
}
}
void blinkRed()
{
uint32_t currentMillis = millis();
static uint32_t startMillis = 0;
if (currentMillis - startMillis >= period) {
startMillis = currentMillis;
ledOn = !ledOn;
if (ledOn) {
setColour(255, 0, 0);
}
else {
setColour(0, 0, 0);
}
}
}
void blinkBlue()
{
uint32_t currentMillis = millis();
static uint32_t startMillis = 0;
if (currentMillis - startMillis >= period) {
startMillis = currentMillis;
ledOn = !ledOn;
if (ledOn) {
setColour(0, 0, 255);
}
else {
setColour(0, 0, 0);
}
}
}
void redbrightness()
{
inputval = analogRead(LDR);
outval = map(inputval, 0, 500, 900, 0); //The 0-1023 can be changed depending on light from your setup
int lightval = constrain(outval, 0, 500); //the contain output values within 0-255 range
analogWrite(11, lightval);
delay (500);
}
void greenbrightness()
{
inputval = analogRead(LDR);
outval = map(inputval, 0, 500, 900, 0); //The 0-1023 can be changed depending on light from your setup
int lightval = constrain(outval, 0, 500); //the contain output values within 0-255 range
analogWrite(3, lightval);
delay (500);
}
void bluebrightness()
{
inputval = analogRead(LDR);
outval = map(inputval, 0, 380, 255, 0); //The 0-1023 can be changed depending on light from your setup
int lightval = constrain(outval, 0, 255); //the contain output values within 0-255 range
analogWrite(10, lightval);
delay (500);
}
bool Distance()
{
// Reset the sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Start sending signals out
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
//Read in time it look to recieve the signal back
duration = pulseIn(ECHO_PIN, HIGH);
// convert the time to distance
distance = (duration / 2) / 29.1;
if (distance < 150) {
carpassed = millis();
return 1;
}
else {
delay(500);
return 0 ;
}
}
void loop()
{
static uint32_t startMillis = 0; // for the FSM
switch (state)
{
case State::CLOSED_RED :
if (previousState != state) // on ENTRY
{
Serial.println(F("enter CLOSED_RED"));
previousState = state;
setColour(255, 0, 0);
}
// do condition (while in this state)
if (buttonRead()) state = State::WAITING; // exit condition
break;
case State::WAITING :
if (previousState != state)
{
Serial.println(F("enter WAITING"));
previousState = state;
startMillis = millis();
}
if (millis() - startMillis > Asecond * 1000UL) state = State::UPWARDS;
break;
case State::UPWARDS :
if (previousState != state)
{
Serial.println(F("enter UPWARDS"));
previousState = state;
startMillis = millis();
gateUp();
}
blinkRed();
if (millis() - startMillis > 1000) state = State::OPEN_FREE; // Servos are fast. this "delay" only helps to make the blink visible
break;
case State::OPEN_CAR :
if (previousState != state)
{
Serial.println(F("enter OPEN_CAR"));
previousState = state;
startMillis = millis();
}
if (Distance() == 1)
{
Serial.println(F("restart timer"));
startMillis = millis(); // restart timer as long as car is seen
}
if (millis() - startMillis > 5000) state = State::OPEN_FREE; // leave state if time has passed
break;
case State::OPEN_FREE :
if (previousState != state)
{
Serial.println(F("enter OPEN_FREE"));
previousState = state;
startMillis = millis();
}
if (Distance() == 1) state = State::OPEN_CAR;
if (millis() - startMillis > 5000) state = State::DOWNWARDS; // leave state if time has passed
break;
case State::DOWNWARDS :
if (previousState != state)
{
Serial.println(F("enter DOWNWARDS"));
previousState = state;
startMillis = millis();
gateDown();
}
blinkRed();
if (millis() - startMillis > 1000) state = State::CLOSED_BLUE; // Servos are fast. this "delay" only helps to make the blink visible
break;
case State::CLOSED_BLUE :
if (previousState != state)
{
Serial.println(F("enter CLOSED_BLUE"));
previousState = state;
startMillis = millis();
setColour(0, 0, 255);
}
if (millis() - startMillis > (Bsecond*2/3) * 1000UL) state = State::CLOSED_WARNING;
break;
case State::CLOSED_WARNING :
if (previousState != state)
{
Serial.println(F("enter CLOSED_WARNING"));
previousState = state;
startMillis = millis();
}
blinkBlue();
if (millis() - startMillis > (Bsecond*1/3) * 1000UL) state = State::UPWARDS; // reopen gate
break;
}
}
void loopOld()
{
if (Distance())
{
if (checkpedestrian)
{
Gate(0);
setColour(0, 0, 255);
bluebrightness();
}
else
{
if (gatelast != Distance())
{
redbrightness();
blinkRed();
Gate (1);
gatelast = Distance();
}
else {
Gate(1);
setColour (0, 255, 0);
greenbrightness();
}
}
}
else
{
if (millis() > carpassed + Csecond * 1000)
{
if (gatelast != Distance())
{
redbrightness();
blinkRed();
Gate (0);
gatelast = Distance();
}
else {
Gate(0);
setColour(255, 0, 0);
redbrightness();
}
}
else {
Gate(1);
setColour(0, 0, 255);
bluebrightness();
}
}
if (digitalRead(button) == HIGH)
{
buttontime = millis();
}
if (buttontime != 0 && millis() > buttontime + (Asecond * 1000) && !Distance())
{
for (unsigned long time = millis(); millis() < time + 2 * Bsecond / 3;)
{
setColour(0, 0, 255);
bluebrightness();
}
for ( unsigned long time = millis(); millis() < time + Bsecond / 3;)
{
blinkBlue();
bluebrightness();
}
buttontime = 0;
}
if (Serial.available() > 0) { //wait for data available
String teststr = Serial.readString(); //read until timeout
Serial.println(teststr + "\n");
teststr.trim(); // remove any \r \n whitespace at the end of the String
if (teststr == "open") {
Serial.println("correct!");
} else {
Serial.println("wrong password, try again");
}
}
}