Display analog range status on LCD

Hello, There !
Project Environment : Arduino Mega 2560, 10-bit ADC, Alphanumeric LCD

In one of the my project, i am trying to display the status of range of potentiometer only when the ADC data is stable (like the range for 10 bit ADC is 0 to 1023) so that i want to print if the range is between 0 to 200 want to display state-1, for 201 to 300 want to display state-2, for 301 to 400 want to display state-3 ! .
But in that case it shows the correct stable ADC value but shows state-2 every time for less than 1s and then update correct state. so i want hide " state-2" display before correct status.

Thank you in Advance !

Source code :

#include <LiquidCrystal.h>
LiquidCrystal lcd (38,37,36,35,34,33);

int currentValue;  
int previousValue;
int analogPin=0;

void setup() {
lcd.begin(16, 4);
}

void loop() {
  previousValue = currentValue;  
  currentValue = analogRead(analogPin);  

  if (previousValue == currentValue) {  
      if(currentValue < 200 ){lcd.setCursor(9,3); lcd.print("       "); lcd.setCursor(9,3); lcd.print("State-1"); }  
                   
   else if(currentValue >200  && currentValue < 300 ){ lcd.setCursor(9,3); lcd.print("       "); lcd.setCursor(9,3); lcd.print("State-2");  }
                       
   else if(currentValue >300 && currentValue < 400 ){ lcd.setCursor(9,3); lcd.print("       ");  lcd.setCursor(9,3); lcd.print("State-3"); }
    
  }

 else {
  lcd.setCursor(9,3); lcd.print("       ");
  }

Hello
Take some time and study the application of the switch/case instruction in detail. This will help.

How many different ranges are there and are they all of equal size ?

@UKHeliBob There are only three ranges and that are described in Question

A few ideas to refactor your code:

Don't do everything in the loop(): use functions instead. For instance, instead of repeating this hard-coded snippet

{ lcd.setCursor(9,3); lcd.print("       "); lcd.setCursor(9,3); lcd.print("State-2");

repeated 3 times, where only one (hard-coded) digit is changing, have a first function return an int (or a uint8_t) from 0 to 3. Then, have a second function build up a buffer according to what the first function returns. Finally, have a third function actually print the buffer that function 2 has made. In this way, you can test each part individually and go on to the next part when the previous one is behaving.

You don't need case, nor even else if: a chain of successive ifs will do.
For instance:

#define FLOOR_3 400
#define FLOOR_2 300
#define FLOOR_1 200

uint8_t checkState (void)
{
  uint8_t state = 0; // will be returned if every if fails

  if (currentValue < FLOOR_3) state = 3;
  if (currentValue < FLOOR_2) state = 2;
  if (currentValue < FLOOR_1) state = 1;

  return state;
}
1 Like

Hello
try this prototype code:

switch(score) {
 case 000 ... 199: System.out.println("value in 000 ... 199 range"); break;
 case 200 ... 399: System.out.println("value in 200 ... 399 range"); break;
 case 400 ... 599: System.out.println("value in 400 ... 599 range"); break;
 case 600 ... 799: System.out.println("value in 600 ... 799 range"); break;
 default: System.out.println("out of range"); break;
}

Have a nice day and enjoy coding in C++.

@330R It works fine, Thank you !

I'm glad this helped; you're welcome.

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