n20
I new to Nextion. I'm using Arduino Uno and I need help coding to get a DC motor controlled by Nextion LCD to turn ON, OFF, and - + speed. I'M TRYING TO CONTROL the position of the engine.
I have this code for a dc motor but I need to program it for a dc motor with an encoder. help friends.
I am using n20 dc motor with encoder
[image]
#include <doxygen.h>
#include <NexButton.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexGauge.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexSlider.h>
#include <NexText.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexWaveform.h>
//DC motor
int motorPin = 3;
int fanSpeed = 100; // Counter
//Declare Fan page buttons
NexButton bON = NexButton(3, 3, "bON");
NexButton bOFF = NexButton(3, 4, "bOFF");
NexButton bMenuF = NexButton(3, 6, "bMenuF");
NexButton bFMinus = NexButton(3, 7, "bFMinus");
NexButton bFPlus = NexButton(3, 8, "bFPlus");
NexTouch *nex_listen_list[] =
{
//Declare Fan page buttons and slider
&bON,
&bOFF,
&bMenuF,
&bFMinus,
&bFPlus,
NULL
};
// Touch events
void bONPopCallback(void *ptr) // Release event for button
{
analogWrite(motorPin, 100); // Turn ON fan
}
void bOFFPopCallback(void *ptr) // Release event for button
{
analogWrite(motorPin, 0); // Turn OFF fan
}
void bFMinusPopCallback (void *ptr)
{
fanSpeed = fanSpeed - 5; //Minus 5 to current value of counter
}
void bFPlusPopCallback (void *ptr)
{
fanSpeed = fanSpeed + 5; //Plus 5 to current value of counter
}
void setup() {
// put your setup code here, to run once:
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
//Serial.println("Give a number from 50 to 255.");
//analogWrite(motorPin, 0); //Fan always off when system powered on
// Register the event callback fns of each touch event
// Format for press events: <object name>.attachPush(<object name>PushCallback);
// Format for release events: <object name>.attachPop(<object name>PopCallback);
bON.attachPop(bONPopCallback);
bOFF.attachPop(bOFFPopCallback);
bFMinus.attachPop(bFMinusPopCallback);
bFPlus.attachPop(bFPlusPopCallback);
}
void loop() {
nexLoop (nex_listen_list); //Check for any touch event
}





