Led not lighting the way it should

hey i need my code to

  1. When 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.
  2. While the boom gate is in motion and opening, the LED shall blink red at 5Hz.
  3. When the boom gate is fully open and a car is present, the LED shall display green (not blinking).
  4. For the C seconds that the boom gate remains open after the car leaves the sensor, the LED
    shall blink green at 5Hz.
  5. While the boom gate is closing, the LED shall blink red at 5Hz.
  6. When the boom gate is closed, and pedestrians are allowed to cross:
    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
    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
    down to the nearest second. For example, if the pedestrians can cross for B = 10 seconds,
    the light shall be solid blue for 7 seconds and then blink for the remaining 3 seconds.

but my current code is not doing that can someone help

i have different blink code fucntions for 3 different colour in millis

Hi,

Can you please post your code?
Can you please post a schematic of your project?
How are you triggering the operation of the gate?

How have you got your switches wired.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

is that a school project?

yea it is

If course it is! No-one uses the word "shall" unless it's a formal specification or agreement of some kind.

1 Like

what is the purpose of the ultrasonic sensor. It's not stated as requirement.

it is to detect if there is a car present

have you already heard about a "finite state machine"? When you google for it or read in wikipedia does that help? If not - what else do you need to know?

my led is not blinking when the gate is in motion and the button is not really activating the blue light

What is the purpose of the button?
What is the purpose of the serial interface (writing "open")`?

Hello
How do you enter the pass word?

Can you please convert the following statements into programming codes using Arduino UNO and post it here?

1. When the Bloom Gate is down, a micro switch (S1 connected at DPin-4 with internal pull-up resistor, Fig-1 below) remains closed and the redLED (connected at DPin-10 with a 2.2k series resistor) remains ON.

2. When the Bloom Gate is opening and not yet fully opened (when fully opened -- the microswitch S2 gets closed, which is wired via DPin-5 and internal pull-up resistor, Fig-1 below), the redLED keeps blinking at 5 Hz.

led1Sw2y
Figure-1:

@subyy

imho your requirement 1 and 6 are not clear:

1 When 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.

6 When the boom gate is closed, and pedestrians are allowed to cross:

so what does this mean? are the pedestrians allowed to cross when boom gate is down (according to 6) or not (assumpiton from 1).

please explain what should happen when.

pedestrians are allowed to cross when the boom gate is down.. 1 just says that when no pedestrians are crossing as well as gate down led stays red

Distance();
if (button==LOW){
   boom_gate.write(0);}
  else {
    Distance();
    setColour (255,0,0);}

 if(button==HIGH){
    Distance();
    boom_gate.write(90);}
 blinkRed();

i am not sure if this is what u wanted

A Bloom Gate (Fig-1) is there to control vehicle entry/departure. Why are you talking about pedestrian? Please, explain.
bloomGate

Figure-1:

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");
    }
  }
}

Thank you for posting the codes (post #21) which you have tried but are not really correct! Now, create the sketch of Section-2 bases on the codes of Section-1 and report the outcomes.

1.
(1) Configure DPin-4 and DPin-5 (Fig-1 of post #18) as input lines with internal pull-up resistors. Configure DPin-10 as output line.

pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(10, OUTPUT);

(2) Keep checking if S1 is closed and then turn on the redLED.

while(digitalRead(4) != LOW)
{
    ;    //keep checking
}
digitalWrite(10, HIGH);    //redLED is ON

2. Put the codes of Section-1 at the appropriate places of the following blank sketch. After that upload the sketch in your Arduino UNO and report the outcomes.

void setup()
{

}

void loop()
{

}

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