Tuesday, August 23, 2011

23 August 2011 controller by Arduino

ok, sometimes foosi also goes into some electronics stuff...

2years ago when Alex Teo show me some controller (for some high-speed photography) that ready made for sell in internet but the controller was too costly and we are not afford to buy it. After Alex show me the controller and how to shot the high-speed photos, i have an idea came out in my mind that we can build that controller with PLC (programmable logic controller) but a new PLC was too expensive and i have no chance to found a PLC that going to scrape in my ex company. So the idea stored into the freezer in my brain.

Until 2months ago, Alex started to looking at the expensive ready made controller again, then we started to looking around in the internet for any cheaper controller or not, i suddenly found a thing called ARDUINO.

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. The source code for the Java environment is released under the GPL and the C/C++ microcontroller libraries are under the LGPL.

more introduction about Arduino here


This LCD Shield also used in my project



Anyone who wanted to get this Arduino for your projects, can leave me a message. This Arduino are suitable for student, lecturer or any individual that wish to create some electronic project.


I have search in the Arduino forum and found some projects that almost similar to what i'm thinking to do. So I ordered one Arduino and start the controller project.

one of the similar project that i found in the forum.
my code was modified from his code. I have my code posted at bottom of my post.


Circuit drawing for the component...I'm very sure that there are some error in my drawing because i'm a electronic noob...LOL


Previous version of my project without box.

































Sensor used to sense object

Sensor used to sense object

Solenoid valve


Completed look...And some menu for the controller.







So, this controller are much more cheaper than those we saw at internet.


code that i upload into my arcuino controller.

#include
#include
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 timerSetting = 0;
int flashDelay = 0;
int valveDelay = 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 varloop = 0; //check sensor var

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.setCursor(0,lcdTopRow);
lcd.print("LexSiFrezzer1.0");
lcd.setCursor(0,lcdBottomRow);
lcd.print("bYfoosi&Alex");
delay(5000);
digitalWrite(backLightControl, backLightOFF);
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");

timerSetting = EEPROM.read(0);
flashDelay = EEPROM.read(1);
valveDelay = EEPROM.read(2);


delay(1000);
digitalWrite(backLightControl, backLightON);
lcd.clear();

if (timerSetting == 0)
{
lcd.setCursor(0, lcdTopRow);
lcd.print("settingSelection");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print("1flash 2valve");
}

else if (timerSetting == 1)
{
lcd.setCursor(0, lcdTopRow);
lcd.print("plsSetTimerFor");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print(flashDelay);
lcd.setCursor(6, lcdBottomRow);
lcd.print("flashDelay");
lcd.setCursor(0, lcdBottomRow);
}
else if (timerSetting == 2)
{
lcd.setCursor(0, lcdTopRow);
lcd.print("plsSetTimerFor");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print(valveDelay);
lcd.setCursor(6, lcdBottomRow);
lcd.print("valveDelay");
lcd.setCursor(0, lcdBottomRow);
}


lcd.setCursor(0, lcdBottomRow);
lcd.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(); //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 (timerSetting == 0) //right key
{
cursorValue++;
if (cursorValue <= 1)
{
cursorValue = 0;
}
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("UpDownToSelect");
lcd.setCursor (cursorValue,1);
}
else if (key == 0) //right key
{
cursorValue++;
if (cursorValue >= 6)
{
cursorValue = 0;
}
lcd.setCursor (cursorValue,1);
//lcd.print (cursorValue);
if (timerSetting == 1 && cursorValue == 2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("delayTimeFor");
lcd.setCursor(6,lcdBottomRow);
lcd.print("flashDelay");
lcd.setCursor(cursorValue,1);
}
if (timerSetting == 2 && cursorValue == 2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print("delayTimeFor");
lcd.setCursor(6,lcdBottomRow);
lcd.print("valveDelay");
lcd.setCursor(cursorValue,1);
}
}
}

/*
*/
void leftkey()
{
if (timerSetting == 0)
{
cursorValue--;
}
if (cursorValue <= -1)
{
cursorValue = 0;
}
else if (timerSetting == 1 || timerSetting == 2)
{
cursorValue--;
if (cursorValue <= -1)
{
cursorValue = 5;
}
}
lcd.setCursor (cursorValue,1);

if (cursorValue == 0 && timerSetting ==1)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print ("settingSelection");
lcd.setCursor(cursorValue,1);
}
else if (cursorValue == 0 && timerSetting ==2)
{
lcd.setCursor(0,lcdTopRow);
lcd.print(" ");
lcd.setCursor(0,lcdTopRow);
lcd.print ("settingSelection");
lcd.setCursor(cursorValue,1);
}
}

/*
*/
void upkey()
{
if (cursorValue == 0)
{
timerSetting = timerSetting +1;
if (timerSetting >= 3)
{
timerSetting = 0;
}

if (timerSetting == 0)
{
lcd.setCursor(0, lcdTopRow);
lcd.print(" ");
lcd.setCursor(0, lcdTopRow);
lcd.print("settingSelection");
lcd.setCursor(0, lcdBottomRow);
lcd.print(" ");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print("1flash 2valve");
lcd.setCursor(0, lcdBottomRow);
}

if (timerSetting == 1)
{
lcd.setCursor(0, lcdTopRow);
lcd.print(" ");
lcd.setCursor(0, lcdTopRow);
lcd.print("plsSetTimerFor");
lcd.setCursor(0, lcdBottomRow);
lcd.print(" ");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print(flashDelay);
lcd.setCursor(6, lcdBottomRow);
lcd.print("flashDelay");
lcd.setCursor(0, lcdBottomRow);
}

if (timerSetting == 2)
{
lcd.setCursor(0, lcdTopRow);
lcd.print(" ");
lcd.setCursor(0, lcdTopRow);
lcd.print("plsSetTimerFor");
lcd.setCursor(0, lcdBottomRow);
lcd.print(" ");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print(valveDelay);
lcd.setCursor(6, lcdBottomRow);
lcd.print("valveDelay");
lcd.setCursor(0, lcdBottomRow);
}
lcd.print(timerSetting);
lcd.setCursor(cursorValue,1);
}
////////////////////for flash delay///////////////////
if (cursorValue == 2 && timerSetting == 1)
{
flashDelay = flashDelay + 1000;

if (flashDelay >= 10000)
{
flashDelay = 0;
}

lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 3 && timerSetting == 1)
{
flashDelay = flashDelay + 100;

if (flashDelay >= 1000)
{
flashDelay = 0;
}

lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 4 && timerSetting == 1)
{
flashDelay = flashDelay + 10;

if (flashDelay >= 100)
{
flashDelay = 0;
}

lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 5 && timerSetting == 1)
{
flashDelay = flashDelay + 1;

if (flashDelay >= 9)
{
flashDelay = 0;
}

lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
////////////////////for valve delay////////////////////
if (cursorValue == 2 && timerSetting == 2)
{
valveDelay = valveDelay + 1000;

if (valveDelay >= 10000)
{
valveDelay = 0000;
}

lcd.print(valveDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 3 && timerSetting == 2)
{
valveDelay = valveDelay + 100;

if (valveDelay >= 1000)
{
valveDelay = 000;
}

lcd.print(valveDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 4 && timerSetting == 2)
{
valveDelay = valveDelay + 10;

if (valveDelay >= 100)
{
valveDelay = 00;
}

lcd.print(valveDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 5 && timerSetting == 2)
{
valveDelay = valveDelay + 1;

if (valveDelay >= 10)
{
valveDelay = 0;
}

lcd.print(valveDelay);
lcd.setCursor(cursorValue,1);
}


return;
}

/*
*/
void downkey()
{
if (cursorValue == 0)
{
timerSetting = timerSetting -1;
if (timerSetting <= -1)
{
timerSetting = 2;
}

if (timerSetting == 0)
{
lcd.setCursor(0, lcdTopRow);
lcd.print(" ");
lcd.setCursor(0, lcdTopRow);
lcd.print("settingSelection");
lcd.setCursor(0, lcdBottomRow);
lcd.print(" ");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print("1flash 2valve");
lcd.setCursor(0, lcdBottomRow);
}

if (timerSetting == 1)
{
lcd.setCursor(0, lcdTopRow);
lcd.print(" ");
lcd.setCursor(0, lcdTopRow);
lcd.print("plsSetTimerFor");
lcd.setCursor(0, lcdBottomRow);
lcd.print(" ");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print(flashDelay);
lcd.setCursor(6, lcdBottomRow);
lcd.print("flashDelay");
lcd.setCursor(0, lcdBottomRow);
}

if (timerSetting == 2)
{
lcd.setCursor(0, lcdTopRow);
lcd.print(" ");
lcd.setCursor(0, lcdTopRow);
lcd.print("plsSetTimerFor");
lcd.setCursor(0, lcdBottomRow);
lcd.print(" ");
lcd.setCursor(0, lcdBottomRow);
lcd.print(timerSetting);
lcd.print(" ");
lcd.print(valveDelay);
lcd.setCursor(6, lcdBottomRow);
lcd.print("valveDelay");
lcd.setCursor(0, lcdBottomRow);
}
lcd.print(timerSetting);
lcd.setCursor(cursorValue,1);
}

////////////////////for flash delay////////////////////
if (cursorValue == 2 && timerSetting == 1)
{
flashDelay = flashDelay -1000;

if (flashDelay < 999)
{
flashDelay = 0;
}

lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 3 && timerSetting == 1)
{
flashDelay = flashDelay - 100;

if (flashDelay < 99)
{
flashDelay = 0;
}

lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 4 && timerSetting == 1)
{
flashDelay = flashDelay - 10;

if (flashDelay < 9)
{
flashDelay = 0;
}

lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 5 && timerSetting == 1)
{
flashDelay = flashDelay - 1;

if (flashDelay < 1)
{
flashDelay = 0;
}

lcd.print(flashDelay);
lcd.setCursor(cursorValue,1);
}

////////////////////for valve delay////////////////////
if (cursorValue == 2 && timerSetting == 2)
{
valveDelay = valveDelay - 1000;

if (valveDelay <= 1999)
{
valveDelay = 1000;
}

lcd.print(valveDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 3 && timerSetting == 2)
{
valveDelay = valveDelay - 100;

if (valveDelay <= 199)
{
valveDelay = 100;
}

lcd.print(valveDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 4 && timerSetting == 2)
{
valveDelay = valveDelay - 10;

if (valveDelay <= 19)
{
valveDelay = 10;
}

lcd.print(valveDelay);
lcd.setCursor(cursorValue,1);
}
if (cursorValue == 5 && timerSetting == 2)
{
valveDelay = valveDelay - 1;

if (valveDelay <= 0)
{
valveDelay = 1;
}

lcd.print(valveDelay);
lcd.setCursor(cursorValue,1);
}

return;
}

void saveRead()
{
digitalWrite(backLightControl, backLightOFF);
openCamera();
delay(100);
valveOpen();
while (varloop != 1)
{
sensorTrigger();
}
varloop = 0;

delay(0);
digitalWrite(camera, LOW);
delay(1000);
EEPROM.write(0, timerSetting);
EEPROM.write(1, flashDelay);
EEPROM.write(2, valveDelay);
digitalWrite(backLightControl, backLightON);
loop;
}


int openCamera()
{
digitalWrite(camera, HIGH);

}

int valveOpen()
{
digitalWrite(valve, HIGH);
delay(valveDelay);
digitalWrite(valve, LOW);
}

int sensorTrigger()
{
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);
varloop = 1;
}
}








No comments:

Search foosi