LCD in combination with potentiometer and LED's

Hi all,

I am quit new working with Arduino. After trying to figure out the whole morning I thought it was better to ask for help at the Arduino Forum ;).

The case is:

I want to display at my LCD screen a value from 2 till 5. This should be working by turning on the potentiometer. I have made a code which is shown below (and attached). The problem is that the LCD screen still displays a value from 0 to 1023. And actually when I rotate to fast, the value is over the 1023 (for example 5430). As well I have attached the setup of the hardware.

I have lighten the green LED, but it was for testing. In the end I want that this system displays:
Value 2/3 = RED LED ON
Value 4 = YELLOW LED ON
Value 5 = GREEN LED ON

I hope someone with a better understanding then me with Arduino can help me. Thanks a lot.

#include <LiquidCrystal.h>

// Variabelen voor LED lampjes
int LED_GROEN = 10;
int LED_GEEL = 9;
int LED_ROOD = 8;

// variabele voor LCD scherm
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
Serial.begin(9600);

pinMode(LED_GROEN, OUTPUT);
pinMode(LED_GEEL, OUTPUT);
pinMode(LED_ROOD, OUTPUT);

//pinMode(buffersize, INPUT);

lcd.begin(16, 2); // set up the LCD's number of columns and rows:

lcd.print("Buffersize ="); // Print a message to the LCD.
}

void loop() {

lcd.setCursor(0, 1); // set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0):

int potValue = analogRead(A5);
int val = map(potValue, 0, 1023, 2, 5);
lcd.print(potValue);

digitalWrite(LED_GROEN, HIGH);
}

sketch_mar29a.ino (850 Bytes)

int val = map(potValue, 0, 1023, 2, 5);
lcd.print(potValue);

Shouldn't you print val, not potValue, there?

Please read the how to use this forum stickies to see how to properly post code.

Hi Shannon,

I have adjust it and it works! Thank you.

Now I can move on and try to add the led lights :slight_smile:

To come back at this subject. I have add a code to lighten up the LED's by wanted values. Almost everythings works fine except at the value of 4/5. At the value of five the yellow led is still lighten (actually blinking). See in the pictures attached.

What could be the reason for this? Has it something to do with a tolerance or...?

#include <LiquidCrystal.h>

// Variabelen voor LED lampjes
int LED_GROEN = 10;
int LED_GEEL = 9;
int LED_ROOD = 8;

// variabele voor LCD scherm
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
Serial.begin(9600);

pinMode(LED_GROEN, OUTPUT);
pinMode(LED_GEEL, OUTPUT);
pinMode(LED_ROOD, OUTPUT);

lcd.begin(16, 2); // set up the LCD's number of columns and rows:

lcd.print("Buffersize ="); // Print a message to the LCD.
}

void loop() {

lcd.setCursor(0, 1); // set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0):

int potValue = analogRead(A5);
int val = map(potValue, 0, 1023, 2, 5);
lcd.print(val);
Serial.print("Buffersiz = ");
Serial.println (val);

if (val > 1 && val <= 3){
digitalWrite(LED_ROOD, HIGH);
}
else{
digitalWrite(LED_ROOD, LOW);
}

if (val > 3 && val <= 4){
digitalWrite(LED_GEEL, HIGH);
}
else{
digitalWrite(LED_GEEL, LOW);
}

if (val > 4 && val <= 5){
digitalWrite(LED_GROEN, HIGH);
}
else{
digitalWrite(LED_GROEN, LOW);
}

}

sketch_mar29a.ino (1.12 KB)

Now with the right way of visualizing a code in the forum, I am sorry.

#include <LiquidCrystal.h>


// Variabelen voor LED lampjes
int LED_GROEN = 10;
int LED_GEEL = 9;
int LED_ROOD = 8;



// variabele voor LCD scherm
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() {
  Serial.begin(9600);

  pinMode(LED_GROEN, OUTPUT);
  pinMode(LED_GEEL, OUTPUT);
  pinMode(LED_ROOD, OUTPUT);

  lcd.begin(16, 2); // set up the LCD's number of columns and rows:

  lcd.print("Buffersize =");  // Print a message to the LCD.
}


void loop() {

  lcd.setCursor(0, 1);   // set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0):

  int potValue = analogRead(A5);
  int val = map(potValue, 0, 1023, 2, 5);
  lcd.print(val);
  Serial.print("Buffersiz = ");
  Serial.println (val);

  if (val > 1 && val <= 3) {
    digitalWrite(LED_ROOD, HIGH);
  }
  else {
    digitalWrite(LED_ROOD, LOW);
  }

  if (val > 3 && val <= 4) {
    digitalWrite(LED_GEEL, HIGH);
  }
  else {
    digitalWrite(LED_GEEL, LOW);
  }

  if (val > 4 && val <= 5) {
    digitalWrite(LED_GROEN, HIGH);
  }
  else {
    digitalWrite(LED_GROEN, LOW);
  }

}

You are printing the value of val. What are the values? Do the values match the LEDs?

If there is noise on the pot input, place a 0.1uF cap from the analog input to ground.

You are printing the value of val. What are the values? Do the values match the LEDs?
If I understand you correct, the value's are 2 - 5. By the value of 2/3 the red light turns on, by the value of 4 the yellow light turns on, and by value 5 the green led turns on.

If there is noise on the pot input, place a 0.1uF cap from the analog input to ground.
Thank you, I will take a closer look at this :wink: