Controlling a dc motor after a period of time using Nodemcu & Blynk

Hello, i’m very new to arduino and still learning (no programming background). Please be gentle to me. Btw, i’m helping my daughter to do this project. She really likes electronic especially arduino.

I’m making a simple IOT system on how to open/close a curtain by using NodeMCU + L298N + Blynk

What i expect : 1. when i press V0 = The curtain is opening for 5 seconds then stop

  1. when i press V1 = The curtain is closing for 5 seconds then stop

*note that V1 and V2 are the switch button. Not push button. So, when V1-ON, V2 should OFF. Vice versa.

What happen : Nothing works.

*When i tried just by using NodeMCU, by giving the the pins HIGH or LOW. it works. So, it means that the wiring of the components and its pins are true.

It really confusing me. Sorry for my bad english.

I read the post by nick before posting the problem/topic, but i dont know how to post the code they others did (have a box for codes). Thanks so much

regards, HAMDAN

#code

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth = “c5342bebfbce468db903e859d769ea62”;
char ssid = " ";
char pass = " ";

int Positive = 15;
int Negative = 13;

long int stop1;

void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);

stop1=millis()+5000; // 5 seconds timer

pinMode(Positive, OUTPUT);
pinMode(Negative, OUTPUT);
}

void loop()
{
Blynk.run();
}

BLYNK_WRITE(V0)
{
if (millis()<stop1)
{
digitalWrite(Positive, LOW); // open curtain
digitalWrite(Negative, HIGH);
}

else
{
digitalWrite(Positive, LOW); // stop the operation after 5 seconds
digitalWrite(Negative, LOW);
}
}

BLYNK_WRITE(V1)
{
if (millis()<stop1)
{
digitalWrite(Positive, HIGH); // close curtain
digitalWrite(Negative, LOW);
}

else
{
digitalWrite(Positive, LOW); // stop the operation after 5 seconds
digitalWrite(Negative, LOW);
}
}

BLYNK_WRITE() is called when?

How long after the Arduino is reset do these calls happen? You assign a value to stop1 in setup(), but never change it.

It seems to me that you want to assign a value to stop1 (lousy name) in BLINK_WRITE() when the function is called to start the motor(s) moving.

You want, in loop(), to see if the motor(s) have started, because stop1 is not zero, and have been running for more than 5 seconds, because millis() - stop1 is greater than 5 seconds. See why stop1 is a crappy name?

What you care about is when the motors started moving, and now, so you can see how long they have been moving. You do not care about the stop time.

First of all, thanks Pauls. That's why i said i'm very new to arduino, i only understand a little bit, i also bought a very basic myduino kit to be learned.

About stop1, i search google "how to control a dc motor using arduino and blynk" and their code using stop1. so, i dont know whether we can change or not.

I dont know whether "BLYNK WRITE" should be written inside void loop () or after void loop (). and what is the effect if it is inside or outside. But trust me, i really work hard to understand how to do the arduino programming. i even stayed for 12 hours searching for examples and tutorials.

Actually, my daughters (9 years old) idea is to stop the curtain after a period of time. I test using nodemcu and motor driver, it took 5 seconds to fully open the curtain. Then, i googled, how to stop a dc motor after a given time, and the example shows the BLYNK WRITE is written after void loop (). I tried to understand but it seems like this is really not my things. Thats why im searching for help here.. Thanks so much Pauls.

Blynk example: https://community.blynk.cc/t/dc-motor-control/24515/6

so, i dont know whether we can change or not.

Its a name. You can use whatever name you like. jackLostHisPants is as good a name as stop1.

I dont know whether "BLYNK WRITE" should be written inside void loop () or after void loop ().

The function can be before loop() or after loop(), but it can not be inside loop().

What you need BLYNK_WRITE() to do is to turn the motor on and store the time that it did that.

loop() will then be responsible for turning the motor off.

Sorry for the late reply Pauls.. thanks btw.. i understand about it a lil bit.. can you suggest another way on how to stop the motor after a period of time ?

can you suggest another way on how to stop the motor after a period of time ?

No. The ONLY way that will work is to record when you turn the motor on, and then periodically (read that as "on every pass through loop()), see it it is time to turn the motor off.