I'm building an Arduino circuit that must flip the direction of a DC motor through an L298N h-bridge, as well as drive three servos with up to two analog PWM outputs. Henceforth, I need to use millis() to control the direction of the motor since delay() will block the other functions. On the L298N I have to set one pin to HIGH and another pin to LOW to get the motor to spin one direction, and flip those to get it to spin the opposite direction. I'm trying to write code using millis() to accomplish that, but I'm running into some issues and would appreciate any help. Current code pasted below.
For reference I'm using an ESP8266 board in Arduino IDE. D7 and D8 are known good pins when I had a sketch using delay() to alternate the two pins.
Board: NodeMCU 1.0 (ESP-12E)
Library: 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();
}
First off when I try to compile this code I'm getting errors in my loop:
D:\Documents\Arduino\l298n-millis-attempt3\l298n-millis-attempt3.ino: In function 'void toggleMotorPin2()': D:\Documents\Arduino\l298n-millis-attempt3\l298n-millis-attempt3.ino:35:15: error: a function-definition is not allowed here before '{' token void loop() { ^ D:\Documents\Arduino\l298n-millis-attempt3\l298n-millis-attempt3.ino:41:3: error: expected '}' at end of input } ^
exit status 1
Compilation error: a function-definition is not allowed here before '{' token
I'm not entirely sure how to fix this, so any help here would be appreciated. Also, does my code look good? Obviously I haven't run it yet, so I don't know how well it'll run if we can get the void loop errors fixed.
I had watched several tutorials on millis(). I admittedly didn't start with any basic codes like LED blinking, but I think I understand the theory behind millis(). I tried to follow this tutorial on YouTube, but using similar code I'm getting that error in the void loop. Not sure how to fix it based on what I've looked up so far.
Trying to be as descriptive as possible. Thanks for the help!
The compile error is due to missing }
before loop()
definition begins.
motorPin2timer = millis();
} //This is the missing Curly Bracket
void loop() {
if ( (millis() - motorPin1timer) >= motorPin1interval)
toggleMotorPin1();
if ( (millis() - motorPin2timer) >= motorPin2interval)
toggleMotorPin2(); // corrected motorPin2 to motorPin2timer
}
Also, you have typo error of motorPin2
instead of motorPin2timer
. here is the full modified code for you
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
}
Hope it works for you.
Amendment after Commnent of the Solution
The issue is in the way you are testing the pin state.
Here is the modified code snippet for toggleMotorPin2()
void toggleMotorPin2() {
if (digitalRead(motorPin2) == HIGH)
digitalWrite(motorPin2, LOW);
else
digitalWrite(motorPin2, HIGH);
motorPin2timer = millis();
}
I hope this works for you. Also, You may like to debug the code by use of Serial.println()
to monitor the state of pins and other variables to help in identifying the bugs.