Control Display Module 7-Segment 2 Digit LED display with potentiometer

Hi,

I'm using Big Easy Driver for controlling a stepper motor with a potentiometer for speed of rotation. Also, I'm using buttons for start/stop - reverse and speed up funtions of the motor.

So far so good.

Now, my goal is to have a Display Module 7-Segment 2 Digit LED display, just for using it as a reference for me, to show where the value of speed of the motor is.

Here is my code for controlling the stepper :

#include <Stepper.h>

Stepper stepper(400, 9, 8);

const int buttonPin = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;
const int buttonPin4 = 6;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

const int led1 = 10;
const int led2 = 11;
const int led3 = 12;
const int led4 = 13;


void setup()
{
  
  pinMode(buttonPin, INPUT);//set inputs and outputs.
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
 }

void loop()
{
 int buttonState = digitalRead(buttonPin);
 int buttonState2 = digitalRead(buttonPin2);
 int buttonState3 = digitalRead(buttonPin3);
 int buttonState4 = digitalRead(buttonPin4);
 int motorStep = 30;
 
  
  int val = analogRead(A0); // get the sensor value
  int motorSpeed = map(val, 0, 1023, 0, 1500);//motor speed range.


 if (buttonState2 == HIGH){ // BUTTON 2
 motorSpeed *= 2;  // double the speed of the motor.
  digitalWrite(led2, HIGH);
  }
  else {
    digitalWrite(led2, LOW); // the led for double the speed.
  }

   if (buttonState4 == HIGH){ // BUTTON 4
 motorSpeed *= 4;  // x4 the speed of the motor.
  digitalWrite(led4, HIGH);
  }
  else {
    digitalWrite(led4, LOW); // the led for x4
  }

   
   stepper.setSpeed(motorSpeed); // set the speed of the motor in RPM.
   
  
  if (buttonState == LOW){ //BUTTON 1
    motorStep = 0; //  Switch to start/stop the motor  
    digitalWrite(led1, LOW);
  }
  else {
    digitalWrite(led1, HIGH);
  }
 
  if (buttonState3 == HIGH){ // BUTTON 3
 motorStep = - motorStep;  // Reverse motion.
  digitalWrite(led3, HIGH);
  }
  else {
    digitalWrite(led3, LOW); // the led for reverse.
  }
  
  
  stepper.step(motorStep);  
  
}

Here is my connections

The segment display that I have is the following:
https://grobotronics.com/display-module-7-segment-2-digit.html

I can't find online a decent tutorial of what I trying to do.
Again, I'm trying to get a reference value of my "motorSpeed" value, mapped from 0 to 99 (cause of the 2 digits)

I hope my query will be clear for someone reading this post.

Any comment or advice will be appreciated :slight_smile:

Thank you,
Jtel

seems they are just using the 74HC595 to control the displays (with load, data in, and clock)

you can read a basic tutorial on how they work here and the ShiftDisplay library could possibly work

EDIT: I noticed a comment from an aliexpress Russian customer here for a similar module who posted a couple images of Arduino code showing the mapping

Using a similar statement you can convert to your data to display speed. int motordisplay = map(val, 0, 1023, 0, 99);//motor speed range. Posting a Schematic showing the display would be a big help, the frizzy thing is pretty picture with a bunch of colored lines.

Hi again,

After a long time I decide to finish this code and project.

Thank you for your replies!
Really helpful to me.

I'm trying to control the 2 digit LED display with the value of a potentiometer while moving the motor.
I have found this link here that's pretty close of what I'm trying to do.

Here is my code :

#include <Stepper.h>
#include <ShiftRegister74HC595.h>

Stepper stepper(400, 9, 8);
ShiftRegister74HC595 sr (2, 0, 1, 2); // 2 digits, outputs from 0,1 & 2 pins of Arduino
                                      //SDI 0, CLOCK 1, LOAD 2


int value,digit1,digit2; 
uint8_t  numberB[] = {B11000000, //0
                      B11111001, //1 
                      B10100100, //2
                      B10110000, //3 
                      B10011001, //4
                      B10010010, //5
                      B10000011, //6
                      B11111000, //7
                      B10000000, //8
                      B10011000, //9
                      B10110011, //R
                      B10110000 //E
                     };



const int buttonPin = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;
const int buttonPin4 = 6;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

const int led1 = 10;
const int led2 = 11;
const int led3 = 12;
const int led4 = 13;


void setup()
{
  
  pinMode(buttonPin, INPUT);//set inputs and outputs.
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
 }

void loop()
{
 int buttonState = digitalRead(buttonPin);
 int buttonState2 = digitalRead(buttonPin2);
 int buttonState3 = digitalRead(buttonPin3);
 int buttonState4 = digitalRead(buttonPin4);
 int motorStep = 30;
 
  
  int val = analogRead(A0); // get the sensor value
  int motorSpeed = map(val, 0, 1023, 0, 1500);//motor speed range.
  int dig1 = map(val, 0, 1023, 0, 99); //digits range

  //DIGITS EXPORT

    digit1=dig1 % 10 ; // how to avoid these values when I'm pushing a button?
    digit2=(dig1 / 10) % 10; // how to avoid these values when I'm pushing a button?
    
  uint8_t numberToPrint[]= {numberB[digit2],numberB[digit1]};
    sr.setAll(numberToPrint); 


 // BUTTONS & LEDS CONTROL + SPECIFIC VALUE ON DISPLAY

 if (buttonState2 == HIGH){ // BUTTON 2
 motorSpeed *= 2;  // double the speed of the motor.
 uint8_t numberToPrint[]= {numberB[0],numberB[2]}; // I will need to show only 0 & 2
 sr.setAll(numberToPrint); 
  digitalWrite(led2, HIGH);
  }
  else {
    digitalWrite(led2, LOW); // the led for double the speed.
  }

   if (buttonState4 == HIGH){ // BUTTON 4
 motorSpeed *= 4;  // x4 the speed of the motor.
 uint8_t numberToPrint[]= {numberB[0],numberB[4]}; // I will need to show only 0 & 4
 sr.setAll(numberToPrint); 
  digitalWrite(led4, HIGH);
  }
  else {
    digitalWrite(led4, LOW); // the led for x4
  }

   
   stepper.setSpeed(motorSpeed); // set the speed of the motor in RPM.
   
  
  if (buttonState == LOW){ //BUTTON 1
    motorStep = 0; //  Switch to start/stop the motor  
    digitalWrite(led1, LOW);
  }
  else {
    digitalWrite(led1, HIGH);
  }
 
  if (buttonState3 == HIGH){ // BUTTON 3
 motorStep = - motorStep;  // Reverse motion.
  digitalWrite(led3, HIGH);
  }
  else {
    digitalWrite(led3, LOW); // the led for reverse.
  }
  
  
  stepper.step(motorStep);  
  
}


This works fine with the potentiometer. The display module goes from 00 to 99 while moving it.

As an extra feature I would like to show to the display only specific numbers when I'm pushing specific buttons (e.g. when I 'm pushing button2 I would like to show "02")

The problem is that the module shows these numbers but can't avoid having the previous "value" that is set by the potentiometer.

Please watch this small video to understand better.
video

I have value "71" and when I'm pushing button2 only shows "02" above the "71".

How to avoid that and have a clear 0 and 2, when I'm pushing any button?

Any comments suggestions or advice are welcome.
Thank you for your time

Jtel

Try this (untested)

#include <Stepper.h>
#include <ShiftRegister74HC595.h>

Stepper stepper(400, 9, 8);
ShiftRegister74HC595 sr (2, 0, 1, 2); // 2 digits, outputs from 0,1 & 2 pins of Arduino
//SDI 0, CLOCK 1, LOAD 2


int value, digit1, digit2;
uint8_t  numberB[] = {B11000000, //0
                      B11111001, //1
                      B10100100, //2
                      B10110000, //3
                      B10011001, //4
                      B10010010, //5
                      B10000011, //6
                      B11111000, //7
                      B10000000, //8
                      B10011000, //9
                      B10110011, //R
                      B10110000 //E
                     };



const int buttonPin = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;
const int buttonPin4 = 6;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

const int led1 = 10;
const int led2 = 11;
const int led3 = 12;
const int led4 = 13;


void setup()
{

  pinMode(buttonPin, INPUT);//set inputs and outputs.
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
}

void loop()
{
  int buttonState = digitalRead(buttonPin);
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);
  int buttonState4 = digitalRead(buttonPin4);
  int motorStep = 30; // I think this is a mistake! Please try to figure out why


  int val = analogRead(A0); // get the sensor value
  int motorSpeed = map(val, 0, 1023, 0, 1500);//motor speed range.

  //DIGITS EXPORT

  if (buttonState2 == LOW && buttonState4 == LOW) {
    int dig1 = map(val, 0, 1023, 0, 99); //digits range
    digit1 = dig1 % 10 ;
    digit2 = (dig1 / 10) % 10;
    uint8_t numberToPrint[] = {numberB[digit2], numberB[digit1]};
    sr.setAll(numberToPrint);
  }

  // BUTTONS & LEDS CONTROL + SPECIFIC VALUE ON DISPLAY

  if (buttonState2 == HIGH) { // BUTTON 2
    motorSpeed *= 2;  // double the speed of the motor.
    uint8_t numberToPrint[] = {numberB[0], numberB[2]}; // I will need to show only 0 & 2
    sr.setAll(numberToPrint);
    digitalWrite(led2, HIGH);
  }
  else {
    digitalWrite(led2, LOW); // the led for double the speed.
  }

  if (buttonState4 == HIGH) { // BUTTON 4
    motorSpeed *= 4;  // x4 the speed of the motor.
    uint8_t numberToPrint[] = {numberB[0], numberB[4]}; // I will need to show only 0 & 4
    sr.setAll(numberToPrint);
    digitalWrite(led4, HIGH);
  }
  else {
    digitalWrite(led4, LOW); // the led for x4
  }


  stepper.setSpeed(motorSpeed); // set the speed of the motor in RPM.


  if (buttonState == LOW) { //BUTTON 1
    motorStep = 0; //  Switch to start/stop the motor
    digitalWrite(led1, LOW);
  }
  else {
    digitalWrite(led1, HIGH);
  }

  if (buttonState3 == HIGH) { // BUTTON 3
    motorStep = - motorStep;  // Reverse motion.
    digitalWrite(led3, HIGH);
  }
  else {
    digitalWrite(led3, LOW); // the led for reverse.
  }


  stepper.step(motorStep);

}

Hi PaulRB,

Thank you for your reply!
This is really works!
Thank you very much :slight_smile:

Next step is to give some kind of "priority" to the values of the display module.
In other words, when I push a combination of buttons to get only the value that I want to show.

Here are the priorities that I want to show on the display :

  1. when button2 and button4 are HIGH -- show only the "04"
  2. when button2 and button3 are HIGH -- show only the "00"
  3. when button4 and button3 are HIGH -- show only the "00"
  4. when butoon2 and button3 and button4 are HIGH -- show only the "00"

I try to use while loop but it seems that is stuck whenever a combination of buttons are HIGH

Of course I want to achieve all these "priorities" without affecting that "if" command that PaulRB wrote with success (whenever buttons 2 & 4 are LOW....) And of course does not affect the motion of the stepper motor.

Any advice or comments are welcome.
Thank you for time

Jtel

Try this approach:

if (<priority 1 condition>) {
  <priority 1 action>
  }
else if (<priority 2 condition>) {
  <priority 2 action>
  }
else if (<priority 3 condition>) {
  <priority 3 action>
  }
else if (<priority 4 condition>) {
  <priority 4 action>
  }
else {
  <action if no other priorities are present>
  }

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