I thought I understand how to do it with my attachedinterrupt but it is not running at all.
My PIR is connected to D3 on the ESP. Should I have to change it now because of arduino Pin 2 and Pin 3?
#define BLYNK_PRINT Serial
#include <Adafruit_NeoPixel.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <SPI.h>
#include <WiFiUdp.h>
// LED Stuff
#define PIN D5
#define NUM_LEDS 37
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ400);
void showStrip();
void clearStrip();
void setPixel(int, int, int, int);
void fadeToBlack(int, byte);
void ZeRGBa();
void colorWipe(int, int, int, unsigned long);
void CylonBounce(int, int, int, int, int, int);
void meteorRain(int, int, int, int, int, boolean, unsigned long);
void Strobe(int, int, int, int, int, int);
// normal Void Declaration
void WIFI_check();
void ALARM_time_check();
void ALARM_on();
void PIR_time_check();
void PIR_on();
void WDAY_and_NOW();
void NTP_update();
void PIN_change();
void bdelay(unsigned long);
// WiFi Stuff
**** STUF AS USAL ****
// Variables
int red = 1;
int green = 0;
int blue = 2;
int connectionSuccess = 0;
// Alarm Variables
int ALARM_start = 21600;
int ALARM_end_time;
int ALARM_timer = 10000;
int ALARM_effect = 4;
int ALARM_counter;
char ALARM_days_start[] = "1,2,3,4,5";
bool ALARM_days[] = {true, true, true, true, true, false, false};
// Motion Sensor Variables
int PIR_pin = D3;
int PIR_val;
int PIR_start = 79200;
int PIR_stop = 21600;
int PIR_end_time;
int PIR_timer = 10 * 1000;
int PIR_effect = 1;
char PIR_days_start[] = "1,2,3,4,5,6,7";
bool PIR_days[] = {true, true, true, true, true, true, true};
bool PIR_active = false;
const long utcOffsetInSeconds = 2 * 60 * 60;
bool debug = true;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "10.0.4.1", utcOffsetInSeconds);
unsigned long TIME_sync_millis = 0;
int TIME_sync_timer = 60;
int now;
int finish;
int weekday;
int WIFI_check_timerID;
int NTP_update_timerID;
int ALARM_check_timerID;
int ALARM_on_timerID;
int PIR_on_timerID;
unsigned long currentMillis;
unsigned long colorWipeMillis;
BlynkTimer timer;
void setup()
{
Serial.begin(115200);
unsigned long started = millis();
Serial.println();
WiFi.begin(ssid, pass); //Blynk.begin(auth, ssid, pass, serv, 8080);
Serial.println(String("Connecting to ") + ssid + " ...");
while (WiFi.status() != WL_CONNECTED) {
if (millis() - started < 20 * 1000) {
yield();
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Wifi works, now try Blynk connection");
delay(2 * 1000);
Blynk.config(auth, serv, 8080);
Blynk.connect(30 * 1000);
if (Blynk.connected() == true) {
delay(2000);
Blynk.run();
Serial.println("Blynk is online");
}
}
timeClient.begin();
WIFI_check_timerID = timer.setInterval(5 * 1000L, WIFI_check);
ALARM_check_timerID = timer.setInterval(60 * 1000L, ALARM_time_check);
ALARM_on_timerID = timer.setInterval(1 * 1000L, ALARM_on);
NTP_update_timerID = timer.setInterval(120 * 1000L, NTP_update);
timeClient.begin();
pixels.begin();
Blynk.virtualWrite(V0, red);
Blynk.virtualWrite(V1, green);
Blynk.virtualWrite(V2, blue);
Blynk.virtualWrite(V5, "First Start");
Blynk.virtualWrite(V8, ALARM_timer);
Blynk.virtualWrite(V11, ALARM_start, 0, 0, ALARM_days_start);
Blynk.virtualWrite(V14, ALARM_effect);
Blynk.virtualWrite(V20, TIME_sync_timer);
if (debug) {
Blynk.virtualWrite(V21, 1);
}
else {
Blynk.virtualWrite(V21, 2);
}
timeClient.update();
timer.disable(ALARM_on_timerID);
attachInterrupt(PIR_pin, PIN_change, CHANGE);
digitalWrite(PIR_pin, LOW);
}
void loop()
{
timer.run();
Blynk.run();
}
void ICACHE_RAM_ATTR PIN_change()
{
if (digitalRead(PIR_pin == HIGH)) {
if (debug) {
Serial.println("PIN Change with PIR done");
}
PIR_time_check();
}
else {
if (timeClient.getHours() * 60 * 60 + timeClient.getMinutes() * 60 + timeClient.getSeconds() > PIR_end_time) {
clearStrip();
timer.disable(PIR_on_timerID);
}
}
}
void WIFI_check()
{
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.println("Lost WiFi Connection, trying to reconnect... ");
connectionSuccess = 1;
}
else if (WiFi.status() == WL_CONNECTED && connectionSuccess == 1) {
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Wifi works, now try Blynk connection");
bdelay(2000);
Blynk.config(auth, serv, 8080);
Blynk.connect(30 * 1000);
if (Blynk.connected() == true) {
bdelay(2000);
Blynk.run();
Serial.println("Blynk is online");
}
connectionSuccess = 0;
}
}
void NTP_update()
{
timeClient.update();
if (debug) {
Serial.println("Syncing NTP time");
}
}
/* not needed anymore
void TIME_check()
{
ALARM_time_check();
//PIR_time_check(); // outcommented because of softwareinterrupt
}*/
void WDAY_and_NOW()
{
weekday = timeClient.getDay();
if (debug) {
Serial.println(String("Zeit: ") + timeClient.getHours() + ":" + timeClient.getMinutes());
}
if (timeClient.getDay() == 0) {
weekday = 7;
}
now = timeClient.getHours() * 60 * 60 + timeClient.getMinutes() * 60;
}
/*****************************************************
ALARM Time Check
*****************************************************/
void ALARM_time_check()
{
WDAY_and_NOW();
if (now == ALARM_start && ALARM_days[weekday] && ALARM_start >= 0) {
clearStrip();
ALARM_end_time = now + ALARM_timer + timeClient.getSeconds();
timer.enable(ALARM_on_timerID);
//ALARM_on_timerID = timer.setTimer(1000, ALARM_on, ALARM_timer);
if (debug) {
Serial.println(String("ALARM Effect started at: ") + timeClient.getHours() + ":" + timeClient.getMinutes() + ":" + timeClient.getSeconds());
}
}
}
void ALARM_on()
{
if (timeClient.getHours() * 60 * 60 + timeClient.getMinutes() * 60 + timeClient.getSeconds() <= ALARM_end_time) {
if (ALARM_effect == 1) {
ZeRGBa();
} else if (ALARM_effect == 2) {
colorWipe(10, 45, 50, 50);
colorWipe(0, 0, 0, 50);
} else if (ALARM_effect == 3) {
CylonBounce(255, 0, 0, 4, 20, 60);
} else if (ALARM_effect == 4) {
meteorRain(255, 127, 127, 10, 64, true, 30);
} else if (ALARM_effect == 5) {
Strobe(255, 0, 255, 10, 50, 1000); // Slower: Strobe(0xff, 0x77, 0x00, 10, 100, 1000);
}
}
else {
if (debug) {
Serial.println(String("ALARM Effect ended at: ") + timeClient.getHours() + ":" + timeClient.getMinutes() + ":" + timeClient.getSeconds());
}
timer.disable(ALARM_on_timerID);
}
}
/*****************************************************
PIR Time Check START
*****************************************************/
void PIR_time_check()
{
if (debug) {
Serial.println("PIR_time_check");
}
WDAY_and_NOW();
if (PIR_days[weekday] && PIR_start >= 0 && PIR_stop >= 0) {
// Time between 5:00 and 6:00 f.e.
if (PIR_start < PIR_stop) {
finish = PIR_stop;
}
// Time between 6:00 and 5:00 f.e.
else if (PIR_start > PIR_stop) {
finish = PIR_stop + 24 * 60 * 60;
// Now is at time on the next day after Start
// Start = 20:00 | Stop = 4:00 | Now = 2:00
if (now < PIR_start) {
now = now + 24 * 60 * 60;
}
}
}
// Check if Now is between Start and Stop
if (now >= PIR_start && now < finish && !timer.isEnabled(ALARM_on_timerID)) {
PIR_end_time = now + PIR_timer + timeClient.getSeconds();
timer.enable(PIR_on_timerID);
if (debug) {
Serial.println("PIR Detection IS Active");
}
}
else {
timer.disable(PIR_on_timerID);
if (debug) {
Serial.println("PIR Detection NOT Active");
}
}
}
void PIR_on()
{
if (debug) {
Serial.println(String("PIR was running at: ") + timeClient.getHours() + ":" + timeClient.getMinutes() + ":" + timeClient.getSeconds());
}
if (timeClient.getHours() * 60 * 60 + timeClient.getMinutes() * 60 + timeClient.getSeconds() <= PIR_end_time) {
if (PIR_effect == 1) {
ZeRGBa();
}
else if (PIR_effect == 2) {
colorWipe(50, 10, 45, 25);
colorWipe(0, 0, 0, 25);
}
else if (PIR_effect == 3) {
CylonBounce(255, 0, 0, 4, 20, 60);
}
else if (PIR_effect == 4) {
meteorRain(255, 127, 127, 10, 64, true, 30);
}
else if (PIR_effect == 5) {
Strobe(255, 0, 255, 10, 50, 1000); // Slower: Strobe(0xff, 0x77, 0x00, 10, 100, 1000);
}
}
else {
clearStrip();
timer.disable(PIR_on_timerID);
}
}
void bdelay(unsigned long milli)
{
unsigned long end_time = millis() + milli;
while (millis() < end_time)
{
if (Blynk.connected())
{
Blynk.run();
}
yield();
}
}
/********************* LED Stuff ***********************/
void showStrip() {
pixels.show();
}
void clearStrip() {
pixels.fill(0, 0, NUM_LEDS);
showStrip();
}
void setPixel(int Pixel, int red, int green, int blue) {
pixels.setPixelColor(Pixel, pixels.Color(red, blue, green));
}
/********************* LED Effects *********************/
void ZeRGBa()
{
for (int i = 0; i < NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
}
}
void colorWipe(int red, int green, int blue, unsigned long SpeedDelay)
{
if (colorWipeMillis - currentMillis >= SpeedDelay) {
colorWipeMillis = currentMillis;
for (int i = 0; i < NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
}
}
}
void CylonBounce(int red, int green, int blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
for (int i = 0; i < NUM_LEDS - EyeSize - 2; i++) {
clearStrip();
setPixel(i, red / 10, green / 10, blue / 10);
for (int j = 1; j <= EyeSize; j++) {
setPixel(i + j, red, green, blue);
}
setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
showStrip();
bdelay(SpeedDelay);
}
bdelay(ReturnDelay);
for (int i = NUM_LEDS - EyeSize - 2; i > 0; i--) {
clearStrip();
setPixel(i, red / 10, green / 10, blue / 10);
for (int j = 1; j <= EyeSize; j++) {
setPixel(i + j, red, green, blue);
}
setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
showStrip();
bdelay(SpeedDelay);
}
bdelay(ReturnDelay);
}
void meteorRain(int red, int green, int blue, int meteorSize, int meteorTrailDecay, boolean meteorRandomDecay, unsigned long SpeedDelay) {
clearStrip();
for (int i = 0; i < NUM_LEDS + NUM_LEDS; i++) {
// fade brightness all LEDs one step
for (int j = 0; j < NUM_LEDS; j++) {
if ( (!meteorRandomDecay) || (random(10) > 5) ) {
fadeToBlack(j, meteorTrailDecay);
}
}
// draw meteor
for (int j = 0; j < meteorSize; j++) {
if ( ( i - j < NUM_LEDS) && (i - j >= 0) ) {
setPixel(i - j, red, green, blue);
}
}
showStrip();
bdelay(SpeedDelay);
}
}
void fadeToBlack(int ledNo, byte fadeValue) {
uint32_t oldColor;
uint8_t r, g, b;
oldColor = pixels.getPixelColor(ledNo);
r = (oldColor & 0x00ff0000UL) >> 16;
g = (oldColor & 0x0000ff00UL) >> 8;
b = (oldColor & 0x000000ffUL);
r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 256);
g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 256);
b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 256);
pixels.setPixelColor(ledNo, r, g, b);
}
void Strobe(int red, int green, int blue, int StrobeCount, int FlashDelay, int EndPause) {
for (int j = 0; j < StrobeCount; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
}
showStrip();
bdelay(FlashDelay);
clearStrip();
bdelay(FlashDelay);
}
bdelay(EndPause);
}
/********************* LED Effects *********************/
BLYNK_WRITE(V0)
{
red = param.asInt();
}
BLYNK_WRITE(V1)
{
green = param.asInt();
}
BLYNK_WRITE(V2)
{
blue = param.asInt();
}
BLYNK_WRITE(V8)
{
ALARM_timer = param.asInt() * 1000;
}
BLYNK_WRITE(V11)
{
TimeInputParam t(param);
if (t.hasStartTime()) {
ALARM_start = t.getStartHour() * 60 * 60 + t.getStartMinute() * 60 + t.getStartSecond();
}
else {
ALARM_start = -1;
}
for (int i = 1; i <= 7; i++) {
ALARM_days[i] = t.isWeekdaySelected(i);
}
ALARM_time_check();
}
BLYNK_WRITE(V14)
{
switch (param.asInt()) {
case 1: {
//ZeRGBa
ALARM_effect = 1;
break;
}
case 2: {
//colorWipe
ALARM_effect = 2;
break;
}
case 3: {
//Cylon
ALARM_effect = 3;
break;
}
case 4: {
//Meteor
ALARM_effect = 4;
break;
}
case 5: {
//Strobe
ALARM_effect = 5;
break;
}
}
}
BLYNK_WRITE(V20)
{
TIME_sync_timer = param.asInt();
if (debug) {
Serial.println(String("Time Synchronisation every: ") + TIME_sync_timer + "Minutes");
}
}
BLYNK_WRITE(V21)
{
int debug_switch = param.asInt();
if (debug_switch == 1) {
debug = true;
}
else {
debug = false;
}
}
EDIT: Wow just same time you posted your answer
hehe
So ok, I don’t have to change the PIN D3. But my attached Interrupt won’t be run at all.
Is there anything special I have to do with that Interrupt on Wemos D1?
There was an error after every blynk connect while not having ICACHE_RAM_ATTR added to the PIN_change void.