/**
* NodeMCU_ESP8266_L9110_H-Bridge_Demo_Heater_Elec
* https://www.engineersgarage.com/nodemcu-and-l293d-motor-driver-controlling-dc-motor/
* https://lastminuteengineers.com/esp8266-pinout-reference/
* https://esp8266-shop.com/esp8266-guide/esp8266-nodemcu-pinout/
* //Button Debouncing - INPUT_PULLUP
* https://esp32io.com/tutorials/esp32-button-debounce
*/
//#include <ezButton.h>
//Motors
#define MotorForward 21//14//lol1n esp 12e//14//NodeMCU ESP8266//21 // choose the pin for the Motor Forward
#define MotorReverse 20//12//lol1n esp 12e//12//NodeMCU ESP8266//20 // choose the pin for the Motor Reverse
#define PumpPlus 33//5//lol1n esp 12e//4//NodeMCU ESP8266//33 // choose the pin for the Pump Forward
#define PumpMinus 26//4//lol1n esp 12e//15//NodeMCU ESP8266//26 // choose the pin for the Pump Reverse
//RGB LED
// #define RLed 10//17
// #define GLed 9//16
// #define BLed 8//15
//Buttons
//ezButton button1(0); //D3// create ezButton object that attach to pin GIOP21;
#define buttonPlus 39//13//D7//0//D3//16//39 // choose the input pin (for a pushbutton)
// #define buttonMinus 2//D4//5//38 // choose the input pin (for a pushbutton)
// #define buttonSelector 16//D0//0//40 // choose the input pin (for a pushbutton)
#define buttonAllOnOff 40//2//D4//2//41 // choose the input pin (for a pushbutton)
#define DEBOUNCE_TIME 20 // the debounce time in millisecond, increase this time if it still chatters
// Button Debouncing: Variables will change:
int lastSteadyState = LOW; // the previous steady state from the input pin
int lastFlickerableState = LOW; // the previous flickerable state from the input pin
// int currentState; // the current reading from the input pin
// Button Debouncing: the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
int buttonStateMotorPlus = 0; // variable for reading the pin status
int buttonStateMotorMinus = 0; // variable for reading the pin status
int buttonStatePumpPlus = 0; // variable for reading the pin status
int buttonStatePumpMinus = 0; // variable for reading the pin status
int buttonStateSelector = 0;
int buttonStateAllOnOff = 0; // variable for reading the pin status
int prestate = 0;
int selector_count_value = 0;
int motor_count_value = 0;
int pump_count_value = 0;
// Motor Speed Values - Start at zero
int MotorSpeed1 = 60;
int MotorSpeed2 = 60;
// void setup(void);
void setup() {
//delay(1000);
pinMode(MotorForward, OUTPUT); // declare Motor as output
pinMode(MotorReverse, OUTPUT); // declare Motor as output
pinMode(PumpPlus, OUTPUT); // declare Pump as output
pinMode(PumpMinus, OUTPUT); // declare Pump as output
// pinMode(RLed, OUTPUT); // declare RLed as output
// pinMode(GLed, OUTPUT); // declare GLed as output
// pinMode(BLed, OUTPUT); // declare BLed as output
//button1.setDebounceTime(DEBOUNCE_TIME); // set debounce time to 50 milliseconds
pinMode(buttonPlus, INPUT_PULLUP);//INPUT_PULLUP // declare pushbutton as input
// pinMode(buttonMinus, INPUT_PULLDOWN_16); // declare pushbutton as input
// pinMode(buttonSelector, INPUT_PULLDOWN_16); // declare pushbutton as input
pinMode(buttonAllOnOff, INPUT_PULLUP); // declare pushbutton as input
// digitalWrite(RLed, HIGH);
// digitalWrite(GLed, HIGH);
// digitalWrite(BLed, HIGH);
Serial.begin(9600);
Serial.println("Motor control using L9110 Driver");
}
void MotorControl(int OnOff)
{
//do
//{
//Turn the Motors ON
if(OnOff == 1)
{
// Set the motor speeds using L9110 H-Bridge driver
// MotorSpeed1 = 900;
analogWrite(MotorForward, MotorSpeed1);
analogWrite(MotorReverse, 0);
analogWrite(PumpPlus, MotorSpeed1);
analogWrite(PumpMinus, 0);
Serial.println("Motor Forward");
delay(125); //125 = 4 times per 1 second = 1000/8
analogWrite(MotorForward, 0);
analogWrite(MotorReverse, MotorSpeed1);
analogWrite(PumpPlus, 0);
analogWrite(PumpMinus, MotorSpeed1);
Serial.println("Motor Reverse");
delay(125);
}
//}while(OnOff ==1)
else // Turn the Motors OFF
{
// Set the motor speeds using L9110 H-Bridge driver
analogWrite(MotorForward, 0);
analogWrite(MotorReverse, 0);
analogWrite(PumpPlus, 0);
analogWrite(PumpMinus, 0);
//Serial.println("Motors are OFF");
delay(100);
}
}
void loop(){
/*
//delay(1000);
//read the state of the pushbutton value:
// buttonStateSelector = digitalRead(buttonSelector);
buttonStateMotorPlus = digitalRead(buttonPlus);
buttonStateAllOnOff = digitalRead(buttonAllOnOff);
//button1.loop(); // MUST call the loop() function first
//#region Button All ON/OFF Debouncing
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch/button changed, due to noise or pressing:
if (buttonStateAllOnOff != lastFlickerableState) {
// reset the debouncing timer
lastDebounceTime = millis();
// save the the last flickerable state
lastFlickerableState = buttonStateAllOnOff;
}
if ((millis() - lastDebounceTime) > DEBOUNCE_TIME) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if(lastSteadyState == HIGH && buttonStateAllOnOff == LOW)
{
//Turn Motors ON
if(prestate == 0)
{
prestate = 1;
}
else // Turn Motors OFF
{
// Set the motor speeds using L9110 H-Bridge driver
// MotorControl(0);
// analogWrite(MotorForward, 0);
// analogWrite(MotorReverse, 0);
// analogWrite(PumpPlus, 0);
// analogWrite(PumpMinus, 0);
// Serial.println("Motors are OFF");
// delay(100);
prestate = 0;
}
Serial.println("The button is pressed");
Serial.print("Prestate value: ");
Serial.println(prestate);
}
else if(lastSteadyState == LOW && buttonStateAllOnOff == HIGH)
//Serial.println("The button is released");
// save the the last steady state
lastSteadyState = buttonStateAllOnOff;
}
//#endregion Button All ON/OFF Debouncing
//#region Button Motor Plus Debouncing
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch/button changed, due to noise or pressing:
if (buttonStateMotorPlus != lastFlickerableState) {
// reset the debouncing timer
lastDebounceTime = millis();
// save the the last flickerable state
lastFlickerableState = buttonStateMotorPlus;
}
if ((millis() - lastDebounceTime) > DEBOUNCE_TIME) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if(lastSteadyState == HIGH && buttonStateMotorPlus == LOW)
{
motor_count_value = motor_count_value + 50;
if(motor_count_value > 245)
{
motor_count_value = 245;
}
// Read the values from the potentiometers
//MotorSpeed1 = motor_count_value;//analogRead(SpeedControl1);
//MotorSpeed2 = motor_count_value--;//analogRead(SpeedControl2);
MotorSpeed1 = motor_count_value;
// Convert to range of 0-255
MotorSpeed1 = map(motor_count_value, 0, 255, 0, 4095);
//MotorSpeed1 = map(motor_count_value, 0, 255, 0, 1023);
//MotorSpeed1 = map(motor_count_value, 60, 4095, 60, 255);
//MotorSpeed2 = map(MotorSpeed2, 0, 1023, 0, 255);
//Serial.print("Motor Count Value: ");
Serial.print("Motor Count Value: ");
Serial.println(motor_count_value);
Serial.print("Motor Speed: ");
Serial.println(MotorSpeed1);
//Serial.println("The button is pressed");
}
else if(lastSteadyState == LOW && buttonStateMotorPlus == HIGH)
//Serial.println("The button is released");
// save the the last steady state
lastSteadyState = buttonStateMotorPlus;
}
*/
//#endregion Button Motor Plus Debouncing
//Turn ON/OFF ALL Motors
/*
delay(DEBOUNCE_TIME); //For push button debouncing
if(buttonStateAllOnOff == LOW)
{
//Turn Motors ON
if(prestate == 0)
{
prestate = 1;
}
else // Turn Motors OFF
{
// Set the motor speeds using L9110 H-Bridge driver
// MotorControl(0);
// analogWrite(MotorForward, 0);
// analogWrite(MotorReverse, 0);
// analogWrite(PumpPlus, 0);
// analogWrite(PumpMinus, 0);
// Serial.println("Motors are OFF");
// delay(100);
prestate = 0;
}
}
//Increase the Speed
delay(DEBOUNCE_TIME); //For push button debouncing
if(buttonStateMotorPlus == LOW)
{
motor_count_value = motor_count_value + 50;
if(motor_count_value > 245)
{
motor_count_value = 245;
}
// Read the values from the potentiometers
//MotorSpeed1 = motor_count_value;//analogRead(SpeedControl1);
//MotorSpeed2 = motor_count_value--;//analogRead(SpeedControl2);
MotorSpeed1 = motor_count_value;
// Convert to range of 0-255
MotorSpeed1 = map(motor_count_value, 0, 255, 0, 4095);
//MotorSpeed1 = map(motor_count_value, 0, 255, 0, 1023);
//MotorSpeed1 = map(motor_count_value, 60, 4095, 60, 255);
//MotorSpeed2 = map(MotorSpeed2, 0, 1023, 0, 255);
Serial.print("Motor Count Value: ");
Serial.println(motor_count_value);
Serial.print("Motor Speed: ");
Serial.println(MotorSpeed1);
}
*/
// Call the Mootor control function to turn motor ON/OFF and Increase the speed
MotorControl(prestate);
// else // Turn Motors OFF
// {
// // Set the motor speeds using L9110 H-Bridge driver
// analogWrite(MotorForward, 0);
// analogWrite(MotorReverse, 0);
// analogWrite(PumpPlus, 0);
// analogWrite(PumpMinus, 0);
// Serial.println("Button not pressed \"aka: connected to ground\"");
// delay(100);
// prestate = 0;
// }
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1