首页 > 解决方案 > 希望你能帮助一些错误和改进的代码

问题描述

我编写了一个代码,旨在根据电池是充满还是空来自动打开和关闭发电机。

lcd.begin() 和 lcd.clear() 中有一些错误(它们都不起作用)。错误:无效使用非静态成员函数。谢谢!

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool Settings = false;
unsigned long time1;
#define RELAY_PORT 10
float Voltage = 0.00;
int VoltOn = 47;
int VoltOff = 56;
int upbutton = 1;
int downbutton = 2;
int okbutton = 3;
int backbutton = 4;
void setup() {
  pinMode(upbutton, INPUT);
  pinMode(downbutton, INPUT);
  pinMode(okbutton, INPUT);
  pinMode(backbutton, INPUT);
  pinMode(RELAY_PORT, OUTPUT);
  // initialize the LCD
  lcd.begin();

  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.setCursor(0, 1);
  lcd.print ("generator: off");
}

void loop() {
  int analog_value = analogRead(A0);
  Voltage = ((analog_value * 5.0) / 1020) * 12;
  lcd.setCursor(0, 0);
  lcd.print("Volt: ");
  lcd.print(Voltage);
  lcd.print("V");

  if (Voltage <= VoltOn && digitalRead(RELAY_PORT) == LOW)
  {
    digitalWrite(RELAY_PORT, HIGH);
    lcd.setCursor(0, 1);
    lcd.print ("generator: on");
  }

  if (Voltage >= VoltOff && digitalRead(RELAY_PORT) == HIGH)
  {
    digitalWrite(RELAY_PORT, LOW);
    lcd.setCursor(0, 1);
    lcd.print ("generator: off");
  }





  if ((digitalRead(upbutton) == HIGH  && digitalRead(downbutton) == HIGH && digitalRead(okbutton) == HIGH && digitalRead(backbutton) == HIGH) || (Settings = true))
  {
    lcd.clear;
    lcd.print("Settings:");
    delay(2000);
    time1 = millis();
    bool Setting = false;
    while (digitalRead(upbutton) == LOW  && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
    {
      if (Setting == false)
      {
        lcd.clear;
        lcd.scrollDisplayLeft();
        lcd.print("press Up to set turn on");
        lcd.setCursor(0, 1);
        lcd.print("press down to set turn off");
        Setting = true;
      }
      if (millis() > time1 + 60000)
      {
        loop();
      }
    }
    time1 = millis();
    while (millis() > time1 + 60000)
    {
      if (upbutton == HIGH)
      {
        time1 = millis();
        //lcd.clear;
        lcd.scrollDisplayLeft();
        lcd.print("press Up/ Down to up/ Down Voltage turn on");
        while (digitalRead(upbutton) == LOW  && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
        {
          if (millis() > time1 + 60000)
          {
            loop();
          }
        }
        time1 = millis();
        while (millis() > time1 - 60000)
        {
          if (upbutton == HIGH)
          {
            int xdelay = 1000;
            time1 = millis();
            (VoltOn) = (VoltOn) + 1;
            lcd.setCursor(0, 1);
            lcd.print("Voltage-on: ");
            lcd.print(VoltOn);
            lcd.print("V");
            delay(xdelay);
            xdelay = xdelay / 1.5;
          }
          if (downbutton == HIGH)
          {
            int xdelay = 1000;
            time1 = millis();
            (VoltOn) = (VoltOn) - 1;
            lcd.setCursor(0, 1);
            lcd.print("Voltage-on: ");
            lcd.print(VoltOn);
            lcd.print("V");
            delay(xdelay);
            xdelay = xdelay / 1.5;
          }
          if (okbutton == HIGH)
          {
            loop();
          }
          if (backbutton == HIGH)
          { Settings = true;
          }
          if (millis() > time1 + 60000)
          {
            loop();
          }
        }

      }

      if (downbutton == HIGH)
      {
        time1 = millis();
        lcd.clear;
        lcd.scrollDisplayLeft();
        lcd.print("press Up/ Down to up/ Down Voltage turn off");

        while (digitalRead(upbutton) == LOW  && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
        {
          if (millis() > time1 + 60000)
          {
            loop();
          }

        }
        time1 = millis();
        while (millis() > time1 - 60000)
        {
          if (upbutton == HIGH)
          {
            int xdelay = 1000;
            time1 = millis();
            (VoltOff) = (VoltOff) + 1;
            lcd.setCursor(0, 1);
            lcd.print("Voltage-off: ");
            lcd.print(VoltOff);
            lcd.print("V");
            delay(xdelay);
            xdelay = xdelay / 1.5;
          }
          if (downbutton == HIGH)
          {
            int xdelay = 1000;
            time1 = millis();
            (VoltOff) = (VoltOff) - 1;
            lcd.setCursor(0, 1);
            lcd.print("Voltage-off: ");
            lcd.print(VoltOff);
            lcd.print("V");
            delay(xdelay);
            xdelay = xdelay / 1.5;
          }
          if (okbutton == HIGH)
          {
            loop();;
          }
          if (backbutton == HIGH)
          {
            Settings = true;
          }
          if (millis() > time1 + 60000)
          {
            loop();
          }
        }

      }

      if (backbutton == HIGH)
      {
        loop();
      }

      if (okbutton == HIGH)
      {
        loop();
      }

    }

  }
}

标签: arduino

解决方案


我认为您不应该在 stackoverflow 上要求某人只是为了完成您的工作。人们不喜欢这里不具体的问题,这表明自己的研究几乎没有努力。

也就是说,突出的一件事是,您的所有 lcd.clear 都缺少括号,它们应该lcd.clear(); 尝试将您的代码分成较小的部分并测试各个组件,如果某些东西不起作用。


推荐阅读