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.
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.
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
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
}
}
nothing as I removed it
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)