I am trying to build an Exposure Meter with Arduino Uno. The project is supposed to take values for several parameters as input and give the results for four other parameters based on the inputs given. My code ios given here.
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define upmenu 2
#define downmenu 3
#define upval 4
#define downval 5
#define setbutton 6
#define sensorpin A1
unsigned int i = 0;
class parameters{
private:
String head;
String para;
int* int_vals;
float* float_vals;
int len;
int final_val;
float final_valdec;
public:
parameters(String n, String x, int* a, int l){
head = n;
para = x;
int_vals = a;
len = l;
final_val = *a;
}
parameters(String n, String x, float* b, int l){
head = n;
para = x;
float_vals = b;
len = l;
final_valdec = *(b+3);
}
void getName(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(head);
}
int getIntval(int i){
i = i%len;
lcd.setCursor(0,1);
lcd.print(para+" = ");
lcd.setCursor(7,1);
lcd.print(*(int_vals+i));
lcd.setCursor(12,1);
lcd.print(final_val);
return *(int_vals+i);
}
float getDecval(int i){
i = i%len;
lcd.setCursor(0,1);
lcd.print(para+" = ");
lcd.setCursor(7,1);
lcd.print(*(float_vals+i));
lcd.setCursor(12,1);
lcd.print(final_valdec);
return *(float_vals+i);
}
int getLen(){
return len;
}
void setInt(int x){
final_val = x;
}
void setDec(float *b, int x){
final_valdec = *(b+x);
}
int getFinal(){
return final_val;
}
float* getFinal_dec(){
return &final_valdec;
}
};
int isoval[] = {12, 24, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 530, 640, 800, 1020, 1250, 1600};
int soval[] = {5, 10, 15, 20, 25, 45, 60, 75, 90, 144, 160, 165, 170, 172, 180, 270, 300};
int ssval[] = {1, 2, 4, 8, 15, 30, 48, 50, 60, 100, 125, 250, 500, 1000, 2000, 2500, 4000, 5000, 10000, 20000};
const int sdVal = 360;
int fpsval[] = {6, 12, 18, 24, 30, 36, 48, 72, 96, 144, 192};
float ffval[] = {.33, .5, .67, 1, 1.33, 1.5, 1.67, 2, 2.33, 2.5, 2.67, 3};
parameters iso = parameters("ISO-SENSITIVITY","ISO", isoval, 20);
parameters so = parameters("SHUTTER OPENING","SO", soval, 17);
parameters ss = parameters("SHUTTER SPEED","SS", ssval, 20);
parameters fps = parameters("FRAME PER SECOND","FPS", fpsval, 11);
parameters ff = parameters("FILTER FACTOR","FF", ffval, 12);
parameters opts[] = {iso, so, ss, fps, ff};
float aperture;
float lux;
float footcandle;
float ev;
float reading;
int menu_pos = 0;
int val_pos = 0;
int ff_pos = 0;
void menuchange(int up, int down);
void valuechange(int up, int down);
void showVals(int x);
void disp(int x, int y);
void setValue(int x, int y);
void op();
float sensorop(){
float temp = analogRead(sensorpin);
return temp;
}
void setup(){
lcd.begin(16, 2);
lcd.clear();
pinMode(upmenu, INPUT_PULLUP);
pinMode(downmenu, INPUT_PULLUP);
pinMode(upval, INPUT_PULLUP);
pinMode(downval, INPUT_PULLUP);
pinMode(setbutton, INPUT_PULLUP);
pinMode(sensorpin, INPUT);
lcd.setCursor(1,0);
lcd.print("Exposure Meter");
}
void loop(){
int menuup = digitalRead(upmenu);
int menudown = digitalRead(downmenu);
int valup = digitalRead(upval);
int valdown = digitalRead(downval);
int set = digitalRead(setbutton);
if(menuup^menudown){ //If asked to change the menu
menuchange(menuup, menudown);
}
if(valup^valdown){ //If asked to change the value of a parmeter
valuechange(valup, valdown);
}
if(set){ //set value for a parameter
setValue(menu_pos%9, val_pos%(opts[menu_pos%9].getLen()));
}
reading = sensorop();
delay(200);
}
void menuchange(int up, int down){
if(up){
menu_pos++;
}
if(down && menu_pos>0){
menu_pos--;
}
disp(menu_pos%9, val_pos%(opts[menu_pos%9].getLen()));
delay(200);
}
void valuechange(int up, int down){
if(up){
val_pos++;
}
if(down && val_pos>0){
val_pos--;
}
disp(menu_pos%9, val_pos%(opts[menu_pos%9].getLen()));
delay(200);
}
void setValue(int x, int y){
if(x<=3) opts[x].setInt(opts[x].getIntval(y));
if(x==4){
ff_pos = y;
opts[x].getDecval(ff_pos);
}
}
void showVals(int x, int y){
int menuup = digitalRead(upmenu);
int menudown = digitalRead(downmenu);
if(x<=3) opts[x].getIntval(y);
if(x==4) opts[x].getDecval(y);
if(x==5 || x==6){
lcd.noDisplay();
lux = float(0.005*reading);
footcandle = (lux/10.76391);
int isof = opts[0].getFinal();
int sof = opts[1].getFinal();
int ssf = opts[2].getFinal();
int fpsf = opts[3].getFinal();
float fff=ffval[ff_pos];
delay(100);
lcd.display();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("LUX");
lcd.setCursor(5,0);
lcd.print("FOOTCANDLE");
lcd.setCursor(0,1);
lcd.print(lux);
lcd.setCursor(8,1);
lcd.print(footcandle);
}
if(x==7 ||x==8){
lcd.noDisplay();
lux = float(0.005*reading);
footcandle = (lux/10.76391);
int isof = opts[0].getFinal();
int sof = opts[1].getFinal();
int ssf = opts[2].getFinal();
int fpsf = opts[3].getFinal();
float fff=ffval[ff_pos];
aperture=isof*footcandle*sof*2;
aperture=aperture/(float(sdVal)*float(ssf)*float(fpsf)*fff);
aperture=sqrt(aperture);
ev=log(aperture*aperture*float(ssf))/log(2);
delay(100);
lcd.display();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("APERTURE");
lcd.setCursor(11,0);
lcd.print("EV");
lcd.setCursor(3,1);
lcd.print(aperture);
lcd.setCursor(11,1);
lcd.print(ev);
}
}
void disp(int menu_pos, int val_pos){
opts[menu_pos].getName();
showVals(menu_pos, val_pos);
}
Now the problem is after setting the values when I go in the output options the LCD shows some garbage display before showing the desired output. I seek help to eliminate this problem