My code currently calculates 4 random numbers, from 0-32 by calling "random(33)". It then displays these and the average on an LCD display.
However, I keep getting numbers higher than 32, which sometimes affects the average and sometimes doesn't)
/*
LiquidCrystal Library - Custom Characters
Demonstrates how to add custom characters on an LCD display.
The LiquidCrystal library works with all LCD displays that are
compatible with the Hitachi HD44780 driver. There are many of
them out there, and you can usually tell them by the 16-pin interface.
This sketch prints "I <heart> Arduino!" and a little dancing man
to the LCD.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K potentiometer:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* 10K poterntiometer on pin A0
created21 Mar 2011
by Tom Igoe
Based on Adafruit's example at
https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
Also useful:
http://icontexto.com/charactercreator/
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// make some custom characters:
byte bar0[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b11111
};
byte bar1[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b11111,
0b11111
};
byte bar2[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b11111,
0b11111,
0b11111
};
byte bar3[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b11111,
0b11111,
0b11111,
0b11111
};
byte bar4[8] = {
0b00000,
0b00000,
0b00000,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
byte bar5[8] = {
0b00000,
0b00000,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
byte bar6[8] = {
0b00000,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
// this will be an array to hold bargraph values. It's currently just holding fixed values)
char display[4][12]={
{
254,(byte)0,254,254,(byte)1,254,254,(byte)2,254,254,(byte)3,254 }
,
{
254,255,254,254,255,254,254,255,254,254,255,254 }
,
{
49,255,254,50,255,254,51,255,254,52,255,254 }
,
{
254,255,254,254,255,254,254,255,254,254,255,254 }
,
};
unsigned long previousMillis = 0;
int cyl1,cyl2,cyl3,cyl4;
void setup() {
randomSeed(analogRead(A0));
// create new characters
lcd.createChar(0, bar0);
lcd.createChar(1, bar1);
lcd.createChar(2, bar2);
lcd.createChar(3, bar3);
lcd.createChar(4, bar4);
lcd.createChar(5, bar5);
lcd.createChar(6, bar6);
// set up the lcd's number of columns and rows:
lcd.begin(20, 4);
// Print a message to the lcd.
// lcd.print(" Arduino! ");
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > 5000) { // refresh the display every 5 seconds
// save the last time you blinked the LED
previousMillis = currentMillis;
// get some random cylinder readings
cyl1=random(33);
cyl2=random(33);
cyl3=random(33);
cyl4=random(33);
int average=(cyl1+cyl2+cyl3+cyl4)/4;
lcd.setCursor(0, 0);
lcd.print("1:");
lcd.print(cyl1);
lcd.setCursor(0, 1);
lcd.print("2:");
lcd.print(cyl2);
lcd.setCursor(0, 2);
lcd.print("3:");
lcd.print(cyl3);
lcd.setCursor(0, 3);
lcd.print("4:");
lcd.print(cyl4);
for(int j=0;j<4;j++){
lcd.setCursor(5, j);
for(int i=0;i<12;i++){
lcd.write(display[j][i]);
}
}
lcd.setCursor(17,1); // second row down
lcd.print("Ave");
lcd.setCursor(18,2); // third row down
lcd.print(average);
}
}
Here's a sample output: