Arduino: NodeMCU + PIR Sensor + LED Panel

Делаем детектор движения с модулем реле.
И отображаем количество срабатываний на led панели.

#define relPin D6
#define pirPin D4

#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <ESP8266WiFi.h>

Max72xxPanel matrix = Max72xxPanel(D3, 1, 1);
int pirValue;
bool pirDelay = false;
unsigned long resetTimer = millis();
unsigned long pirTimeout;
unsigned long ticker_next;
int tape_int = 0;
String tape = String(tape_int);
int spacer = 2;
int width = 5 + spacer;

void setup() {
  pinMode(relPin, OUTPUT);
  pinMode(pirPin, INPUT);
  matrix.setIntensity(10);
}
  
void loop(){
  resetCounter();
  if (pirDelay == false) {
    getPirValue();
  } else if (pirDelay == true) {
      pirValue = digitalRead(pirPin);
      if (millis() - pirTimeout > 5000 && pirValue != HIGH) {
      pirDelay = false;
  }
  }
}

void resetCounter() {
  if (millis() - resetTimer > 3600000) {
    tape_int = tape_int - tape_int;
    tape = tape_int;
    resetTimer = millis();
  }
  handleTicker();
}

void getPirValue() {
  pirValue = digitalRead(pirPin);
  pirTimeout = millis();
  if (pirValue == HIGH) { 
    digitalWrite(relPin, HIGH);
    tape_int = tape_int + 1;
    tape = tape_int;
    pirDelay = true;
  } else {
    digitalWrite(relPin, LOW);
    }
}

String utf8rus(String source) {
  int i,k;
  String target;
  unsigned char n;
  char m[2] = { '0', '\0' };

  k = source.length(); i = 0;
  while (i < k) {
    n = source[i]; i++;
    if (n >= 0xC0) {
      switch (n) {
        case 0xD0: {
          n = source[i]; i++;
          if (n == 0x81) { n = 0xA8; break; }
          if (n >= 0x90 && n <= 0xBF) n = n + 0x2F;
            break;
        }
        case 0xD1: {
          n = source[i]; i++;
          if (n == 0x91) { n = 0xB7; break; }
          if (n >= 0x80 && n <= 0x8F) n = n + 0x6F;
            break;
        }
      }
    } // switch
    m[0] = n; target = target + String(m);
  }
  return target;
}

void handleTicker() {
  for ( int i = 0 ; i < width * tape.length() + matrix.width() - 1 - spacer; i++ ) {
    matrix.fillScreen(LOW);

    int letter = i / width;
    int x = (matrix.width() - 1) - i % width;
    int y = (matrix.height() - 8) / 2;

    while ( x + width - spacer >= 0 && letter >= 0 ) {
      if ( letter < tape.length() ) {
        matrix.drawChar(x, y, tape[letter], HIGH, LOW, 1);
      }

      letter--;
      x -= width;
    }
    matrix.write();
    delay(50);
  }
}
Поделиться:

Нет комментариев