With my code, the motor is rotating CW and anti-CW. I want it to stop after some time like 5 seconds. How I can limit it so that it will stop after 5 sec although it is continually receiving data from the sensor.
code:
const int pin1=11;
const int pin2=10;
void setup()
{
pinMode (pin1,OUTPUT);
pinMode (pin2, OUTPUT);
pinMode (9,OUTPUT);
Serial.begin(9600);
}
void loop()
{
int ldrstatus=analogRead(A0);
analogWrite(9,255);
if (ldrstatus<=30)
{
digitalWrite(pin1,HIGH);
digitalWrite(pin2,LOW);
delay (2000);
}
else
{
digitalWrite(pin2,HIGH);
digitalWrite(pin1,LOW);
delay(2000);
}
Serial.println(ldrstatus);
}
LarryD
April 23, 2021, 8:41pm
2
In the Arduino IDE, use Ctrl T to format your code then copy the complete sketch . Use the </> button from the ‘reply menu’ to attach the copied sketch.
Never ever use delay() in your sketches; at least not until you know why you shouldn’t.
Please review these:
NOTE for newcomers. The demo is entirely contained in this Post and Reply #1. There is no need to read further unless you are interested. ...added 25Sep2014
There have been a few occasions recently where newcomers seemed to have difficulty applying the "blink without delay" and "state" concepts to a sketch that is intended to manage a number of concurrent actions.
Its very time consuming to respond with suitable code examples - particularly as every case is a little different.
I have prepared…
1 Like
const int pin1=11;
const int pin2=10;
void setup()
{
pinMode (pin1,OUTPUT);
pinMode (pin2, OUTPUT);
pinMode (9,OUTPUT);
Serial.begin(9600);
}
void loop()
{
int ldrstatus=analogRead(A0);
analogWrite(9,255);
if (ldrstatus<=30)
{
digitalWrite(pin1,HIGH);
digitalWrite(pin2,LOW);
delay (2000);
}
else
{
digitalWrite(pin2,HIGH);
digitalWrite(pin1,LOW);
delay(2000);
}
Serial.println(ldrstatus);
}
LarryD
April 23, 2021, 9:34pm
4
Are you aware that delay(2000); stops normal program execution for 2 seconds ?
Have you reviewed the links offered to you ?
I want to stop the motor after 5 sec. I want to limit it otherwise it will be rotating continuously.
Let's say that the sensor gets a value less than 30, so the motor rotates. You could change your delay to 5000 and stop the motor afterwards.
When should a reading less than 30 be able to spin the motor again?
cange delay(2000) to delay(5000)
But simply delay does not stop the motor i think there would be some code for limiting
Hello
Well I think you need a timer function to control the run time for the motor and a switch/case to turn the direction either CW of CCW.
Yes but i don't know how to use timer functions.
LarryD
April 24, 2021, 3:50pm
12
Timers:
Did you look at the video from post #2 ?
LarryD
April 24, 2021, 8:52pm
13
Try this:
//DC motor control using ldr sensor Module
//
//Version 1.1
//
//https://forum.arduino.cc/t/dc-motor-control-using-ldr-sensor-module/852936/12
#define isMoving 0
#define isTimedOut 1
#define threshold 30
#define motorSpeed 50
#define motorStop 0
const byte heartbeatLED = 13;
const byte pin1 = 11;
const byte pin2 = 10;
const byte pinPWM = 9;
byte motorFlag = isTimedOut;
bool LDRstatus = false;
bool lastLDRstatus = false;
//Timing stuff
unsigned long heartbeatMillis;
unsigned long motorMillis;
const unsigned long heartbeatRate = 500ul; //1/2 second
const unsigned long motorOnTIME = 5000ul; //5 seconds
//****************************************************************************
void setup()
{
Serial.begin(9600);
pinMode(heartbeatLED, OUTPUT);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pinPWM, OUTPUT);
//disable the motor at power up
analogWrite(pinPWM, motorStop);
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
motorFlag = isTimedOut ;
//capture the current sensor reading
if (analogRead(A0) <= threshold)
{
lastLDRstatus = true;
}
else
{
lastLDRstatus = false;
}
} //END of setup()
//****************************************************************************
void loop()
{
//**********************
//is it time to toggle the heartbeat LED ?
if (millis() - heartbeatMillis >= heartbeatRate)
{
//restart the Timer
heartbeatMillis = millis();
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//**********************
//is it time to turn off the motor ?
if (motorFlag == isMoving && millis() - motorMillis >= motorOnTIME)
{
motorFlag = isTimedOut;
analogWrite(pinPWM, motorStop);
}
//**********************
checkLDR();
} //END of loop()
//****************************************************************************
void checkLDR()
{
int reading = analogRead(A0);
Serial.println(reading);
//**********************
//has the sensor changed threshold ?
if (reading <= threshold)
{
LDRstatus = true;
}
else
{
LDRstatus = false;
}
//**********************
if (lastLDRstatus != LDRstatus)
{
//********
if (motorFlag == isTimedOut && LDRstatus == true)
{
//update to the new state
lastLDRstatus = LDRstatus;
analogWrite(pinPWM, motorSpeed);
motorFlag = isMoving;
//restart the Timer
motorMillis = millis();
//motor forward ??
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
}
//********
if (motorFlag == isTimedOut && LDRstatus == false)
{
//update to the new state
lastLDRstatus = LDRstatus;
analogWrite(pinPWM, motorSpeed);
motorFlag = isMoving;
//restart the Timer
motorMillis = millis();
//motor REV ??
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
}
}
} //END of checkLDR()
//****************************************************************************
1 Like
I have tried this but this is not helping. it is giving me the same continuous rotation. the motor is not stopping, rather as the condition changes for LDR it reverses the direction of motor.
As Tom says:
Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Hi,
Thanks @arsalanmohammad87
LCD Module;
Motor Driver:
We need to see your power connections including gnds.
Have you got the UNO gnd connected to the 289 gnd?
Tom...
I am powering up UNO with USB cable and L298N with 12v dc power supply at 12v and GND pin