Nextion hmi display screen with arduino R3

Hello,
I am currently working on my medicine dispenser project with Nextion Hmi Display Screen connected to arduino uno r3 with 3 servo motor. My goal is that to make nextion display screen as my source of user manual where if they pressed a button it will dispense specific pills same goes with the other 2. My question is it feasible to have Nextion hmi display screen to be the controller when you have connected it to arduino uno R3? For instance, If I had connected the Nextion Display Screen into an Arduino Uno R3 and to servo motor. How would i configurate the code when I tried to implement it. I need some example. Please!

Explanation:

  • construct a Nextion HMI display that can serve in the capacity of a controller for an Arduino Uno R3. Design a fundamental layout for the Nextion display. This should consist of a heading, three text boxes labeled "morning," "afternoon," and "night," and three buttons labeled "pills 1, 2, and 3." When a button is pushed, there should be a distinct kind of pill released. This may be coded into each button. As an example, the first button may dispense Advil, the second button would dispense Benedril, and the third button might dispense Tylenol.The first thing you need to do is establish a fundamental arrangement on the Nextion display. This design need to include a heading, three text boxes labeled "morning," "afternoon," and "night," and three buttons labeled "pills 1, 2, and 3." When a button is pushed, there should be a distinct kind of pill released. This may be coded into each button. As an example, the first button may dispense Advil, the second button would dispense Benedril, and the third button might dispense Tylenol.The next thing that has to be done is to program each button so that it will dispense the appropriate kind of pill when it is pushed. Within the Nextion program, this may be accomplished by providing a one-of-a-kind code for each button. After the user presses the button, the Arduino will read the code and dispense the appropriate pill. In the end, you will need to connect the Arduino to the Nextion display and then configure the buttons so that they will transmit the appropriate code to the Arduino. This may be accomplished via the use of the Nextion software or by direct programming of the Arduino. After you have finished all of these processes, the Nextion HMI display will be able to function as a controller for an Arduino Uno R3.
    the schematic
#include<LiquidCrystal.h>
#include <Servo.h>

LiquidCrystal lcd(9, 8, 5, 4, 3, 2);

Servo servo;

Servo servo_6;

Servo servo_5;

Servo servo_3;

int angle = 0;
//int angle = 90;
//int angle 180;
//int angle 360;

const int buttonPin1 = 6; // the number of the pushbutton pin
const int buttonPin2 = 5;
const int buttonPin3 = 3;

//const int buttonPin1.pressed = 6; // the number of the pushbutton pin
//const int buttonPin2.pressed = 5;
//const int buttonPin3.pressed = 3;
// variable will change

int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;

//int buttonState1.pressed = 0; // variable for reading the pushbutton status
//int buttonState2.pressed = 0;
//int buttonState3.pressed = 0;

const int buzzer = 8; //buzzer to arduino pin 10
const int POTENTIOMETER_PIN = A0; //Arduino pin connected to Potentiometer pin
//const int ANALOG_THRESHOLD = 5000;

void setup()
{

//Initialize the LED pin as an output:
pinMode(LED_BUILTIN, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);

lcd.begin(16,2);

pinMode(buzzer, OUTPUT); // Set buzzer - pin 10 as an output

pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
servo_6.attach(6);
servo_5.attach(5);
servo_3.attach(3);

}

void loop()
{

// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(LED_BUILTIN, HIGH);
}
else if (buttonState2 == HIGH) {
digitalWrite(LED_BUILTIN, HIGH);
}
else if (buttonState3 == HIGH) {
digitalWrite(LED_BUILTIN, HIGH);
}
else {
// turn LED off:
digitalWrite(LED_BUILTIN, LOW);

servo_6.write(360);
servo_5.write(180);
servo_3.write(90);
lcd.setCursor(0,0);
lcd.print("Medicine");
lcd.setCursor(2,1);
lcd.print("Dispenser");
delay(5000);
lcd.clear();
delay(1000); // Wait for 1000 millisecond(s)
lcd.print("NextCycle = 8AM");
delay(5000); // Wait for 5000 millisecond(s)
lcd.clear();

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(LED_BUILTIN, HIGH);
}
else if (buttonState2 == HIGH) {
digitalWrite(LED_BUILTIN, HIGH);
}
else if (buttonState3 == HIGH) {
digitalWrite(LED_BUILTIN, HIGH);
}
else {
// turn LED off:
digitalWrite(LED_BUILTIN, LOW);
}

// FIRST CYCLE
digitalWrite(13, HIGH); //Green Light On
lcd.setCursor(0,0);
lcd.print("8:00 AM");
lcd.setCursor(2,1);
lcd.print("MORNING MED");

servo_6.write(360);
servo_5.write(0);
servo_3.write(0);
tone(buzzer, 500); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec
tone(buzzer, 500); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...

digitalWrite(13, LOW); //Green Light Off
lcd.clear();
servo_6.write(0);
servo_5.write(0);
servo_3.write(0);
lcd.print("NextCycle = 12PM");
delay(5000); // Wait for 5000 millisecond(s)
lcd.clear();

// SECOND CYCLE
digitalWrite(12, HIGH); //Blue Light On

lcd.setCursor(0,0);
lcd.print("10:00 PM");
lcd.setCursor(2,1);
lcd.print("AFTERNOON MED");

servo_6.write(0); //TEST
servo_5.write(180);
servo_3.write(0);
tone(buzzer, 500); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec
tone(buzzer, 500); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...

digitalWrite(12, LOW); //Blue Light Off
lcd.clear();
servo_6.write(0);
servo_5.write(0);
servo_3.write(0);

lcd.print("NextCycle = 10PM");
delay(5000); // Wait for 5000 millisecond(s)
lcd.clear();

// THIRD CYCLE

digitalWrite(11, HIGH); //Red Light On

lcd.setCursor(0,0);
lcd.print("10:00 PM");
lcd.setCursor(2,1);
lcd.print("NIGHT MED");

servo_6.write(0); //TEST
servo_5.write(0);
servo_3.write(90);
tone(buzzer, 500); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(100); // ...for 1sec
tone(buzzer, 500); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...

digitalWrite(11, LOW); //Red Light Off
lcd.clear();
servo_6.write(360);
servo_5.write(180);;
servo_3.write(90);

delay(500); // Wait for 5000 millisecond(s)

}
}

code i did for Nextion:
#include <Wire.h>
#include <Servo.h>
#include <Nextion.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
Servo servo1, servo2, servo3;
const int blueLED = 10;
const int buzzer = 8;
int dispenser = 0;
int UM = 0; // This two variables are used to control the amount of times
int prevUM = -1; //that is being refreshed the display
int n; //to store n pills
int i = 0; //count pills
int counter2;
int i2;
int pos;
bool State1 = 0;
bool State2 = 0;
bool State3 = 0;
bool Glass = 1;
bool preGlass = 1;
bool StateTemp = 0;
bool in = 0;
///////////////////////////NEXTION TOUCHSCREEN/////////////////////////////////////////////////
NexButton PL0 = NexButton(2,2,"b0"); //Pick a Button to pressed 1
NexButton PL1 = NexButton(2,3,"b1"); //Pick a Button to pressed 2
NexButton PL2 = NexButton(2,4,"b2"); //Pick a Button to pressed 3
NexButton button1 = NexButton(3,2,"b0"); //Button to confirm pill dispense
NexButton button2 = NexButton(3,3,"b1"); //Button to confirm pill dispense
NexButton RD1 = NexButton(6,1,"b0"); //Button to confirm pill dispense
NexButton RD3 = NexButton(6,2,"b1"); //Button to confirm pill dispense
NexButton RD2 = NexButton(6,3,"b2"); //Button to confirm pill dispense
NexButton Home = NexButton(8,2,"b0"); //Button to confirm pill dispense
NexButton setting = NexButton(0,4,"b0"); //Button to show the pill dipenser
NexButton On = NexButton(0, 7, "b3");
NexButton Off = NexButton(0, 6, "b2");
NexText State = NexText(0, 13, "t13");
NexText degreeF = NexText(1, 3, "degreeF");
NexText degreeC = NexText(1, 2, "degreeC");
NexText Humidity = NexText(1, 9, "Humidity");
NexProgressBar j0 = NexProgressBar (1, 8, "j0");
NexButton Update = NexButton(1,6,"b0"); //button to update temperature and humidty
NexText one= NexText(3, 4, "t1");
NexText two= NexText(3, 5, "t2");
NexPage page0 = NexPage(0,0 , "page0");
NexPage page8 = NexPage(8,0 , "page8");

NexProgressBar progressPill = NexProgressBar (4, 1, "j0");
NexProgressBar j1 = NexProgressBar (7, 2, "j0");

NexTouch *nex_listen_list[] = {&On,&Off,&Update,&button1,&button2,&RD1, &RD2, &RD3, &Home, NULL};
///////////////////////////////////////////////////////////////////////////////////////
void setup()
{
// put your setup code here, to run once:
dht.begin();
Serial.begin(9600);
delay(10);
nexInit(); //Iniziate nextion display
// Register the pop event callback function of the components
On.attachPop(OnPopCallBack,&On);
Off.attachPop(OffPopCallBack,&Off);
Update.attachPop(UpdatePopCallBack,&Update);
button1.attachPop(button1PopCallBack,&button1);
button2.attachPop(button2PopCallBack,&button2);
RD1.attachPop(RD1PopCallBack,&RD1);
RD2.attachPop(RD2PopCallBack,&RD2);
RD3.attachPop(RD3PopCallBack,&RD3);
Home.attachPop(HomePopCallBack,&Home);
dbSerialPrintln("setup done");
servo1.attach(6); // attaches the servo on pin 7 to the servo object
servo2.attach(5); // attaches the servo on pin 6 to the servo object
servo3.attach(3); // attaches the servo on pin 5 to the servo object
// Set LEDs as outputs
pinMode(blueLED, OUTPUT);
pinMode(buzzer, OUTPUT);
delay(1000);
/////////////////////////
}
void loop() {
// put your main code here, to run repeatedly:
// what if we loop the servo motor here
if(Serial.available()> 0)
{
if(StateTemp == LOW)
{
in = 1;
StateTemp == HIGH;
}else
parser();
}
nexLoop(nex_listen_list);
}

Fritzings are useless. Anyway, the powering of especially the servos must be changed. Give them a power supply of their own. Arduinos should in general never been used as power supplies, especially not for motors.
Why does so many questioners mix hardware and software matters into a soup?
Make sketches testing each hardware device one by one. Then bring the codes together and start developing the software using the combination of devices, one function at the time.

@anon24968962
Please insert your two codes SEPARATELY in two sets of code tags.

About the second code - did you try to compile it? What the IDE writes about of memory size, needs to run this code?

I compile all the code, and it works properly. Everything is working but my issue is that when i tried to pressed buttons on the Nextion, it seem like there is nothing going on the motors.

Please insert the code that using the Nextion in a new message

void OnPopCallBack(void *ptr) {
  State.setText("State: on");
  digitalWrite(blueLED, HIGH);
}

void OffPopCallBack(void *ptr) {
  State.setText("State: off");
  digitalWrite(blueLED, LOW);
}
void UpdatePopCallBack(void *ptr) 
{
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f))  
  {
    return;
  }
  static char temperatureCTemp[6];
  dtostrf(t, 6, 2, temperatureCTemp);
  degreeC.setText(temperatureCTemp);
  
  // Update humidity percentage text and progress bar
  char hTemp[10] = {0}; 
  utoa(int(h), hTemp, 10);
  humidity.setText(hTemp);
  j0.setValue(int(h));
  
  // Update temperature in Fahrenheit
  static char temperatureFTemp[6];
  dtostrf(f, 6, 2, temperatureFTemp);
  degreeF.setText(temperatureFTemp);
}
void button1PopCallBack(void *ptr)
{
  int progress = map(counter2,0,counter,0,100);
  progressPill.setValue(progress);
  i=0;
  for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    tone(buzzer,1000);
    delay(5000);
    noTone(buzzer);
    delay(5000);
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    tone(buzzer,1000);
    delay(5000);
    noTone(buzzer);
    delay(5000);
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
  endPill();
}
void button2PopCallBack(void *ptr)
{
  int progress = map(counter2,0,counter,0,100);
  progressPill.setValue(progress);
  for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    tone(buzzer,1000);
    delay(5000);
    noTone(buzzer);
    delay(5000);
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    tone(buzzer,1000);
    delay(5000);
    noTone(buzzer);
    delay(5000);
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
  endPill();
}
void RD1PopCallBack(void *ptr){                          
  //Function with routines  to execute  when the button is pressed
  for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
  page8.show();    
}      

void RD2PopCallBack(void *ptr){                          
  //Function with routines  to execute  when the button is pressed
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
    int j = map(100,0,pos,100,0);
    j1.setValue(j);
    page8.show();
}
void RD3PopCallBack(void *ptr)
{                          
  //Function with routines  to execute  when the button is pressed
  for (pos = 0; pos <= 270; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    tone(buzzer,1000);
    delay(5000);
    noTone(buzzer);
    delay(5000);
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 270; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5000);                       // waits 15 ms for the servo to reach the position
  }
    int j = map(180,0,pos,100,0);
    j1.setValue(j);
    page8.show();
}
void endPill()
{
  State2 = LOW;  
  State3 = LOW;
  State1 = LOW;
  Serial.println(F("leave"));
  counter = 0;
  counter2 = 0;
  page0.show();
  delay(2500);
  i2 = 0; 
  prevUM = -1;
}  

This code seems can't be compiled because the setup() and loop() procedures are missing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.