Help needed for timing of servo motor and also button not working properly

Hi, so I am trying to make my program so when the IR sensor detects something, the servo moves after a few seconds and then moves back again after another few seconds. However, it seems that when something is detected the servo just moves without any delay and then stays on for a few seconds and moves back even if something is still blocking the IR. If someone can help me make it delay at the beginning of detection that would be great, it would be even better if you can help me so that the gate stays open as long as something is blocking the IR and then closes after a few seconds when something isnt blocking it.

Now for the button, it seems to work sometimes, but it should work so that at any moment I press it, it should change the time interval for the single (deadend) lights so that the green on time is cut by half.

Thank you for your time for looking at this, and I would appreciate any help given.

 /*
 
 *************TEST/ROUGH VERSION*************

 Global Variable Dictionary
 int motorMillis- stores current time for use by motor algorithm
 int motorCycleTime- stores time in the opening and closing of gates cycle.
 int motorPreviousMillis- stores the last time gates closed
 int trafficMillis- stores current time for use by traffic lights algorithm
 int trafficCycleTime- stores time passed so far int traffic lights cycle
 int trafficPreviousMillis- stores the last time a cycle was completed
 */

//Import library
#include <Servo.h>

int red1 = 10; //traffic lights at the T / deadend
int yellow1 = 11;
int green1 = 12;
int red2 = 4; //traffic lights crossing the T
int yellow2 = 5;
int green2 = 6;
int pedRed = 2; //pedestrian lights
int pedGreen = 6;
int button = 7; // digital button pin
boolean pressed = false; //if button is pressed or not

//Street Light
int streetSensorPin = A1;   // LDR input, connected to Analog 0
int sensorValue = 0;  // variable to store the value coming from the LDR sensor
int streetLight = 9; //LED for street light

//Timing
//Traffic lights and pedestrian lights
unsigned long trafficPreviousMillis = 0; //Used to reset the cycle for traffic lights
unsigned long change = 0;//Amount of time the cycle is sped up by the button
unsigned long trafficMillis = 0;//current time
unsigned long trafficCycleTime = 0;//stores time in traffic cycle

//Timing for motor and gates
unsigned long motorMillis = 0;//current time
unsigned long motorCycleTime = 0; //time in gate opening and closing cycle
unsigned long motorPreviousMillis = 0; //Used to reset the cycle

//Starting angle of motor
int pos = 0;

//motor and IRS

// Declaring servo and its specific digital pin value 
int servoPin = 8;
int motorDegree = 0;

// Create a servo object
Servo servo1;
//input for IR
int gatesSensor = A3; // Connected to pin analog 3
int val = 0; // variable to store the value coming from the sensor
boolean inProgress = false; //vairiable to store whether is gate are open or not

// setUp Function: sets the pins as INPUT or OUTPUT and connects the motor. Serial communication is set to 9600 baud.
void setup()
{
  // We need to attach the servo to the used pin number
  servo1.attach(servoPin);
  servo1.write(0);
  pinMode(red1, OUTPUT);
  pinMode(yellow1, OUTPUT);
  pinMode(green1, OUTPUT);

  pinMode(red2, OUTPUT);
  pinMode(yellow2, OUTPUT);
  pinMode(green2, OUTPUT);

  pinMode(pedRed, OUTPUT);
  pinMode(pedGreen, OUTPUT);
  pinMode(button, INPUT);

  pinMode(streetSensorPin, INPUT); //pin connected to the relay
  pinMode(streetLight, OUTPUT); //pin connected to the relay

  Serial.begin(9600); //sets serial port for communication
}

/*Funtion loop: controls the motor, IR sensor, LDR/light sensor, street lamp, pedestrian lights, traffic lights and button. 
 Local Variable Dictionary
 int state -reads whether the pedestrian button was pressed or not 
 */
void loop()
{
  //Emitter LED White: short leg to grounding wire long leg to resistor
  //Detector LED Black: Short leg to resistor long leg to grounding wire

  //Time management for motor
  //millis() used instead of delay() to ensure entire program is not delayed causing problems
  motorMillis = millis();
  motorCycleTime = motorMillis - motorPreviousMillis;
  //Reading from IR sensor only when the gates are not in the process of opening and closing
  if (inProgress == false)
  {
    val = analogRead(gatesSensor);
  }

  // Motor operating and opening and closing gates when an object is detected.
  if (val >= 900)
  {
    inProgress = true;
    if (motorCycleTime <= 2000)
    {
      if (motorDegree < 180)
      {
        motorDegree++;
        servo1.write(motorDegree);
      }
    }
    //setting motor back to original postion
    else if (motorCycleTime >= 2000 && motorDegree >= 1)
    {
      motorDegree--;
      servo1.write(motorDegree);
    }
    else
    {
      if (motorDegree == 0 && motorCycleTime >= 4000)
      {
        motorPreviousMillis = millis();
        inProgress = false;
      }
    }
  }

  // LDR/street light sensor
  sensorValue = analogRead(streetSensorPin);
  //Setting sensitivity of LDR and turning on street light accordingly
  if (sensorValue  < 50)
  {
    digitalWrite(streetLight, HIGH);
  }
  else
  {
    digitalWrite(streetLight, LOW);
  }

  //Time management for Traffic lights and pedestrian Lights
  trafficMillis = millis() + change;
  trafficCycleTime = trafficMillis - trafficPreviousMillis;

  //Reading state of button
  int state = digitalRead(button);
  
  //Setting state of button according to read from button.
  if (state == HIGH && pressed == false)
  {
    pressed = true;
  }
  else
  {
    pressed = false;
  }

  //Time interval quickening for pedestrian crossing
  if (pressed == true  &&  trafficCycleTime >= 0 && trafficCycleTime <= 3000)
  {
    change = 3000;
  }

  //Traffic and pedestrian lights algorithm.
  if (trafficCycleTime >= 0 && trafficCycleTime <= 6000)
  {
    //Single Traffic Lights green and Parallel Traffic Lights red
    //Pedestian Lights red
    
    lights1(1);
    lights2(3);
    digitalWrite(pedRed, HIGH);
    digitalWrite(pedGreen, LOW);
  }
  else if (trafficCycleTime > 6000 && trafficCycleTime <= 8000)
  {
    //Single Traffic Lights yellow and Parallel Traffic Lights red
    
    lights1(2);
    lights2(3);
  }
  else if (trafficCycleTime > 8000 && trafficCycleTime <= 10000)
  {
    //Single Traffic Lights red and Parallel Traffic Lights yellow
    
    lights1(3);
    lights2(2);
  }
  else if (trafficCycleTime > 10000 && trafficCycleTime <= 16000)
  {
    //Single Traffic Lights red and Parallel Traffic Lights green
    //Pedestian Lights green
    
    lights1(3);
    lights2(1);
    digitalWrite(pedRed, LOW);
    digitalWrite(pedGreen, HIGH);
    pressed = false;
  }
  else if (trafficCycleTime > 16000 && trafficCycleTime <= 18000)
  {
    //Single Traffic Lights red and Parallel Traffic Lights yellow
    //Pedestian Lights red
    
    lights1(3);
    lights2(2);
    digitalWrite(pedRed, HIGH);
    digitalWrite(pedGreen, LOW);
  }
  else if (trafficCycleTime > 18000 && trafficCycleTime <= 20000)
  {
    //Single Traffic Lights yellow and Parallel Traffic Lights red
    
    lights1(2);
    lights2(3);
  }
  else
  {
    if (trafficCycleTime > 20000)
    {
      //Resets the time values
      trafficPreviousMillis = millis();
      change = 0;
    }
  }

}

/* function lights1(int i); Sets the single traffic lights
 Local Variable Dictonary
 Type Name Description
 int i- controls what scenario to put the traffic lights to.
 Traffic lights 1 at vertical line of T
 */
void lights1(int i)
{
  if (i == 1)
  {
    digitalWrite(green1, HIGH);
    digitalWrite(yellow1, LOW);
    digitalWrite(red1, LOW);
  }
  else if (i == 2)
  {
    digitalWrite(green1, LOW);
    digitalWrite(yellow1, HIGH);
    digitalWrite(red1, LOW);
  }
  else if (i == 3)
  {
    digitalWrite(green1, LOW);
    digitalWrite(yellow1, LOW);
    digitalWrite(red1, HIGH);
  }
}

/* function lights2(int i); Sets the parallel traffic lights
 Local Variable Dictonary
 Type Name Description
 int i- controls what scenario to put the traffic lights to.
 Traffic Lights 2 crosses the T
 */
void lights2(int i)
{
  if (i == 1)
  {
    digitalWrite(green2, HIGH);
    digitalWrite(yellow2, LOW);
    digitalWrite(red2, LOW);
  }
  else if (i == 2)
  {
    digitalWrite(green2, LOW);
    digitalWrite(yellow2, HIGH);
    digitalWrite(red2, LOW);
  }
  else if (i == 3)
  {
    digitalWrite(green2, LOW);
    digitalWrite(yellow2, LOW);
    digitalWrite(red2, HIGH);
  }
}
//Import library
#include <Servo.h>

You are NOT Importing a library, so lose the stupid comment.

  pinMode(button, INPUT);

How IS your button sewn on? Using a switch would make more sense. Using INPUT_PULLUP would make connecting the switch MUCH easier.

  if (inProgress == false)

If WHAT is in progress, or not?

    lights1(1);
    lights2(3);

Useless function names. I should NOT have to look at the function, and guess what these magic numbers mean, to know what the code is doing.

I strongly suspect that the magic numbers should be replaced by enum NAMES that have have those values, where the names make sense. What those names should be, though, I can’t guess.

PaulS:

//Import library

#include <Servo.h>



You are NOT Importing a library, so lose the stupid comment.

So what word would you use in preference to “import”? It seems an OK word to me.

 pinMode(button, INPUT);

How IS your button sewn on? Using a switch would make more sense.

I think it is time for you to accept that the usage of language has evolved :slight_smile:

…R

So what word would you use in preference to "import"?

Maybe the one that is actually used. C and C++ include files. Java imports libraries. We are NOT using Java.

I think it is time for you to accept that the usage of language has evolved

Not going to happen. Buttons are for shirts. Switches are for Arduinos.