有人可以验证我的Arduino电子门代码

时间:2018-12-28 17:46:07

标签: arduino nodemcu blynk

电闸根本不执行代码规定的操作。继电器闪烁问题,NODE MCU变得无响应,无法执行预期的事情。

这是用于使用Blynk应用控制多部电话的电子门。

电路板的设置包括使用继电器,继电器将被打开和关闭,以控制通往两个门的电源,并改变将门的方向位置反转的电源的方向,我还使用了移位寄存器由于NODE MCU上的针脚不够,因此可以控制发送到门的信号(我在代码的顶部包括了移位寄存器中哪个针的功能注释)。

我已经在一个较小的规模上尝试了该系统,该系统仅包括使用一个门臂来测试代码是否可以正常工作,然后我将其扩展到代码中以包括两个门-这包括必须添加其他门逻辑,如果一个门已经到达目的地,则允许一个门继续打开/关闭(每个门的目的地/终点由每个电动机两端的按钮确定,当按下相应按钮时,它将随后打开/关闭)允许逻辑查看状态变化,但这确实起作用,但是它确实会轻拂继电器的开和关,即使一个门还没有到达目的地,也会停止向门的两侧供电。

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Shift Regsiter
// 2 - Power Supply
// 3 - Gate Left Power
// 4 - Gate Right Power
// 6 - Gate Switch
// 7 - LEDs

// Button stoppers
int btnLeftStopperOpened = D5;
int btnLeftStopperClosed = D6;
int btnRightStopperOpened = D7;
int btnRightStopperClosed = D3;

int btnLeftStopperOpenedState = LOW;
int btnLeftStopperClosedState = LOW;
int btnRightStopperOpenedState = LOW;
int btnRightStopperClosedState = LOW;

// Shift register pins
int latchPin = D0;
int clockPin = D1;
int dataPin = D2;
byte data = 0b00000000;

// WiFi details
char auth[] = "0a5a18c1a9f2457eb842013ec8f5fef3";
char ssid[] = "SKYCEDB2";
char pass[] = "LVDLBSBQSC";

// Blynk digital LED
WidgetLED indicatorLED(V3);

// Button booleans
bool openGateBool = false;
bool closeGateBool = false;
bool emergencyStopBool = false;

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  // Inputs
  pinMode(btnLeftStopperOpened, INPUT);
  pinMode(btnLeftStopperClosed, INPUT);
  pinMode(btnRightStopperOpened, INPUT);
  pinMode(btnRightStopperClosed, INPUT);

  // Outputs
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  // Clear Shift Register
  bitClear(data, 2);
  bitClear(data, 3);
  bitClear(data, 4);
  bitClear(data, 6);
  bitClear(data, 7);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, data);
  digitalWrite(latchPin, HIGH);
}

// Blynk virtual buttons
BLYNK_WRITE(V0) {
  openGateBool = !openGateBool;
}

BLYNK_WRITE(V1) {
  closeGateBool = !closeGateBool;
}

BLYNK_WRITE(V2) {
  emergencyStopBool = !emergencyStopBool;
}

void loop() {
  Blynk.run();

  // Detect Button States
  btnLeftStopperOpenedState = digitalRead(btnLeftStopperOpened);
  btnRightStopperOpenedState = digitalRead(btnRightStopperOpened);
  btnLeftStopperClosedState = digitalRead(btnLeftStopperClosed);
  btnRightStopperClosedState = digitalRead(btnRightStopperClosed);

  // Stability Delay
  delay(10);

  // Open gate function
  if((openGateBool == true) && (closeGateBool == false)) {

    // Initial Gate Check
    if((btnLeftStopperOpenedState == LOW) && (btnRightStopperOpenedState == LOW)) {
      bitSet(data, 2);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    }

    // If both gates are opened
    if((btnLeftStopperOpenedState == HIGH) && (btnRightStopperOpenedState == HIGH)) {
      bitClear(data, 2);
      bitClear(data, 3);
      bitClear(data, 4);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);

      // Blynk LED indicator
      Blynk.setProperty(V3, "color", "#23C48E");
      indicatorLED.on();
    }

    // Left Gate Opening
    if(btnLeftStopperOpenedState == LOW) {
      bitSet(data, 3);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    } else {
      bitClear(data, 3);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    }

    // Right Gate Opening
    if(btnRightStopperOpenedState == LOW) {
      bitSet(data, 4);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    } else {
      bitClear(data, 4);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    }

    // Emergency Stop button
    if(emergencyStopBool == true) {
      bitClear(data, 2);
      bitClear(data, 3);
      bitClear(data, 4);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);

      // Blynk LED indicator
      Blynk.setProperty(V3, "color", "#D3435C");
      indicatorLED.on();
      delay(900);
      indicatorLED.off();
      delay(900);
    }
  }

  // Close gate function
  if((openGateBool == false) && (closeGateBool == true)) {

    // Initial Gate Check
    if((btnLeftStopperClosedState == LOW) && (btnRightStopperClosedState == LOW)) {
      bitSet(data, 2);
      bitSet(data, 6);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    }

    // If both gates are closed
    if((btnLeftStopperClosedState == LOW) && (btnRightStopperClosedState == LOW)) {
      bitClear(data, 2);
      bitClear(data, 3);
      bitClear(data, 4);
      bitClear(data, 6);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);

      // Blynk LED indicator
      Blynk.setProperty(V3, "color", "#D3435C");
      indicatorLED.on();
    }

    // Left Gate Closing
    if(btnLeftStopperClosedState == LOW) {
      bitSet(data, 3);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    } else {
      bitClear(data, 3);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    }

    // Right Gate Closing
    if(btnRightStopperClosedState == LOW) {
      bitSet(data, 4);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    } else {
      bitClear(data, 4);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);
    }

    // Emergency Stop button
    if(emergencyStopBool == true) {
      bitClear(data, 2);
      bitClear(data, 3);
      bitClear(data, 4);
      bitClear(data, 6);
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, MSBFIRST, data);
      digitalWrite(latchPin, HIGH);

      // Blynk LED indicator
      Blynk.setProperty(V3, "color", "#D3435C");
      indicatorLED.on();
      delay(900);
      indicatorLED.off();
      delay(900);
    }
  }

  // Checks gate status
  if((btnLeftStopperClosedState == LOW) && (btnRightStopperClosedState == LOW) && (btnLeftStopperOpenedState == LOW) && (btnRightStopperOpenedState == LOW)) {
    indicatorLED.off();
  }

  // Checks if both buttons are false
  if((openGateBool == false) && (closeGateBool == false)) {
    bitClear(data, 2);
    bitClear(data, 3);
    bitClear(data, 4);
    bitClear(data, 6);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, data);
    digitalWrite(latchPin, HIGH);
  }

  // Emergency Stop button
  if(emergencyStopBool == true) {
    bitClear(data, 2);
    bitClear(data, 3);
    bitClear(data, 4);
    bitClear(data, 6);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, data);
    digitalWrite(latchPin, HIGH);

    // Blynk LED indicator
    Blynk.setProperty(V3, "color", "#D3435C");
    indicatorLED.on();
    delay(900);
    indicatorLED.off();
    delay(900);
  }

} // END //

当按下Blynk应用程序上的OPEN按钮时,代码将执行检查以查看OPEN状态按钮是否设置为HIGH-如果未设置为ON,则它将打开电源,然后执行另一项检查,该检查将控制每个门的电源直到到达目的地为止,一旦到达目的地便会停止。 Blynk应用程序上的“关闭”按钮也将发生同样的情况,但包括将开关与电源一起打开。还有一个紧急停止按钮,允许在Blynk应用程序中按下该按钮以关闭所有内容。

  • 相反,发生的事是一个按钮起作用(我已验证以确保虚拟引脚正确)

  • 紧急按钮没有停止

  • 如果一个门到达目的地,它将停止两个门,而不仅仅是一个门

  • 在任一登机口都已达到目的地目标时,继电器会不断闪烁,闪烁

  • NODE MCU有时会自行锁定

0 个答案:

没有答案