Problem stopping DC Motor after each time the servo is closed [SOLVED]

Hi people,

I'm still newbie to it comes Arduino programming. What I'm trying to do is when Sensor detect smoke, open the servo. After sensor doesn't detect smoke anymore, closed the servo. After that, wait about 15 seconds, then move the DC motor. DC motor will move about 2 seconds, then dc motor stop. The DC motor will start again after each time servo is closed. I want to achieve where after each time servo is closed, DC motor will stop. But, I still can't implemented this to my project, because my knowledge still limit. Is this possible to achieve?

Here's my code:

#include <Servo.h>

Servo mainServo;

const unsigned long eventIntervalRED = 2000;
const unsigned long eventIntervalGREEN = 2000;
const unsigned long eventIntervalMotor = 2000;
unsigned long previousTime = 0;
unsigned long previousTimeMotor = 0;

const int motordcfan = 9;
int gasSensor = A0; // select input pin for gasSensor
int led_red = 12;
int led_green = 11;
int gas_value = 0; // variable to store the value coming from the sensor
int sensorThres = 255;
void setup()
{
  mainServo.attach(13);
  mainServo.write(0);
  pinMode(led_red, OUTPUT);
  pinMode(led_green, OUTPUT);
  pinMode(motordcfan, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  
  unsigned long currentTime = millis();
  unsigned long currentTimeMotor = millis();
  
  gas_value = analogRead(gasSensor); // read the value from the pot
  Serial.print("Gas Rate: ");
  Serial.println(gas_value);
  delay(100);
  if (gas_value >= sensorThres)
  {
    digitalWrite(led_red, HIGH);
    digitalWrite(led_green, LOW);
    if(currentTime - previousTime >= eventIntervalRED)
    {
      mainServo.write(180);
      previousTime = currentTime;
    }
    Serial.print("Open Servo");
  }
    
  else
  {
    mainServo.write(0);
    if(currentTime - previousTime >= eventIntervalGREEN)
    {
      digitalWrite(led_red, LOW);
      digitalWrite(led_green, HIGH);
      previousTime = currentTime;
    }
    Serial.print("Close Servo");
  }
  
  if (mainServo.read() == 0)
  {
    if(currentTimeMotor - previousTimeMotor >= eventIntervalMotor)
    {
      digitalWrite(motordcfan, HIGH);
      delay(2000);
      digitalWrite(motordcfan, LOW);
      previousTimeMotor = currentTimeMotor;
      return;
    }
  }
}

I'm apologize if any other topics are related to this, because I still can't understand it. I'm really appreciate to anyone who be able help me.

shouldn't previousTimeMotor be reset (to millis()) when the servo is closed?

sorry, when do you want the motor to start?

May you explain more to me? I'm still don't understand.

Everytime the servo is closed.

seems inconsistent

  • when above threshold, LED on
  • when falling below threshold, LED off and motor on for 2 seconds

see no need for timers

consider

#include <Servo.h>

Servo mainServo;

const int motordcfan = 9;
const int gasSensor = A0; // select input pin for gasSensor
const int led_red   = 12;
const int led_green = 11;

int gas_value; // variable to store the value coming from the sensor

const int SensorThres = 255;

enum { Idle, Open };
int state = Idle;

// -----------------------------------------------------------------------------
void setup ()
{
    mainServo.attach (13);
    mainServo.write (0);

    pinMode (led_red,    OUTPUT);
    pinMode (led_green,  OUTPUT);
    pinMode (motordcfan, OUTPUT);

    Serial.begin (9600);
}

void loop ()
{
    gas_value = analogRead (gasSensor); // read the value from the pot
    Serial.print ("Gas Rate: ");
    Serial.println (gas_value);

    if (state == Idle && gas_value >= SensorThres) {
        mainServo.write (180);

        digitalWrite (led_red, HIGH);
        digitalWrite (led_green, LOW);
        Serial.println ("Open Servo");

        state = Open;
    }

    else if (state == Open && (SensorThres - 5) > gas_value) {
        mainServo.write (0);
        Serial.println ("Close Servo - Motor On");

        digitalWrite (motordcfan, HIGH);
        delay (2000);
        digitalWrite (motordcfan, LOW);
        Serial.println ("Motor Off");

        digitalWrite (led_red, LOW);
        digitalWrite (led_green, HIGH);

        state = Idle;
    }

    delay (1000);    // slow prints
}

Thank you so much for helping me out this problem. Now, the program is working as it should. This topics now is solved. Have a nice day sir. :slight_smile:

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