Nextion LCD Arduino DC motor n20 with encoder help

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
 }
  • Start with removing all the "> " from your posted code.
  • Show the link from where you copied the code (the ">" came from quoted text)

Follow the tutorials in that link.

I am using n20 dc motor with encoder

I'll send what I already have for you to look at


I have this code ready for servo motor. I would like some help to use the n20 motor with encoder.

#include <Servo.h>
int pos1,pos2,pos3,pos4,pos5;
Servo s1,s2,s3,s4,s5;
String dfd_global;

void setup() {
Serial.begin(9600);
s1.attach(3);// 3,5,6,9,14,15 are PWM pins on pro mini
s2.attach(5);
s3.attach(6);
s4.attach(10);
s5.attach(11);
}

void loop() {

if(Serial.available()){ ///////receiving from nextion without library
char character;
String dfd; //dfd stands for data from display, feel free to change variable names! doesn't matter
delay(30);
while(Serial.available()){
character=char(Serial.read()); //reading data from the serial port
dfd += character;
}
//Serial.println(dfd);
dfd_global=dfd; //dfd was not a global variable so wont work outside this loop
}

if (dfd_global.startsWith("A")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("A= " + dfd_global);
int pos1=dfd_global.toInt();
// Serial.println(pos1);
s1.write(pos1); //run servo 1
delay(10);
}

else if (dfd_global.startsWith("B")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("B= " + dfd_global);
int pos2=dfd_global.toInt();
//Serial.println(pos2);
s2.write(pos2); //run servo 2
delay(10);
}

else if (dfd_global.startsWith("C")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("C= " + dfd_global);
int pos3=dfd_global.toInt();
//Serial.println(pos3);
s3.write(pos3); // run servo 3
delay(10);
}

else if (dfd_global.startsWith("D")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("D= " + dfd_global);
int pos4=dfd_global.toInt();
//Serial.println(pos4);
s4.write(pos4); // run servo 4
delay(10);
}

else if (dfd_global.startsWith("E")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("E= " + dfd_global);
int pos5=dfd_global.toInt();
//Serial.println(pos5);
s5.write(pos5); // run servo 5
delay(10);
}

else if (dfd_global.startsWith("Z")){ // this is just a reset button on the nextion display,
s1.write(0); //that changes the angle of all the servos to 0°
s2.write(0);
s3.write(0);
s4.write(0);
s5.write(0);
delay(10);
}

else{
}

}

I have these drivemotors

I need help to control the dcmotor n20 with encoder. Start the engine and use the slide on the nextion display to move the engine forwards and backwards.


I'm trying to make a robotic hand with dc motor with encoder

Be sure to format your code correctly using the button.

I have this code ready for servo motor. I would like some help to use the n20 motor with encoder.

#include <Servo.h>
int pos1,pos2,pos3,pos4,pos5;
Servo s1,s2,s3,s4,s5;
String dfd_global;

void setup() {
Serial.begin(9600);
s1.attach(3);// 3,5,6,9,14,15 are PWM pins on pro mini
s2.attach(5);
s3.attach(6);
s4.attach(10);
s5.attach(11);
}

void loop() {

if(Serial.available()){ ///////receiving from nextion without library
char character;
String dfd; //dfd stands for data from display, feel free to change variable names! doesn't matter
delay(30);
while(Serial.available()){
character=char(Serial.read()); //reading data from the serial port
dfd += character;
}
//Serial.println(dfd);
dfd_global=dfd; //dfd was not a global variable so wont work outside this loop
}

if (dfd_global.startsWith("A")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("A= " + dfd_global);
int pos1=dfd_global.toInt();
// Serial.println(pos1);
s1.write(pos1); //run servo 1
delay(10);
}

else if (dfd_global.startsWith("B")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("B= " + dfd_global);
int pos2=dfd_global.toInt();
//Serial.println(pos2);
s2.write(pos2); //run servo 2
delay(10);
}

else if (dfd_global.startsWith("C")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("C= " + dfd_global);
int pos3=dfd_global.toInt();
//Serial.println(pos3);
s3.write(pos3); // run servo 3
delay(10);
}

else if (dfd_global.startsWith("D")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("D= " + dfd_global);
int pos4=dfd_global.toInt();
//Serial.println(pos4);
s4.write(pos4); // run servo 4
delay(10);
}

else if (dfd_global.startsWith("E")){ // to check which slider is the data coming from
dfd_global.remove(0,1); // removing the prefix
//Serial.println("E= " + dfd_global);
int pos5=dfd_global.toInt();
//Serial.println(pos5);
s5.write(pos5); // run servo 5
delay(10);
}

else if (dfd_global.startsWith("Z")){ // this is just a reset button on the nextion display,
s1.write(0); //that changes the angle of all the servos to 0°
s2.write(0);
s3.write(0);
s4.write(0);
s5.write(0);
delay(10);
}

else{
}

}

Could you help to transform this code for servo motor to dc motor encoder. please

Dear @joey7motor

It is pretty simple to post code as a code-section.
You will get much more help if you take the 5 minutes effort to learn how to proper post code.

You seem to be very new to writing code.
Well it is not done in 1 hour. You should divide your project into smaller steps.

step 1: making your motor rotate clockwise for two seconds then pause 1 second then / rotate motor counter-clockwise for two seconds

If you have managed this:

step 2: reading in the encoder-pulses

step 3: writing the code for controlling a single DC-motor with encoder

well you did not tell how many motors this robotic hand will have.
I estimate 5 fingers = 5 motors

This means you need 5 * 4 = 20 IO-pins to control 5 DC-motors with encoder

It is very important information:

How many DC motors with encoder does this robotic hand have?

Will moving up / down the sliders on the nextion display be the final state of your project ?
or
do you want to go beyond that?
If yes what is the real final stage of your project?

This real final stage determines

  • what microcontroller to use
  • what encoder-library to use

If you want to have quick moving of 5 fingers all at the same time
Something like an Arduino Nano

  • has too less IO-pins
  • has too less RAM
  • too less calculation power to control all this
#define MotFwd  3  // Motor Forward pin
#define MotRev  4 // Motor Reverse pin

int encoderPin1 = 2; //Encoder Output 'A' must connected with intreput pin of arduino.
int encoderPin2 = 3; //Encoder Otput 'B' must connected with intreput pin of arduino.
volatile int lastEncoded = 0; // Here updated value of encoder store.
volatile long encoderValue = 0; // Raw encoder value


void setup() {

  pinMode(MotFwd, OUTPUT); 
  pinMode(MotRev, OUTPUT); 
  Serial.begin(9600); //initialize serial comunication

   pinMode(encoderPin1, INPUT_PULLUP); 
  pinMode(encoderPin2, INPUT_PULLUP);

  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

  //call updateEncoder() when any high/low changed seen
  //on interrupt 0 (pin 2), or interrupt 1 (pin 3) 
  attachInterrupt(0, updateEncoder, CHANGE); 
  attachInterrupt(1, updateEncoder, CHANGE);


}

void loop() {

for (int i = 0; i <= 500; i++){
digitalWrite(MotFwd, LOW); 
 digitalWrite(MotRev, HIGH);
 Serial.print("Forward  ");
 Serial.println(encoderValue);
}

delay(1000);

for (int i = 0; i <= 500; i++){
digitalWrite(MotFwd, HIGH); 
 digitalWrite(MotRev, LOW);
 Serial.print("Reverse  ");
 Serial.println(encoderValue);
}

delay(1000);

} 

void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue --;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue ++;

  lastEncoded = encoded; //store this value for next time

}

and the question is?