Arduino Uno & 16x2 LCD Display w/ 4 push button and 2 potentiometers

Hello,

I'm looking for assistance in generating a menu for my project. I need a 4 button menu for our design. We want to able to push 1 of the buttons and then use the potentiometer to vary and change the settings within.

For example we want to be able to push button 1 which will be labeled "Constant Current" and open that menu and be able to set a certain current value we want.

I have attached the schematic and layout of our design to hope help some more. Thank you

I need a 4 button menu

Notepad has menus. The IDE has menus. How many menus need 4 buttons?

Try again, to explain how buttons relate to menus.

Image from Original Post so we don't have to download it. See this Simple Image Guide

...R

@dmlamura, for some reason you sent me a Personal Message about this problem. I don't know why you chose me because I don't think I have ever written a menu program for an Arduino project.

I presume you have tried writing a program to do what you require so please post it and tell us exactly what it does and what you want it to do that is different.

...R

We are designing an Electronic Load (Lab Test equipment)and trying to create some sort of 4 button Menu feature as shown in the attachment picture from my post. For example when Buttons are pressed it will bring you into a settings menu to set a value for current or power factor, etc.

  • Button 1 will be labeled Constant Current and once pressed it have you set a value for current using the potentiometer.
  • Button 2 same idea but for power factor.
  • Button 3 another option, maybe crest factor or something.
    The once all the settings are set and saved you would hit
  • Button 4 "Load" to output the current and setting selected to say load down an power supply.

Below is a code we have developed so far that maybe can be tweaked, any advice or help would be greatly appreciated.

#include <LiquidCrystal.h>

#include <LiquidCrystal.h>
#include <Wire.h>
//#include <Adafruit_ADS1015.h>
//#include <Adafruit_MCP4725.h>

// LCD innitiation
const int rs = 13, en = 12, d4 = 8, d5 = 9, d6 = 10, d7 = 11;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//ADC innitiation
//Adafruit_ADS1015 ads;

//DAC innitiation
//Adafruit_MCP4725 dac;

//Buttons define
int BT1=6;
int BT2=5;
int BT3=4;
int BT4=3;

int LED1=2;
int LED2=17;
int LED3=18;
int LED4=19;

void setup() {
Serial.begin(9600); // open the serial port at 9600 bps:

//-----------------------------LCD setup
lcd.begin(16, 2);

//-----------------------------ADC setup
// The ADC input range (or gain) can be changed via the following
// functions, but be careful never to exceed VDD +0.3V max, or to
// exceed the upper and lower limits if you adjust the input range!
// Setting these values incorrectly may destroy your ADC!
// ADS1015 ADS1115
// ------- -------
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
// ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
// ads.begin();

//---------------------------DAC setup
// For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC)
// For MCP4725A0 the address is 0x60 or 0x61
// For MCP4725A2 the address is 0x64 or 0x65
// dac.begin(0x62);

// -------------------------------Buttons and led declaration
pinMode(3,INPUT_PULLUP) ; //Button 1
pinMode(4,INPUT_PULLUP) ; //Button 2
pinMode(5,INPUT_PULLUP) ; //Button 3
pinMode(6,INPUT_PULLUP) ; //Button 4
pinMode(7,OUTPUT) ; //RELCONT pin to controll main relay
pinMode(2,OUTPUT) ; // LED 1
pinMode(A3,OUTPUT) ; // LED 2
pinMode(A4,OUTPUT) ; // LED 3
pinMode(A5, OUTPUT); // LED 4
pinMode(A0, INPUT); // POT1 read control potentiometer

}

void loop() {

POT_test();
BT_LED_test();
}

void LCD_test(){
lcd.setCursor(0, 0);
lcd.print("LCD TEST LINE #1");
lcd.setCursor(0, 1);
lcd.print("LCD TEST LINE #2");
}
void BT_LED_test(){
if (digitalRead(BT1)==1){
digitalWrite(LED1,LOW);
lcd.setCursor(0,0);
lcd.print("BT1_H");
}
else{
digitalWrite(LED1,HIGH);
lcd.setCursor(0,0);
lcd.print("BT1_L");
}
Serial.print(digitalRead(BT1));
Serial.print("\n");
//-----------------------------------
if (digitalRead(BT2)==1){
digitalWrite(LED2,LOW);
lcd.setCursor(6,0);
lcd.print("BT2_H");
}
else{
digitalWrite(LED2,HIGH);
lcd.setCursor(6,0);
lcd.print("BT2_L");
}
Serial.print(digitalRead(BT2));
Serial.print("\n");
//-----------------------------------
if (digitalRead(BT3)==1){
digitalWrite(LED3,LOW);
lcd.setCursor(11,0);
lcd.print("BT3_H");
}
else{
digitalWrite(LED3,HIGH);
lcd.setCursor(11,0);
lcd.print("BT3_L");
}
Serial.print(digitalRead(BT3));
Serial.print("\n");
//-------------------
if (digitalRead(BT4)==1){
digitalWrite(LED4,LOW);
lcd.setCursor(0,1);
lcd.print("BT4_H");
}
else{
digitalWrite(LED4,HIGH);
lcd.setCursor(0,1);
lcd.print("BT4_L");
}
Serial.print(digitalRead(BT4));
Serial.print("\n");

}

void POT_test(){
int aread=analogRead(A0);
lcd.setCursor(9,1);
lcd.print("POT");
lcd.setCursor(12,1);
lcd.print(aread);
}
/* void ADC_test()
{
int16_t adc0, adc1, adc2, adc3;
adc0 = ads.readADC_SingleEnded(0);
adc1 = ads.readADC_SingleEnded(1);
adc2 = ads.readADC_SingleEnded(2);
adc3 = ads.readADC_SingleEnded(3);
Serial.print("AIN0: "); Serial.println(adc0);
Serial.print("AIN1: "); Serial.println(adc1);
Serial.print("AIN2: "); Serial.println(adc2);
Serial.print("AIN3: "); Serial.println(adc3);
Serial.println(" ");
}

void DAC_test() {
uint32_t counter;
// Run through the full 12-bit scale for a triangle wave
for (counter = 0; counter < 4095; counter++)
{
dac.setVoltage(counter, false);
}
for (counter = 4095; counter > 0; counter--)
{
dac.setVoltage(counter, false);
}
}*/

To make it easy for people to help you please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

And you have not told us what the program does or what you want it to do differently so that we might know which part to look at.

...R

Here is the Code:

#include <LiquidCrystal.h>

#include <LiquidCrystal.h>
#include <Wire.h>
//#include <Adafruit_ADS1015.h>
//#include <Adafruit_MCP4725.h>

// LCD innitiation
const int rs = 13, en = 12, d4 = 8, d5 = 9, d6 = 10, d7 = 11;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//ADC innitiation
//Adafruit_ADS1015 ads;

//DAC innitiation
//Adafruit_MCP4725 dac;

//Buttons define
int BT1=6;
int BT2=5;
int BT3=4;
int BT4=3;

int LED1=2;
int LED2=17;
int LED3=18;
int LED4=19;


void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:

  //-----------------------------LCD setup
  lcd.begin(16, 2);

  //-----------------------------ADC setup
  // The ADC input range (or gain) can be changed via the following
  // functions, but be careful never to exceed VDD +0.3V max, or to
  // exceed the upper and lower limits if you adjust the input range!
  // Setting these values incorrectly may destroy your ADC!
  //                                                                ADS1015  ADS1115
  //                                                                -------  -------
  // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  // ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  // ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
//    ads.begin();

  //---------------------------DAC setup
  // For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC)
  // For MCP4725A0 the address is 0x60 or 0x61
  // For MCP4725A2 the address is 0x64 or 0x65
//  dac.begin(0x62);
  
  // -------------------------------Buttons and led declaration
  pinMode(3,INPUT_PULLUP) ; //Button 1
  pinMode(4,INPUT_PULLUP) ; //Button 2
  pinMode(5,INPUT_PULLUP) ; //Button 3
  pinMode(6,INPUT_PULLUP) ; //Button 4 
  pinMode(7,OUTPUT) ;       //RELCONT pin to controll main relay
  pinMode(2,OUTPUT) ;       // LED 1
  pinMode(A3,OUTPUT) ;       // LED 2
  pinMode(A4,OUTPUT) ;       // LED 3
  pinMode(A5, OUTPUT);      // LED 4
  pinMode(A0, INPUT);       // POT1 read control potentiometer

  
}


void loop() {
 
 POT_test();
 BT_LED_test();
}

  void LCD_test(){
  lcd.setCursor(0, 0);
  lcd.print("LCD TEST LINE #1");
  lcd.setCursor(0, 1);
  lcd.print("LCD TEST LINE #2");  
  }
  void BT_LED_test(){
    if (digitalRead(BT1)==1){
      digitalWrite(LED1,LOW);
        lcd.setCursor(0,0);
        lcd.print("BT1_H");
     }
     else{
        digitalWrite(LED1,HIGH);
        lcd.setCursor(0,0);
        lcd.print("BT1_L");
      }
      Serial.print(digitalRead(BT1));
      Serial.print("\n");
//-----------------------------------
         if (digitalRead(BT2)==1){
      digitalWrite(LED2,LOW);
        lcd.setCursor(6,0);
        lcd.print("BT2_H");
     }
     else{
        digitalWrite(LED2,HIGH);
        lcd.setCursor(6,0);
        lcd.print("BT2_L");
      }
      Serial.print(digitalRead(BT2));
      Serial.print("\n");
//-----------------------------------
         if (digitalRead(BT3)==1){
      digitalWrite(LED3,LOW);
        lcd.setCursor(11,0);
        lcd.print("BT3_H");
     }
     else{
        digitalWrite(LED3,HIGH);
        lcd.setCursor(11,0);
        lcd.print("BT3_L");
      }
      Serial.print(digitalRead(BT3));
      Serial.print("\n");
//-------------------
         if (digitalRead(BT4)==1){
      digitalWrite(LED4,LOW);
        lcd.setCursor(0,1);
        lcd.print("BT4_H");
     }
     else{
        digitalWrite(LED4,HIGH);
        lcd.setCursor(0,1);
        lcd.print("BT4_L");
      }
      Serial.print(digitalRead(BT4));
      Serial.print("\n");

  }

  void POT_test(){
    int aread=analogRead(A0);
    lcd.setCursor(9,1);
    lcd.print("POT");
    lcd.setCursor(12,1);
    lcd.print(aread);
  }
/*  void ADC_test() 
{
  int16_t adc0, adc1, adc2, adc3;
  adc0 = ads.readADC_SingleEnded(0);
  adc1 = ads.readADC_SingleEnded(1);
  adc2 = ads.readADC_SingleEnded(2);
  adc3 = ads.readADC_SingleEnded(3);
  Serial.print("AIN0: "); Serial.println(adc0);
  Serial.print("AIN1: "); Serial.println(adc1);
  Serial.print("AIN2: "); Serial.println(adc2);
  Serial.print("AIN3: "); Serial.println(adc3);
  Serial.println(" ");
}

void DAC_test() {
    uint32_t counter;
    // Run through the full 12-bit scale for a triangle wave
    for (counter = 0; counter < 4095; counter++)
    {
      dac.setVoltage(counter, false);
    }
    for (counter = 4095; counter > 0; counter--)
    {
        dac.setVoltage(counter, false);
    }
}*/

Right now we have the 4 Buttons labeled BT1 BT2, etc. We want BT1 labeled "CC" for constant current BT2 "PF" power factor BT3 "CF" crest factor BT4 "Run" to apply the selected values. When pushing BT1 (CC) we want a new menu to open for us to use the potentiometer to vary the current desired and then have it saved some how. Same goes for BT2 & BT3. Once all the desired values are saved in those menus we can then press BT4 and apply the selected value to load down a power supply or component/sample we are testing.

Once all the desired values are saved in those menus

A menu is a list of choices. You can NOT save data in a menu.

Your code looks like it was typed by a drunken monkey. Use Tools + Auto Format to fix the horrid indenting, and get that monkey into treatment.

http://forum.arduino.cc/index.php?topic=38053.0

I used this library few times very easy to use. Latest version is 1.4 i guess.

dmlamura:
When pushing BT1 (CC) we want a new menu to open for us to use the potentiometer to vary the current desired and then have it saved some how.

That does not sound like what I understand a menu to be. It seems more like a push-button to choose a particular part of the program.

I would have a piece of code (a function) that reads the buttons and save their values.

I would have another function that reads the potentiometer and then depending on the button-value, saves the ADC-value to the appropriate variable.

Do you have a special "save" button to tell the program when the ADC-value should be saved.

You have some functions in your program already but I think it will be easier to implement what you want if you divide the code further. For example I would have an lcdDisplay() function that updates the display rather than trying to do that in several different parts of the program. That also makes it easier to reduce the frequency of updates - the human brain will probably be content with 2 to 4 updates per second.

...R

This is the website i'm using and uploading the code into. How do I implement those features to be able to do something like that? Mainly just 1 feature like selecting "CC" (Constant current) button and setting the current and applying it to output. How to break code down?

If you look at my schematic and coding using this link it may help you understand a little better. When we press BT1 it changes from _H to L. On the LCD screen when the code is running you will see BT_1 BT_2 BT_3 BT_4 and POT#####, those are for the 4 buttons and the Potentiometer. I know how to relabel the button but we want to be able to click BT_1 and then have the potentiometer function in there where we can set the value (current) to say 1 amp then click BT_4 and output the load.

Here is the tinkercad program we are using and trying to alter. We have no experience with Ardiuno and programming and seeking all the help we can get. Thank you

When we press BT1 it changes from _H to _L.

What changes?

Here is the tinkercad program we are using and trying to alter.

This is NOT the tinkercad forum, despite the similarity of names. Of which there is NONE.

PaulS:
What changes?
This is NOT the tinkercad forum, despite the similarity of names. Of which there is NONE.

Right now we just have it set up as BT1_H and when the button is pressed it will say BT1_L just to show the button press is being recognized. What we want to implement is just to have it say BT1 and then when its pressed a new window or screen will open and say Current ### and then we can use the potentiometer to set the current. Then be able to Press BT4 for it to output the current set.

dmlamura:
This is the website i'm using and uploading the code into.

Does that mean that you are not trying your code on a real Arduino (or clone) with real switches?

If so I don't have a clue how to help.

...R

Robin2:
Does that mean that you are not trying your code on a real Arduino (or clone) with real switches?

If so I don't have a clue how to help.

...R

The program is a replica of our design and layout of our board. We are trying to get it to run on here first and on the LCD display before we upload it into the actual Arduino in our design.

dmlamura:
The program is a replica of our design and layout of our board. We are trying to get it to run on here first and on the LCD display before we upload it into the actual Arduino in our design.

To my (old-fogey) mind it will be much easier just to try it on the actual Arduino. Then there is no need to worry about how compatible is the simuator.

...R

Robin2:
To my (old-fogey) mind it will be much easier just to try it on the actual Arduino. Then there is no need to worry about how compatible is the simuator.

...R

With the coding I have and provided. Do you know how I can alter it so when I click BT_1 (button 1) it will give me an option on the LCD design where I can use the potentiometer to adjust the current from say 0 to 5A?

dmlamura:
With the coding I have and provided. Do you know how I can alter it so when I click BT_1 (button 1) it will give me an option on the LCD design where I can use the potentiometer to adjust the current from say 0 to 5A?

Have you considered what I suggested in Reply #9 - that describes how I would do it.

...R

I'm trying to break it up and follow your instruction but having difficulty and developing the correct code to do so.

Thank you