FOR and Virtual Pins

Hi guys,
I’m trying to make an alarm clock, with sunset, but I’m not able to make the “for” loop stop when the virtual pin is set to “LOW”
I’m Brazilian and I’ve already apologized for my english, I hope you understand.

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

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

int alarm;
int lamp = 4;
int off = 14;
int timer;


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(lamp, OUTPUT);
  pinMode(alarm, OUTPUT);
  pinMode(off, OUTPUT);
}



BLYNK_WRITE(V6)
{
  int alarm = param.asInt();
  if (alarm == 1)
  {
    digitalWrite(lamp, HIGH);
  }
  else
  {
    digitalWrite(lamp, LOW);
  }
}

BLYNK_WRITE(V5)
{

  Serial.print("Got a value: ");
  Serial.println(param.asStr());
  int timer = param.asInt();

  if (timer == HIGH )
  {
    for (int i = 0; i <= 255; i++) {
      Serial.println(i);
      analogWrite(lamp, i);
      if (digitalRead(timer) == LOW) {
        analogWrite(lamp, LOW);
        delay(50);
      }

    }
  }

  if (timer == LOW)
  {
    {
      analogWrite(lamp, LOW);
      Serial.println("OFF");
      delay(30);

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

getting out of a loop can be done with break. However in your code I see a ‘digitalRead’ (reading a physical pin) while in your question you talk about a virtual pin. So what is it exactly you wish to achieve, best to give it in ‘pseudo’ code what you wish to achieve.

He wants to turn on/off a digital pin with a blynk virtual pin (which makes sense to me).

However, I do see a delay in the code… that cannot be good. 255 * 50ms amounts to 12 seconds in total (which is too much in any case).

yeah noticed that too, but without knowing what he wants to achieve its a bit of shooting in the dark.

But from the description and the code I get the jest of it.

It basically requires quite a different code structure. Let me think about this, I was planning to create something like this anyway, so I don’t mind looking a bit harder at this.

To quote Arny: Ill be back.

1 Like

Can be write as:

BLYNK_WRITE(V6)
{
digitalWrite(lamp,param.asInt());
}

i’m trying to understad what is supposed to do the program code… but i think the most importat info is missed…

What widget interacts with the virtual pins? and for what? trying to do what?

I’m having trouble following you in your code and I think you’ve made a mistake with using ‘timer’ variable both global AND local, but this is what I came up with (UNTESTED):

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

char auth[] = "do not";
char ssid[] = "give this";
char pass[] = "info online!!!";

int alarm;
int lamp = 4;
int off = 14;
int counter = 0;
bool wakeUp =false;

BlynkTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(lamp, OUTPUT);
  pinMode(alarm, OUTPUT);
  pinMode(off, OUTPUT);
  timer.setInterval(50, checkLamp); //this will run the checkLamp routine every 50ms
}

BLYNK_WRITE(V6)
{
  digitalWrite(lamp,param.asInt());
}

BLYNK_WRITE(V5)
{
  Serial.print(String("Got a value: ") + param.asStr());
  wakeUp = (bool) param.asInt();
}

checkLamp()
{
//I'm guessing you also have a physical button to turn the alarm off, you can do that check here as well and set the bool accordingly.

  if (wakeUp && counter <= 255){
    analogWrite(lamp, counter++);
  } else {
    analogWrite(lamp, LOW);
	counter = 0;
  }
}

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

thanks for answering!
Actually I’m trying to make an alarm clock using timer wigdet (V5) but as the alarm will work as sunrise (01-255 in 10min +/-) and I need to stop the “for” using a virtual button in those 10 minutes if necessary

Ok, now is more clear… i’ll try to make a sketch :wink:

Using @wolph42 code and assuming that the off button is a switch and it’s attached to v6…simply do:

BLYNK_WRITE(V6) 
{
  wakeUp = (bool) param.asInt(); //when a LOW value is received breaks the for :slight_smile: 
  digitalWrite(lamp,param.asInt());
}
BLYNK_WRITE(V5)
{
  wakeUp = (bool) param.asInt();
  if (wakeUp)
  {
    Serial.print("Time to wake up!!");
    Blynk.virtualWrite(V6,true); //Set «off alarm switch» to HIGH waiting to be pressed for disable alarm
  }
}

ah ok 10 minutes… then you get something like this:
(blindly using @trystan4861 input as well )

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

char auth[] = "do not";
char ssid[] = "give this";
char pass[] = "info online!!!";

int alarm;
int lamp = 4;
int off = 14;
int counter = 0;
int timerId;
bool wakeUp =false;
bool wakingUp = false;

BlynkTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(lamp, OUTPUT);
  pinMode(alarm, OUTPUT);
  pinMode(off, OUTPUT);
}

BLYNK_WRITE(V6) 
{
  wakeUp = (bool) param.asInt(); //when a LOW value is received stop the timer, turn off lamp
  if (!wakeUp) {
    digitalWrite(lamp,LOW);
	timer.deleteTimer(timerId);
	counter = 0;
	wakingUp = false;
  }
}

BLYNK_WRITE(V5)
{
  wakeUp = (bool) param.asInt();
  if (wakeUp && !wakingUp)
  {
    Serial.print("Time to wake up!!");
    Blynk.virtualWrite(V6,true); //Set «off alarm switch» to HIGH waiting to be pressed for disable alarm
	timerId = timer.setTimer(round((10*60*1000)/255), increaseLight, 255);
	wakingUp = true; //prevent creating numerous timers. Probably redundant, but better safe then sorry.
  }
}

increaseLight()
{
    analogWrite(lamp, counter++);
}

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

What do this?

and @seuheroi why do something every 50ms if it’s needy to be done during a period of 10 minutes?

nothing as I removed it :smiley:
the intention was to check whether the lamp should go on every 50ms (as per original code) and then increase it value every 50ms which is about 12s NOT 10 min. When he mentioned the 10 minutes I changed the code so it runs every 2.35s

edit: no i was wrong, now I’ve removed it (I had overlooked that line)

yes, i know… but i was asking why he do it every 50ms (he used a delay for that) has no sense.

see the edit in my former post.

Thank you so much everyone who is trying to help! thank you so much

I’m have trouble with this delete timer, when a try turn off the lamp, the time return and start over.

make int timerId global and change “int timerId =” at BLYNK_WRITE(V5) with “timerId =” maybe will do it

good catch, missed that one! former post updated.

Only 4 days with Blynk but with about 25 years programming… makes a debugger eye :yum:

1 Like

Sorry, but the error remains the same.:roll_eyes:

‘deleteTimer’ was not declared in this scope

of course … you must include the right library to use that…

Check this

but if you are using a BlynkTimer… you must write: timer.deleteTimer(timerId); instead deleteTimer(timerId);