#include <Wire.h>
#include <Keypad.h>
#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.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 {
OPERATION,
SPEED,
TIMER,
PUMPING_OPTIONS,
SUCTION_OPTIONS
};
#define dirPin 8
#define stepPin 9
Option currentOption = OPERATION;
volatile int stepperMaxSpeed = 400; //for setMaxSpeed
volatile int stepperSpeed = 400; //for setSpeed
volatile int stepperStepsize = 1;
//bool stepperSpeedRun = false;
String speedValue = " ";
int Speed;
void setup(){
// stepper.setSpeed(stepperSpeed);
// stepper.setMaxSpeed(stepperMaxSpeed);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(" * SAZETEB * ");
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("Select Operation");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Pumping");
delay(500);
lcd.setCursor(7,1);
lcd.print("/");
delay(500);
lcd.setCursor(8,1);
lcd.print("Suction");
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
for (int dir = 8; dir <= 9; dir++){
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, HIGH);
}
}
void loop(){
char key = keypad.getKey();
if (key != NO_KEY) {
switch (currentOption) {
case OPERATION:
if (key == 'R') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Direction saved");
lcd.setCursor(0, 1);
lcd.print("Pumping");
delay(2000);
}
else if (key == 'L'){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Direction saved");
lcd.setCursor(0, 1);
lcd.print("Suction");
delay(2000);
}
currentOption = SPEED;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Movement:");
lcd.setCursor(9, 1);
lcd.print("MAX<400");
//stepper.setSpeed(stepperSpeed);
//stepper.setMaxSpeed(stepperMaxSpeed);
char key = keypad.getKey();
if (key == 'U') {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Direction saved");
lcd.setCursor(0, 1);
lcd.print(speedValue);
delay(2000);
currentOption = TIMER;
}
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 OPERATION:
if (key == 'R'){
int RLY1 = HIGH;
digitalWrite(L1, HIGH);
currentOption = PUMPING_OPTIONS;
}
else if (key =='L'){
int RLY2 = HIGH;
digitalWrite(L2, HIGH);
currentOption = SUCTION_OPTIONS;
}
break;
case PUMPING_OPTIONS:
while (1)
{
stepperMaxSpeed = stepperMaxSpeed + stepperStepsize;
for (int dir = 8; dir <= 9; dir++)
{
digitalWrite(dir, HIGH);
delay(Speed);
digitalWrite(dir, LOW);
}
char key1 = keypad.getKey();
if (key1 == 'S')
return loop();
}
break;
case SUCTION_OPTIONS:
while (1)
{
stepperMaxSpeed = stepperMaxSpeed - stepperStepsize;
for (int dir = 8; dir >= 9; dir--)
{
digitalWrite(dir, HIGH);
delay(Speed);
digitalWrite(dir, LOW);
}
char key2 = keypad.getKey();
if (key2 == 'S')
return loop();
}
break;
case SPEED:
volatile int stepperMaxSpeed = 400; //for setMaxSpeed
volatile int stepperSpeed = 400; //for setSpeed
volatile int stepperStepsize = 1;
bool stepperMaxSpeed_Selected = false; //for setMaxSpeed
bool stepperSpeed_Selected = false; //for setSpeed
//bool stepperSpeedRun = false;
String speedValue = " ";
int speed;
currentOption = PUMPING_OPTIONS;
currentOption = SUCTION_OPTIONS;
break;
case TIMER:
break;
//stepperMaxSpeed = stepperMaxSpeed + stepperStepsize;
//stepperMaxSpeed = stepperMaxSpeed - stepperStepsize;
}
}
}