my project about automated system I am using stepper motor to open and close the gate

I want my code be like when the gate opened it stays 5 seconds

then check the sensor, if there is a motion the gate should not close and stay open, when there is no motion the gate should close

if the gate is closing and there is a motion, motion should interrupt closing the gate and reopening the gate

I am using Arduino uno

the code is :

#include <RH_ASK.h>//include radioHead ASK library 
#include <SPI.h> // include dependant SPI libray
#include <Stepper.h> // Include the Arduino Stepper.h library:
// Define number of steps per rotation:

RH_ASK rf_driver; // create ASK object
int ledon = 13;
int ledoff = 5;
int sensor = 3;

const int stepsPerRevolution = 2048;// Define number of steps per rotation:

// Wiring:
// Pin 8 to IN1 on the ULN2003 driver
// Pin 9 to IN2 on the ULN2003 driver
// Pin 10 to IN3 on the ULN2003 driver
// Pin 11 to IN4 on the ULN2003 driver
// Create stepper object called 'myStepper', note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {

  rf_driver.init(); //initialize ASK object

  // Begin Serial communication at a baud rate of 9600:

  // Set the speed to 5 rpm:
  pinMode(ledon, OUTPUT);
  pinMode(ledoff, OUTPUT);


void loop() {

  //set buffer to size of expected message
  //uint8_t buf[8];
  // uint8_t buflen =sizeof(buf);

  //check if recevied packet is correct size
  //  if (rf_driver.recv(buf,&buflen))
  // int i;
  // message received with valid checksum
  // Serial.print("id: ");
  // Serial.println((char*)buf);
  // String userid =(char*)buf;
  // Serial.println(userid);

  // }


  // uint8_t buf2[8];
  // uint8_t buflen2 = sizeof(buf2);

  // if (rf_driver.recv(buf,&buflen))

  // {

  //  int i;
  // message received with valid checksum
  // Serial.print("password: ");
  // Serial.println((char*)buf);
  // String password = (char*)buf;
  //  Serial.println(password);

  // }
  // delay(5000);

  String pass = "1234";
  String id = "A00036757";

  if (pass == "1234" ) {
    Serial.print("car exists");
    digitalWrite(ledon, HIGH);
    myStepper.step(stepsPerRevolution / 3); //clockwise -OPEN GATE

    //if(digitalRead(sensor) == HIGH){

  if (digitalRead(sensor) == LOW) {

    digitalWrite(ledon, LOW);

    digitalWrite(ledoff, HIGH);
    myStepper.step(-stepsPerRevolution / 3); // CLOSE AFTER 5s COUNTERCLOCKWISE -  close gate
    digitalWrite(ledoff, LOW);
  else {
    Serial.print("NO CAR");

1 Answer 1


When you use delay(), your code stops and dont check anything at all until the delay is over, if you want to check for sensors during a timer, you have to use millis()

There is a lot of documentation and tutorials you can find to replace delay with millis when you know what you are searching for

Not the answer you're looking for? Browse other questions tagged or ask your own question.