Howdy folks,
I finally got my water drop controller doing what I want it to do.
The scope of the project is to control a ssolenoid valve that will allow one, two, or
three drops of liquid to fall into a tray so that I can take a picture of the drops hitting the
surface of the tray and also photograph a drop colliding with the column of liquid thrown
up by a previous drop. The controller allows me to select a drop stream and control the
spacing between drops. It seems to do everything that I want it to do.
OOOOOOPPPPPPSSSSSS! The code is to large to load. Looks like I need to split it between a couple of posts.
Tomorrows project.
Link to You Tube for a video showing what the controller does.
RWW
#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 DropCount = 0; // default Starting point
int pulseWidth0 = 0; //Valve ON Time (Drop Size)
int pulseWidth1 = 0; //Valve ON Time (Drop Size)
int pulseWidth2 = 0; //Valve ON Time (Drop Size)
int pulseSep0 = 0; // Drop Seperation time (Time Between Drops), valve off time
int pulseSep1 = 0; // Drop Seperation time (Time Between Drops), valve off time
int reading = 0;
int VLVOFF = LOW;
int VLVON = HIGH;
int backLightON = HIGH;
int backLightOFF = LOW;
int ledpinON = HIGH;
int ledpinOFF = LOW;
int triggerON = LOW;
int triggerOFF = HIGH;
int dropsAwayON = LOW;
int dropsAwayOFF = HIGH;
int backLightControl = 10; // Output to control back light on LCD
int valveControl =11; // Output pin to turn valve ON & OFF
int ledpin = 13; // Heartbeat indicator
int timerTrigger = 2; // Output pin to start flash trigger timer
int dropsAway = 3; // Output pin to show signal to valve
int cursorValue = 0;
void setup()
{
pinMode(timerTrigger,OUTPUT); //flash timer trigger
pinMode(backLightControl,OUTPUT); //Backlight control
pinMode(valveControl,OUTPUT); //valve control
pinMode(ledpin, OUTPUT); //we'll use the debug LED to output a heartbeat
pinMode(dropsAway,OUTPUT); // indicater for valve signal
lcd.begin(16,2);
Serial.begin(9600);
//load our variables from EEPROM
DropCount = EEPROM.read(0);
pulseWidth0 = EEPROM.read(1);
pulseSep0 = EEPROM.read(2);
pulseWidth1 = EEPROM.read(3);
pulseSep1 = EEPROM.read(4);
pulseWidth2 = EEPROM.read(5);
digitalWrite(dropsAway,dropsAwayOFF); // Turn off the indicator
digitalWrite(valveControl,VLVOFF); // Valve OFF
digitalWrite(ledpin, ledpinOFF); // Turn OFF board LED
digitalWrite(backLightControl,backLightON); // Turn ON LCD Backlight
lcd.clear();
if (DropCount == 1)
{
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop Count");
lcd.setCursor(0,lcdBottomRow);
lcd.print(DropCount);
lcd.print(" ");
lcd.print(pulseWidth0);
}
else if (DropCount == 2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop Count");
lcd.setCursor(0,lcdBottomRow);
lcd.print(DropCount);
lcd.print(" ");
lcd.print(pulseWidth0);
lcd.print(" ");
lcd.print(pulseSep0);
lcd.print(" ");
lcd.print(pulseWidth1);
}
else if (DropCount == 3)
{
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop Count");
lcd.setCursor(0,lcdBottomRow);
lcd.print(DropCount);
lcd.print(" ");
lcd.print(pulseWidth0);
lcd.print(" ");
lcd.print(pulseSep0);
lcd.print(" ");
lcd.print(pulseWidth1);
lcd.print(" ");
lcd.print(pulseSep1);
lcd.print(" ");
lcd.print(pulseWidth2);
}
lcd.setCursor(0,lcdBottomRow);
lcd.cursor(); //Turn on the cursor
}
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();
}
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
{
outloop();
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 && DropCount == 3)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 1 Size");
lcd.setCursor(cursorValue,1);
}
// lcd.print(pulseWidth0);
else if (cursorValue == 5 && DropCount == 3)
{
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 1 & 2 Sep");
lcd.setCursor(cursorValue,1);
}
else if (cursorValue == 8 && DropCount == 3)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 2 Size");
lcd.setCursor(cursorValue,1);
}
else if (cursorValue == 11 && DropCount == 3)
{
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 2 & 3 Sep");
lcd.setCursor(cursorValue,1);
}
else if (cursorValue == 14 && DropCount == 3)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 3 Size");
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 2 && DropCount == 2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 1 Size");
lcd.setCursor(cursorValue,1);
}
else if (cursorValue == 5 && DropCount == 2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 1 & 2 Sep");
lcd.setCursor(cursorValue,1);
}
else if (cursorValue == 8 && DropCount == 2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 2 Size");
lcd.setCursor(cursorValue,1);
}
else if (cursorValue == 2 && DropCount == 1)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("Drop 1 Size");
lcd.setCursor(cursorValue,1);
}
}
}
/*