Motorized Camera Slider – The Final Chapter! – Arduino Code

//Motorized Camera Slider – The Final Chapter! – Arduino Code
TUTORIAL


Arduino Motorized Slider

-=The Final Chapter=-

Nextion & Arduino Code

HELP OUT

All my content is free.

If you feel that my videos are helping and you would like to contribute, you can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

Brainy-Bits Tip Jar Amount
OVERVIEW

Here it is!

The Final Chapter of the Arduino Motorized Camera Slider Tutorial.

We will have a look at:

  • Creating the graphics for the Nextion touchscreen
  • Adding the components needed in the Nextion Editor Software
  • and finally the Arduino code that makes it work!

If you haven’t seen the prior chapters you can check them out here:

Now that you’re up to speed, let’s finish this!

CONNECTION DIAGRAM

I tried to keep the connections to a minimum so it would be easier to solder on the PCB.

Arduino connections:

  • Pin 2 and 3 are connected to the Easy Driver STEP and DIR pins
  • Pin 10 and 11 are connected to the Nextion TX and RX pins
  • VIN and GND pins of the Arduino Nano are connected directly to the NPF battery

Easy Driver Connections:

  • The stepper motor is connected to the Motor connections on the EasyDriver
  • The PWR IN (GND and V+) are connected directly to the NPF battery

Nextion Connections:

  • TX and RX are connected to Pin 10 and 11 of the Arduino Nano
  • GND is connected to the GND of the NPF battery
  • +5V is connected to the 5V out pin of the Arduino Nano
NEXTION GRAPHICS

I’m using Photoshop to create the graphics, but any graphic software will do.

Since the Nextion screen I’m using in this project (Nextion 3.2″) has a screen resolution of 400×240 pixels, I just had to make sure to create a new document with that same resolution.

Once the graphic is created, all you have to do is save the file as a JPEG.

You can see more of this process in the tutorial video below.

NEXTION EDITOR

Once the graphic for the Nextion screen is created we need to import it in the Nextion Editor software.

It’s here that we will create the touch area as well as the text boxes which will hold the positions and the speed of the slider.

Again, please watch the tutorial video below to get a complete explanation of the process.

UPDATING THE NEXTION LCD

The file that was created using the Nextion Editor (.TFT) can now be put on a MicroSD card to update the Nextion LCD.

This process is pretty straightforward but you need to keep some things in mind.

First make sure that the MicroSD card is formatted using FAT32.

Also if you’re using a Mac to do this, it creates a hidden file that the Nextion LCD sees and refuses to update, so try to copy the .TFT file to the MicroSD using a Windows computer.

Once you have the .TFT file on a FAT32 formatted MicroSD card, insert it in the back of the Nextion display.

Now all you have to do is power up the Nextion (using the included USB adapter and a regular 5v power bank) and the Nextion will start updating.

Once completed, cut the power, remove the MicroSD card for the Nextion.

Power the Nextion screen back up, and you should see the graphic we created now on the screen.

Of course if you press the buttons now nothing will happen since we need the Arduino connected for that.

So let’s move on to the Arduino code!

THE CODE Part 1

Now that the graphic we created in the Nextion Editor is loaded on the Screen, we need to find out the what message is received from the Nextion LCD when a certain button is pressed.

To achieve this we will use this short Arduino Code that reads the message received and displays the information on the Serial Monitor.

As always to get more information on the code check out the tutorial video as well.

// Arduino code to read Messages from Nextion LCD

#include <SoftwareSerial.h>  // Software Serial Port (included in Arduino IDE "no download")
#include <Nextion.h>  // Nextion Library by Bentley Born https://github.com/bborncr/nextion

SoftwareSerial nextion(10, 11);// Nextion TX to pin 10 and RX to pin 11 of Arduino

Nextion NextionLCD(nextion, 9600); // Setup of Nextion library with name NextionLCD at 9600bps

void setup() {
  Serial.begin(9600);
  NextionLCD.init();
}

void loop() {
  String message = NextionLCD.listen(); // if a message is received from the Nextion LCD
  if(message != ""){ // if the message is not empty
    Serial.println(message); // Display the message on the Serial monitor
  } 
}
THE CODE Part 2

Not that we know which message corresponds to which button press or release, we can now write the Arduino Code needed.

In this tutorial I’m using a new Library to communicate with the Nextion LCD.

This library compared to the previous one is much easier to use and requires a lot less coding.

You can find a link to this library at the bottom of this page.

The code waits to receive a message from the Nextion and then does an action based on which button was pressed or released.

As always to get more information on the code check out the tutorial video.

/* Stepper Camera Slider with Nextion Touchscreen LCD Control

Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include <Nextion.h>          // Nextion Library by Bentley Born https://github.com/bborncr/nextion
#include <SoftwareSerial.h>   // Software Serial Port (included in Arduino IDE "no download")
#include <AccelStepper.h>     // Accelstepper by Mike Mcauley https://www.airspayce.com/mikem/arduino/AccelStepper/index.html

const int pinSTEP=2;  // Arduino Pin 2 connected to STEP pin of Easy Driver
const int pinDIR=3;   // Arduino Pin 3 connected to DIR pin of Easy Driver

AccelStepper stepper(1, pinSTEP, pinDIR);  // Setup of the AccelStepper Library

SoftwareSerial nextion(10, 11);// Nextion TX connected to Arduino pin 10 and RX to pin 11

Nextion NextionLCD(nextion, 9600); // Setup of Nextion library with name NextionLCD at 9600bps

int in_position=0;        // variable to hold IN position for slider
int out_position=0;       // variable to hold OUT position for slider
int set_speed=200;        // default travel speed between IN and OUT points
String nextion_message;   // variable to hold received message for Nextion LCD
int move_left=0;          // variable to detect move left on Nextion LCD
int move_right=0;         // variable to confirm move right on Nextion LCD
int start_cycle=0;        // variable to confirm start of travel between IN and OUT points


void setup() {
  NextionLCD.init();
  NextionLCD.setComponentText("t0", "--");  // set default text in box t0 on Nextion LCD
  NextionLCD.setComponentText("t1", "--");  // set default text in box t1 on Nextion LCD
  NextionLCD.setComponentText("t2", String(set_speed));  // set default text in box t2 on Nextion LCD
}


void loop() {
  nextion_message = NextionLCD.listen(); //check for message from Nextion touchscreen
  
  if(nextion_message != ""){ // if a message is received...
    
    if (nextion_message == "65 0 5 1 ffff ffff ffff") {  // Move Left Button
      move_left=1;
      stepper.setMaxSpeed(5000);
      stepper.setAcceleration(5000);
      stepper.setSpeed(5000);
    }

    if (nextion_message == "65 0 5 0 ffff ffff ffff") {  // Release Move Left Button
      move_left=0;
      while (stepper.distanceToGo() != 0) {  // wait for stepper to reach last destination
        stepper.run();
      }
    }    

    if (nextion_message == "65 0 6 1 ffff ffff ffff") {  // Move Right Button
      move_right=1;
      stepper.setMaxSpeed(5000);
      stepper.setAcceleration(5000);
      stepper.setSpeed(5000);
    }

    if (nextion_message == "65 0 6 0 ffff ffff ffff") {  // Release Move Right Button
      move_right=0;
      while (stepper.distanceToGo() != 0) {  // wait for stepper to reach last destination
        stepper.run();
      }
    }

    if (nextion_message == "65 0 7 1 ffff ffff ffff") {  // In Point Button
      in_position=stepper.currentPosition();  // set the IN point to the current stepper position
      NextionLCD.setComponentText("t0", String(in_position));  // display position in t0 box on Nextion
    }

    if (nextion_message == "65 0 8 1 ffff ffff ffff") {  // Out Point Button
      out_position=stepper.currentPosition();  // set the OUT point to the current stepper position
      NextionLCD.setComponentText("t1", String(out_position)); // display position in t0 box on Nextion    
    }   

    if (nextion_message == "65 0 9 1 ffff ffff ffff") {  // Play Button
        start_cycle=1;  // start travel between IN and OUT points
    } 

    if (nextion_message == "65 0 a 1 ffff ffff ffff") {  // Lower Speed Button
      if (set_speed > -1) {
        set_speed=set_speed-10;  // reduce travel speed by 10
        NextionLCD.setComponentText("t2", String(set_speed));  // update speed value in t2 box on Nextion
      }
    }       

    if (nextion_message == "65 0 b 1 ffff ffff ffff") {  // Increase Speed Button
      set_speed=set_speed+10;  // increase travel speed by 10
      NextionLCD.setComponentText("t2", String(set_speed));   // update speed value in t2 box on Nextion    
    }     
  }

  if (move_left == 1) {  // move the slider to the left while holding the left arrow on Nextion
      stepper.moveTo(stepper.currentPosition()+75);
      stepper.runSpeedToPosition();    
  }

  if (move_right == 1) {  // move the slider to the right while holding the right arrow on Nextion
      stepper.moveTo(stepper.currentPosition()-75);
      stepper.runSpeedToPosition();    
  }

  if (start_cycle == 1) {  // Move the slider between the IN and OUT position and the selected speed
    stepper.setMaxSpeed(set_speed);
    stepper.setAcceleration(set_speed);
    stepper.moveTo(in_position);
    stepper.runToPosition();
    stepper.setMaxSpeed(set_speed);
    stepper.setAcceleration((set_speed));
    stepper.moveTo(out_position);
    stepper.runToPosition();
    start_cycle=0;
  }
}
CONCLUSION

So that will do it for the Arduino Motorized Slider Project!

I want to thank everyone who followed this project, and that it may have helped to give you some ideas to build your own!

Hope to see you in the next one!

Thanks for stopping by!

TUTORIAL VIDEO
DOWNLOAD

Copy and Paste the above code/sketch in your Arduino IDE software.

Link to the libraries used in this tutorial:

Nextion Library by Bentley Born: https://github.com/bborncr/nextion

AccelStepper Library by Mike Mcauley:  https://www.airspayce.com/mikem/arduino/AccelStepper/index

Nextion Editor Software: https://nextion.tech/nextion-editor-ver-0-58/

Other Files:

Nextion Editor HMI file

Nextion .TFT file to update the Nextion LCD with MicroSd card

Nextion Enclosure .STL file for 3D printing

*The enclosure was modified from the model created by “etfrench” on Thingiverse

By | 2019-09-20T23:23:33-05:00 September 19th, 2019|Tutorials|

Leave A Comment