#include <Wire.h>
#include <stdio.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;
byte rowPins[ROWS] = {28, 30, 32, 34};
byte colPins[COLS] = {42, 44, 46, 48};
char keys[ROWS][COLS] = {
{'1', '2', '3', 'U'},
{'4', '5', '6', 'L'},
{'7', '8', '9', 'R'},
{'T', '0', 'S', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//TIMER:
int counter = 0;
int attempts = 0;
int max_attempts = 3;
String mymints;
int minutes = 0;
String mysecs;
int seconds = 0;
long int total_seconds = 0;
int secflag = 0;
int timer_started_flag = 0;
unsigned long previousMillis=0;
unsigned long int previoussecs = 0;
unsigned long int currentsecs = 0;
unsigned long currentMillis = 0;
int interval= 1 ;
int tsecs = 0;
//RELAY & LED:
#define L1 10
int RLY1 = LOW;
#define L2 11
int RLY2 = LOW;
//STEPPER MOTOR:
enum state {
SPEED,
OPERATION,
SUCTION,
PUMPING,
TIMER
};
state currentState = SPEED;
String speedValue = " ";
int Speed;
int dirPin = 8;
int stepPin = 9;
const int stepsPerRevolution = 800;
Stepper myStepper(stepsPerRevolution, 8, 9);
volatile int stepperMaxSpeed = 400;
volatile int stepperSpeed = 400;
volatile int stepperStepsize = 1;
bool stepperMaxSpeed_Selected = false;
bool stepperSpeed_Selected = false;
void setup(){
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(" ** SAZETEB ** ");
delay(2000);
lcd.setCursor(0,1);
lcd.print(">");
lcd.setCursor(15,1);
lcd.print("<");
delay(100);
lcd.setCursor(1,1);
lcd.print(">");
lcd.setCursor(14,1);
lcd.print("<");
delay(500);
lcd.setCursor(2,1);
lcd.print("INFUSIONPUMP");
delay(3000);
lcd.setCursor(0,1);
String text = " ";
lcd.print(text);
for (int x = 0; x <= 14; x++){
lcd.scrollDisplayRight();
delay(100);
}
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ENTER SPEED:");
lcd.setCursor(9, 1);
lcd.print("MAX<400");
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 (currentState) {
case SPEED:
if (key == 'U') {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" (SPEED SAVED): ");
lcd.setCursor(12,1);
lcd.print("rpm");
lcd.setCursor(5, 1);
lcd.print(speedValue);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SELECT:");
delay(500);
lcd.setCursor(8, 0);
lcd.print("<");
delay(500);
lcd.setCursor(9, 0);
lcd.print("SUCTION");
delay(500);
lcd.setCursor(8, 1);
lcd.print(">");
delay(500);
lcd.setCursor(9, 1);
lcd.print("PUMPING");
currentState = OPERATION;
}
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 == 'L'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" (OPERATION): ");
lcd.setCursor(0, 1);
lcd.print("SUCTION");
delay(500);
lcd.setCursor(10, 1);
lcd.print("<");
delay(100);
lcd.setCursor(9, 1);
lcd.print("<");
delay(100);
lcd.setCursor(8, 1);
lcd.print("<");
while (1){
char key = keypad.getKey();
if (key != NO_KEY){
if (key == 'S'){
suction();
}
}
}
}
else if (key =='R'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" (OPERATION): ");
lcd.setCursor(0, 1);
lcd.print("PUMPING");
delay(500);
lcd.setCursor(8, 1);
lcd.print(">");
delay(100);
lcd.setCursor(9, 1);
lcd.print(">");
delay(100);
lcd.setCursor(10, 1);
lcd.print(">");
while(1){
char key = keypad.getKey();
if (key != NO_KEY){
if (key == 'S'){
pumping();
}
}
}
}
break;
}
}
}
void suction() {
int Speed = 0;
Speed = speedValue.toInt();
myStepper.setSpeed(Speed);
unsigned long startTime = millis(); // Get the current time
unsigned long maxDuration = 10000; // 10 seconds
while ((millis() - startTime) < maxDuration) {
myStepper.step(stepsPerRevolution);
}
}
void pumping(){
int Speed = 0;
Speed = speedValue.toInt();
myStepper.setSpeed(Speed);
unsigned long startTime = millis(); // Get the current time
unsigned long maxDuration = 10000; // 10 seconds
while ((millis() - startTime) < maxDuration){
myStepper.step(-stepsPerRevolution);
}
}
/*void suction(){
Speed = " ";
Speed = speedValue.toInt();
myStepper.setSpeed(Speed);
for (int x = 0; stepsPerRevolution <= 800; x++ ){
myStepper.step(stepsPerRevolution);
}
}
void suction3() {
int Speed = 0;
Speed = speedValue.toInt();
myStepper.setSpeed(Speed);
int maxSteps = 800;
int stepCount = 0;
while (stepCount < maxSteps) {
myStepper.step(stepsPerRevolution);
stepCount++;
}
}*/