LED Gibberish

Hey guys,

Just started messing with 16x2 LCD screens. I'm getting gibberish characters after my output readings for an analog pot.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int numReadings = 10;

int readings[numReadings]; // Array for average
int index = 0;
int total = 0;
int average = 0;
const int analogInPin = 0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
int prevValue = 0;          // For limiting 

void setup() 
{
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
    lcd.begin(16, 2);
}

void loop() {
  total= total - readings[index];
  readings[index] = analogRead(analogInPin);
  total= total + readings[index];
  index = index + 1;
  if (index >= numReadings)
  index = 0;
  average = total / numReadings;

  sensorValue = analogRead(analogInPin);            

  outputValue = map(sensorValue, 0, 1023, 0, 255); 
  average = map(sensorValue, 0, 1023, 0, 255); 

  analogWrite(analogOutPin, outputValue);
  if (average!=prevValue) {
    lcd.setCursor(0,0);
    lcd.println(average);
  }
  delay(10);
  average = prevValue;  
}

Does the LCD actually work? Have you tried:

lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.println("hello");

And then do nothing else. Does that work?

Also I don't see how this will work:

int readings[numReadings]; // Array for average
int index = 0;
int total = 0;

If the readings can be an int, surely you need a bigger number to hold the total (10 ints)?

There is no documentation of lcd.println ever exists. The only documented printing method is lcd.print, there is no ln.

OK, well:

lcd.print("hello");

Or this, even, following on from the thread about streaming:

#include <LiquidCrystal.h>
#include <Streaming.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
{
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd << "hello";
}

void loop () {}

Thanks, works now. Got rid of the "println" and replaced with "print".

Now I just have to update it so that when there aren't 3 digits and it rolls down to 1, it doesn't leave the residual numbers there.

Also I don't see how this will work:

int readings[numReadings]; // Array for average
int index = 0;
int total = 0;

If the readings can be an int, surely you need a bigger number to hold the total (10 ints)?
[/quote]

That was just copied and pasted right from the smoothing example. But that is a very good point. It should be a Long and not an Integer.

thedefog:
Thanks, works now. Got rid of the "println" and replaced with "print".

Now I just have to update it so that when there aren't 3 digits and it rolls down to 1, it doesn't leave the residual numbers there.

I normally print a few extra empty spaces as a sloppy way to erase residual numbers:

lcd.print(" ");

If desired, you may use sprintf to control exactly how many digits to output and what kind of padding etc. This will be total control at the cost of a few K of FLASH.

I normally print a few extra empty spaces as a sloppy way to erase residual numbers:

lcd.print(" ");

If desired, you may use sprintf to control exactly how many digits to output and what kind of padding etc. This will be total control at the cost of a few K of FLASH.

Thanks for the info. I'll try both.