ok, now i get the flow step almost done. can change value up and down in 0001-9999.
#include <LiquidCrystal.h>
#include <EEPROM.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
/*
Right Value = 0
Up Value = 1
Down Value = 2
Left Value = 3
Select Value = 4
*/
int adc_key_val[5] ={
30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
int lcdTopRow = 0;
int lcdBottomRow = 1;
int flashSetting = 1;
int flashDelay = 0;
int reading = 0;
//initial for ON/OFF
int backLightON = HIGH;
int backLightOFF = LOW;
int ledpinON = HIGH;
int ledpinOFF = LOW;
//initial for pin I/O
int backLightControl = 10; // Output to control back light on LCD
int ledpin = 13; // Heartbeat indicator
int sensor = 2; //sensor signal
int flash = 11; //output to on flash
int camera = 12;
int valve = 3;
int sensorStat = 0;
int cursorValue = 0;
void setup()
{
pinMode(backLightControl, OUTPUT);
pinMode(ledpin, OUTPUT);
pinMode(sensor, INPUT);
pinMode(flash, OUTPUT);
pinMode(camera, OUTPUT);
pinMode(valve, OUTPUT);
lcd.begin(16, 2);
Serial.begin(9600);
digitalWrite(backLightControl, backLightON);
lcd.clear();
{
lcd.setCursor(0, lcdTopRow);
lcd.print("flashSetting");
lcd.setCursor(0, lcdBottomRow);
lcd.print(flashSetting);
lcd.print(" ");
lcd.print(flashDelay);
}
lcd.setCursor(0, lcdBottomRow);
lcd.cursor();
flashDelay = EEPROM.read(0);
}
void loop()
{
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
{
oldkey = key;
if (key >=0)
{
rightkey(); //right key
}
if (key == 1) //up key
{
upkey();
}
else if (key == 2) //down key
{
downkey();
}
else if (key ==3) //left key
{
leftkey();
}
else if (key == 4) //select key
{
saveRead();
delay(500);
}
}
}
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
/*
*/
void rightkey()
{
if (key == 0) //right key
{
cursorValue++;
if (cursorValue >= 15)
{
cursorValue = 15;
}
lcd.setCursor (cursorValue,1);
//lcd.print (cursorValue);
if (cursorValue == 2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("flashDelay");
lcd.setCursor(cursorValue,1);
}
}
}
/*
*/
void leftkey()
{
cursorValue--;
if (cursorValue <= 0)
{
cursorValue = 0;
}
lcd.setCursor (cursorValue,1);
if (cursorValue == 0)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print ("flashSetting");
lcd.setCursor(cursorValue,1);
}
else if (cursorValue == 2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("flashDelay");
lcd.setCursor(cursorValue,1);
}
}
/*
*/
void upkey()
{
if (cursorValue == 2)
{
flashDelay = flashDelay + 1000;
if (flashDelay >= 10000)
{
flashDelay = 0;
}
lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 3)
{
flashDelay = flashDelay + 100;
if (flashDelay >= 1000)
{
flashDelay = 0;
}
lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 4)
{
flashDelay = flashDelay + 10;
if (flashDelay >= 100)
{
flashDelay = 0;
}
lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 5)
{
flashDelay = flashDelay + 1;
if (flashDelay >= 10)
{
flashDelay = 0;
}
lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
return;
}
/*
*/
void downkey()
{
if (cursorValue == 2) //decrement tens of drop size 1
{
flashDelay = flashDelay - 1000;
if (flashDelay <= 999)
{
flashDelay = 0000;
}
lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 3) //decrement tens of drop size 1
{
flashDelay = flashDelay - 100;
if (flashDelay <= 99)
{
flashDelay = 000;
}
lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 4) //decrement tens of drop size 1
{
flashDelay = flashDelay - 10;
if (flashDelay <= 9)
{
flashDelay = 00;
}
lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 5) //decrement tens of drop size 1
{
flashDelay = flashDelay - 1;
if (flashDelay <= 0)
{
flashDelay = 0;
}
lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
return;
}
void saveRead()
{
digitalWrite(backLightControl, backLightOFF);
digitalWrite(camera, HIGH);
delay(50);
digitalWrite(valve, HIGH);
delay(100);
digitalWrite(valve, LOW);
sensorStat = digitalRead(sensor);
if (sensorStat == HIGH)
{
delay(flashDelay); //delay to fire flash if sensor detect waterdrop
digitalWrite(flash, HIGH);
delay(100); //delay to show LED as flash indicator
digitalWrite(flash, LOW);
delay(1000);
}
digitalWrite(camera, LOW);
EEPROM.write(0, flashDelay);
digitalWrite(backLightControl, backLightON);
loop;
}