[SOLVED] Alternatives to delay() - SimpleTimer isn't enough

Try timer library, it contains function “after”
t.after(millis(),function_callback);
though, idk if it will work with blynk.

1 Like

It doesn’t mean you won’t be able to call pumpOn() again. pumpOn() is a function which you have defined and written, so you can call it whenever you like. timerId is just a variable you can use to refer to a specific timer, so you can do things like check whether a certain timer is still running, reset it, or delete it. You don’t even need to use timerIds in the simplest case; if you write this line

setTimeout(1000, pumpOn);

That will cause pumpOn() to be called 1000 ms later.

Again, read the SimpleTimer documentation. It provides all the functionality you’re asking for.

1 Like

The interval is run everytime it is called, so even though it is deleted, every time a function runs with the interval in it, the interval is run…

1 Like

I think you need to have a play iwth SimpleTimer untill you realised it actually a very AdvancedTimer (see what I did there?)

You only need to set up a pumpOn() function with a timer. Very easy to do.

void pumpOn(){
  // do you pump on functions here
}

Then you can just call timer.setTimeout(1000, pumpOn); when ever you need to turn the pump on 1sec later.

Want to turn it off after XX seconds? Easy:

void pumpOn(){
  // do you pump on functions here
 timer.setTimeout(3600, pumpOff); // turn off after 1min
}

void pumpOff(){
  // do you pump off functions here
}

And if you’re a bit OCD about keeping things tidy then just add this function and call doPump() where ever you like.

void doPump(){
  timer.setTimeout(1000, pumpOn);
}
1 Like

@zadspecial - so basically, any “alternatives” to simpleTimer to do what you want are not even worth discussing…

1 Like

I was putting the timer.setTimeout() function inside the void setup() function instead of void loop() like it is usually done with timer.setInterval(). My problem is solved. The code runs flawlessly now. Thanks a lot, everyone.

BLYNK_WRITE(V1) {
if ( param.asInt() ) {
  digitalWrite(18, HIGH);
  delay(10);
  servo.write(255);
  delay(1500);
  digitalWrite(18, LOW);
} else {
  digitalWrite(18, HIGH);
  delay(10);
  servo.write(10);
  delay(1500);
  digitalWrite(18, LOW);
}

}

guys i want to remove delay function in this code. can anybody help me??
ps:-i don’t know coding

Copy my example a few posts up! Please try yourself first… and only ask for help if you really cant do it…

can you explain me how the simple timer works?

Simpletimer counts time in milliseconds and does things you tell it to do when a certain time is reached…

Nah man it would take me all day… any way… there are loads of good documents out there about it

http://playground.arduino.cc/Code/SimpleTimer

1 Like

can the millis function be used instead of this but doing the same thing as in code that is acctually written by you @Jamin

No, use SimpleTimer for everything.

@Sai_Khurana it’s easier to learn how to use SimpleTimer than trying to use millis().

1 Like

@Jamin here is what i want to happen
CASE 1 when the V1 goes HIGH

  1. Turn on the transistor on(HIGH) that is on pin A4
  2. set the PWM value on pin D11 = 255
  3. Turn of the transistor (LOW) after the servo has reached the desired position(which takes about 1500 milliseconds )
    CASE 2 when V1 goes LOW
  4. Turn on the transistor (HIGH)
  5. Set the PWM value on pin D11 = 10
  6. Turn of the transistor after 1500 milliseconds

Yes, English is not my first language so i overlook it when i am in a hurry
Sorry, I will take care of this in future.

what is easy to understand and implement SimpleTimer or millis()?

I hate millis()

Simpletimer is so… Simple…

1 Like
#include <SimpleTimer.h>

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 *
 * This example shows how to use ESP8266 Shield (with AT commands)
 * to connect your project to Blynk.
 *
 * WARNING!
 *   It's rather tricky to get it working, please read this article:
 *   https://github.com/blynkkk/blynk-library/wiki/ESP8266-with-AT-firmware
 *
 * Change WiFi ssid, pass, and Blynk auth token to run :)
 * Feel free to apply it to any other example. It's simple!
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include<Servo.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Dreamerz";
char pass[] = "pass";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial
Servo servo;
SimpleTimer timer;

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600


ESP8266 wifi(&EspSerial);


BLYNK_WRITE(V1) {
if ( param.asInt()== 1 ) {
  void enable(timer);
  digitalWrite(18, HIGH);
  servo.write(255);
   ??? //replace this with a function that can execute the following after 2 seconds
  digitalWrite(18, LOW);
}
}

BLYNK_WRITE(V1) 
{ if ( param.asInt() == 0 ) {
  void enable(timer);
  digitalWrite(18, HIGH);
  servo.write(10);
  ??? //replace this with a function that can execute the following after 2 seconds
  digitalWrite(18, LOW);
}
}





    
    

void setup()
{
  pinMode(18,OUTPUT);
  timer.setTimeout(2000, repeatMe);
  
 
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
}

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

help please. not able to understand simple timer
@Costas @Jamin

1 Like

Dont spam the forums with duplicate posts/threads

You ask a question once. If someone does not reply, do not make more threads about the same question.

I would like to point out that it is a public holiday long weekend here in New Zealand. It’s summer and im going to the beach.

Here is your answer, it compiles but untested. Im not going to be around for 24 hours to help but someone else here might.

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>
#include <SimpleTimer.h>

char auth[] = "auth";
char ssid[] = "Dreamerz";
char pass[] = "pass";

#define EspSerial Serial
Servo servo;
SimpleTimer timer;

#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

// THIS IS A FUNCTION WHICH YOU CAN PASS AND 
// ARGUMENT TO, AND CAN BE USED LOADS OF TIMES
// QUICKLY AND EASILY. YOU SHOULD ALWAYS BUILD
// FUNCTIONS TO DO REPEATED TASKS

void servo_ON(int data) {
  digitalWrite(18, HIGH);
  delay(10); // NO MORE THAN 25ms is OKAY
  servo.write(data); // PASSING data TO THE servo.write() COMMAND
}

// THE SIMPLE OFF FUNCTION. THIS ONE CANT HAVE AN ARGUMENT
// BECAUSE IT IS A CALLBACK FOR THE TIMER. WHEN THE setTimeout()
// COMPLETES, IT CALLS THIS FUNCTION
void servo_OFF() {
  digitalWrite(18, LOW);
}

BLYNK_WRITE(V1) {
  if ( param.asInt() ) {
    servo_ON(255); // ENTER MOVEMENT VALUE AS ARGUMENT
    timer.setTimeout(1500, servo_OFF); // AFTER 1500ms (1.5s), RUN servo_OFF() ONCE
  } else {
    servo_ON(10); // ENTER MOVEMENT VALUE AS ARGUMENT
    timer.setTimeout(1500, servo_OFF);
  }

//--------------

  void setup() {
    pinMode(18, OUTPUT);
    Serial.begin(9600);
    delay(10);
    EspSerial.begin(ESP8266_BAUD);
    delay(10);
    Blynk.begin(auth, wifi, ssid, pass);
  }

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

So simple :slight_smile:

Enjoy the beach @Jamin

@Jamin thank you so much. i won’t spam the forums again. SORRY!!

1 Like