#include <Wire.h>
#include <Keypad.h>
#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
//LCD:
LiquidCrystal_I2C lcd(0x27, 16, 2);
//KEYPAD:
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'U'},
{'4', '5', '6', 'L'},
{'7', '8', '9', 'R'},
{'T', '0', 'S', 'D'}
};
byte rowPins[ROWS] = {28, 30, 32, 34};
byte colPins[COLS] = {42, 44, 46, 48};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//TIMER:
char currentTimeValue[4];
int currentState = 1;
int timerSeconds = 0;
int lpcnt = 0;
//RELAY & LED:
#define L1 6
int RLY1 = LOW;
#define L2 7
int RLY2 = LOW;
//STEPPER_OPTIONS:
enum Option {
SERVICE,
SPEED,
TIMER,
PUMPING_OPTIONS,
SUCTION_OPTIONS
};
#define dirPin 8
#define stepPin 9
bool useSubstitutePins = false;
bool invertedService = false;
Option currentOption = SPEED;
String speedValue = " ";
int speed;
void setup(){
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("S A Z E T E B");
delay(3000);
lcd.setCursor(0,1);
lcd.print("Infusion pump");
delay(1000);
lcd.setCursor(13,1);
lcd.print(".");
delay(500);
lcd.setCursor(14,1);
lcd.print(".");
delay(500);
lcd.setCursor(15,1);
lcd.print(".");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Movement:");
for (int dir = 8; dir <= 9; dir++)
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, HIGH);
}
void loop(){
char key = keypad.getKey();
if (key != NO_KEY) {
switch (currentOption) {
case SPEED:
if (key == 'U') {
lcd.clear();
lcd.print("Speed saved");
lcd.setCursor(0, 1);
lcd.print(speedValue);
delay(2000);
currentOption = SERVICE;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Servic:");
lcd.setCursor(9, 0);
lcd.print("Pumping");
lcd.setCursor(9, 1);
lcd.print("Suction");
}
else if (key == 'D') {
if (speedValue.length() > 0) {
speedValue.remove(speedValue.length() - 1);
lcd.setCursor(speedValue.length(), 1);
lcd.print(" ");
}
}
else if (key >= '0' && key <= '9') {
if (speedValue.length() < 4) {
speedValue += key;
lcd.setCursor(speedValue.length() - 1, 1);
lcd.print(key);
}
}
break;
case SERVICE:
if (key == 'R') {
currentOption = PUMPING_OPTIONS;
showCountdown();
showEnteredTime();
displayCodeEntryScreen();
}
else if (key == 'L') {
currentOption = SUCTION_OPTIONS;
showCountdown();
showEnteredTime();
displayCodeEntryScreen();
}
break;
case PUMPING_OPTIONS:
while (1)
{
lcd.clear();
for (int dir = 8; dir <= 9; dir++)
digitalWrite(stepPin, HIGH);
delayMicroseconds(speed);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed);
delay(500);
}
break;
case SUCTION_OPTIONS:
while (1)
{
lcd.clear();
for (int dir = 8; dir <= 9; dir--)
digitalWrite(stepPin, HIGH);
delayMicroseconds(speed);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed);
delay(500);
}
break;
}
}
if (key == 'U') {
int l;
char tempVal[3];
char key = keypad.getKey();
if (int(key) != 0 and currentState == 1){
switch (key){
case 'D':
relayStatus(false);
currentTimeValue[0] = '0';
currentTimeValue[1] = '0';
currentTimeValue[2] = '0';
currentTimeValue[3] = '0';
showEnteredTime();
currentState = 1;
lpcnt = 0;
timerSeconds = 0;
break;
case 'S':
int RLY2 = HIGH;
digitalWrite(L2,RLY2);
tempVal[0] = currentTimeValue[0];
tempVal[1] = currentTimeValue[1];
tempVal[2] = 0;
timerSeconds = atol(tempVal) * 60;
tempVal[0] = currentTimeValue[2];
tempVal[1] = currentTimeValue[3];
tempVal[2] = 0;
timerSeconds = timerSeconds + atol(tempVal);
currentState = 2;
currentOption = SPEED;
currentOption = SERVICE;
break;
default:
currentTimeValue[0] = currentTimeValue[1];
currentTimeValue[1] = currentTimeValue[2];
currentTimeValue[2] = currentTimeValue[3];
currentTimeValue[3] = key;
showEnteredTime();
break;
}
}
if (currentState == 2){
if (int(key) != 0){
if(key == 'D'){
int RLY1 = HIGH;
digitalWrite(L1,RLY1);
relayStatus(false);
displayCodeEntryScreen();
currentTimeValue[0] = '0';
currentTimeValue[1] = '0';
currentTimeValue[2] = '0';
currentTimeValue[3] = '0';
showEnteredTime();
currentState = 1;
lpcnt = 0;
timerSeconds = 0;
}
}
else {
if(lpcnt > 9){
lpcnt = 0;
--timerSeconds;
showCountdown();
if (timerSeconds <= 0){
currentState = 1;
relayStatus(false);
displayCodeEntryScreen();
showEnteredTime();
}
else {
relayStatus(true);
}
}
++lpcnt;
delay(100);
}
}
}
}
//TIMER_SETTING:
void showEnteredTime(){
lcd.setCursor(5,1);
lcd.print(currentTimeValue[0]);
lcd.print(currentTimeValue[1]);
lcd.print(":");
lcd.print(currentTimeValue[2]);
lcd.print(currentTimeValue[3]);
}
void relayStatus(bool status){
if (status)
digitalWrite(stepPin, HIGH);
delayMicroseconds(speed);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed);
}
void showCountdown(){
char timest[6];
lcd.setCursor(0,0);
lcd.print(" Motor is running ");
lcd.setCursor(2,1);
lcd.print("** ");
sprintf(timest, "%d:%.2d" , (timerSeconds/60), (timerSeconds - ((timerSeconds/60)*60)));
lcd.print(timest);
lcd.print(" **");
}
void displayCodeEntryScreen(){
clearScreen();
lcd.setCursor(0,0);
lcd.print("Countdown time: ");
}
void clearScreen(){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}