需要使用“millis()”同时在高/低之间翻转两个引脚

问题描述 投票:0回答:1

我正在构建一个 Arduino 电路,该电路必须通过 L298N h 桥翻转直流电机的方向,并通过最多两个模拟 PWM 输出驱动三个伺服系统。从今以后,我需要使用 millis() 来控制电机的方向,因为delay() 会阻止其他函数。在 L298N 上,我必须将一个引脚设置为高电平,将另一个引脚设置为低电平,以使电机朝一个方向旋转,然后翻转这些引脚,使其朝相反方向旋转。我正在尝试使用 millis() 编写代码来实现这一目标,但我遇到了一些问题,希望得到任何帮助。当前代码粘贴在下面。

作为参考,我在 Arduino IDE 中使用 ESP8266 板。当我使用延迟()来交替两个引脚的草图时,D7 和 D8 是已知的好引脚。

主板:NodeMCU 1.0 (ESP-12E)

库:esp8266 v2.7.3

const byte motorPin1 = D7;
const byte motorPin2 = D8;

const unsigned long motorPin1interval = 1000;
const unsigned long motorPin2interval = 1000;

unsigned long motorPin1timer;
unsigned long motorPin2timer;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);

  motorPin1timer = millis();
  motorPin2timer = millis();
}

void toggleMotorPin1() {
  if (digitalRead(motorPin1) == LOW)
    digitalWrite(motorPin1, HIGH);
  else
    digitalWrite(motorPin1, LOW);

  motorPin1timer = millis();
}

void toggleMotorPin2() {
  if (digitalRead(motorPin2) == HIGH)
    digitalWrite(motorPin2, LOW);
  else
    digitalWrite(motorPin2, HIGH);

  motorPin2timer = millis();

  void loop() {
    if ( (millis() - motorPin1timer) >= motorPin1interval)
      toggleMotorPin1();

    if ( (millis() - motorPin2) >= motorPin2interval)
      toggleMotorPin2();
  }

首先,当我尝试编译此代码时,我在循环中遇到错误:

D:\Documents\Arduino\l298n-millis-attempt3\l298n-millis-attempt3.ino: 在函数“voidtoggleMotorPin2()”中: D:\Documents\Arduino\l298n-millis-attempt3\l298n-millis-attempt3.ino:35:15:错误:在“{”标记之前不允许使用函数定义 无效循环(){ ^ D:\Documents\Arduino\l298n-millis-attempt3\l298n-millis-attempt3.ino:41:3: 错误:输入末尾应有“}” } ^

退出状态1

编译错误:在“{”标记之前不允许使用函数定义

我不完全确定如何解决这个问题,所以这里的任何帮助将不胜感激。另外,我的代码看起来不错吗?显然我还没有运行它,所以我不知道如果我们能够修复无效循环错误它会运行得如何。

我看过一些关于 millis() 的教程。诚然,我没有从任何基本代码(例如 LED 闪烁)开始,但我认为我理解 millis() 背后的理论。我尝试按照 YouTube 上的本教程进行操作,但是使用类似的代码,我在 void 循环中遇到了该错误。根据我到目前为止所查找的内容,不确定如何修复它。 尝试尽可能具有描述性。感谢您的帮助!

arduino milliseconds
1个回答
0
投票
}

定义开始之前缺少

loop()
motorPin2timer = millis();
 }   //This is the missing Curly Bracket
  
  void loop() {
    if ( (millis() - motorPin1timer) >= motorPin1interval)
      toggleMotorPin1();

    if ( (millis() - motorPin2timer) >= motorPin2interval) 
      toggleMotorPin2(); // corrected motorPin2 to motorPin2timer
  }

此外,您的拼写错误是 
motorPin2

而不是

motorPin2timer
。这是为您修改的完整代码
const byte motorPin1 = D7;
const byte motorPin2 = D8;

const unsigned long motorPin1interval = 1000;
const unsigned long motorPin2interval = 1000;

unsigned long motorPin1timer;
unsigned long motorPin2timer;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);

  motorPin1timer = millis();
  motorPin2timer = millis();
}

void toggleMotorPin1() {
  if (digitalRead(motorPin1) == LOW)
    digitalWrite(motorPin1, HIGH);
  else
    digitalWrite(motorPin1, LOW);

  motorPin1timer = millis();
}

void toggleMotorPin2() {
  if (digitalRead(motorPin2) == HIGH)
    digitalWrite(motorPin2, LOW);
  else
    digitalWrite(motorPin2, HIGH);

  motorPin2timer = millis();
}  // Added }

void loop() {
  if ((millis() - motorPin1timer) >= motorPin1interval)
    toggleMotorPin1();

  if ((millis() - motorPin2timer) >= motorPin2interval)
    toggleMotorPin2(); // Modified the motorPin2 to motorPin2timer
}

希望它对你有用

© www.soinside.com 2019 - 2024. All rights reserved.