Voltage indicator with photosensor

Hello
I am creating an Arduino sketch that scrolls the sentence "The voltage is > 2.5V" to the left on the first row of the LCD if the voltage level at pin A5 is greater than 2.5 volts. If the voltage level at pin A5 is less than 2.5 volts, the sentence "The voltage is 2.5V" will scroll to the right on the LCD's second row.

Although I am almost done I don't know what is the issue

  • the first loop stops if it is below 2.5 but if it above it doesn't stop
  • How to break the loop if I want to transition between them
// C++ code
//
#include <LiquidCrystal.h>
const int rs=8, e=9, d4=10, d5=11, d6=12, d7=13;
LiquidCrystal lcd(rs,e,d4,d5,d6,d7);
int ls; //photoresistor
float voltage;
void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop()
{
  ls = analogRead(A5);
  voltage = ls * (5.0 / 679.0); //converting to voltage
  Serial.println(voltage); //print voltage
  int i=0;

  if (voltage < 2.5) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("The voltage is < 2.5V");
    while (i < 1) {
      // scroll one position right:
      lcd.scrollDisplayRight();
      delay(100);
      if (voltage > 2.5){
          break;
        }
    }
  }
  else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("The voltage is > 2.5V");
    while (i < 1) {
      // scroll one position left:
      lcd.scrollDisplayLeft();
      delay(100);
      if (voltage < 2.5){
          break;
        }
    }
  }
}

Hello
What is the design goal to use a while() in side the loop?

When you’re happy this is working the way you want it to, move the animation out into a function and get rid of the delay() calls.

Then your program can do something alongside the animated text!

Once you are inside the while loop, voltage never changes. You will have to read the analog input again and do the calculation.

scrolling text in the LCD to the left and right side respectively

Do you mean to make this a function?
and if I removed delay it won't move

Do you mean something like this?

Yes, that would be better.

What is the use of the variable i in while(i < 1)? It never changes as far as I can see.

i is for the loop
as long i<1 the loop will not stop till the value of photoresistor change

Would it? :face_with_hand_over_mouth:

How about

float voltage()
{
  int ls = analogRead(A5);
  return ls * (5.0 / 679.0);
}

Then you can say

        if (voltage() > 2.5){
          break;
        }

but, as @sterretje pointed out, i never changes. What's the point of a variable that never varies?

You might as well say

while(true) ...

The photoresistor can change, sure, but the variable voltage will not, right now, as also pointed out.

thank you this works but for some reason the first if condition stops scrolling after once but the second one doesn't

updated code:

// C++ code
//
#include <LiquidCrystal.h>
const int rs=8, e=9, d4=10, d5=11, d6=12, d7=13;
LiquidCrystal lcd(rs,e,d4,d5,d6,d7);

float voltage()
{
  int ls = analogRead(A5);
  return ls * (5.0 / 679.0);
}

void setup()
{
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop()
{
  Serial.println(voltage()); //print voltage

  if (voltage() < 2.5) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("The voltage is < 2.5V");
    while (true) {
      // scroll one position right:
      lcd.scrollDisplayRight();
      delay(100);
      if (voltage() > 2.5){
          break;
        }
    }
  }
  else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("The voltage is > 2.5V");
    while (true) {
      // scroll one position left:
      lcd.scrollDisplayLeft();
      delay(100);
      if (voltage() < 2.5){
          break;
        }
    }
  }
}

Try it using a real Arduino and real components and let us know if the real circuit has the same problem.

Will let you guys know next week then

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