#include <Wire.h>
#include <Keypad.h>
#include <Stepper.h>
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define Stepper Motor
AccelStepper stepper(AccelStepper::FULL4WIRE, 9, 8); // 2 and 3 are the initial stepper motor pins
const byte ROWS = 4;
const byte COLS = 4;
const char deleteKey = 'D';
const float mmPerStep = 8; // One step moves 3.15 mm
float speed = 0;
bool isFeeding = false;
bool invertedOperation = false;
bool useSubstitutePins = false;
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', 'R'},
{'7', '8', '9', 'L'},
{'T', '0', 'S', 'D'}
};
Keypad keypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
//keypad.addEventListener(keypadEvent); //add an event listener for this keypad
// keypad.setHoldTime (3000);
lcd.init(); // initialize the lcd
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("OPERATION:");
lcd.setCursor(0, 1);
lcd.print("PULSE:");
stepper.setMaxSpeed(100); // Adjust this to your desired speed
stepper.setAcceleration(50); // Adjust this to your desired acceleration
}
void loop() {
char Key = keypad.getKey();
if (Key != NO_KEY) {
if (Key == deleteKey) {
speed = 0;
lcd.setCursor(7, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(7, 1);
lcd.print("DELETE");
delay(1000);
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(0, 1);
lcd.print("PULSE:");
} else if (isdigit(Key)){
speed = speed * 10 + (Key - '0');
lcd.setCursor(7, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(7, 1);
lcd.print(speed);
} else if (Key == 'S' && !isFeeding) { // Check if not already feeding
isFeeding = true;
speedSetting();
} else if (Key == 'T' && isFeeding) { // Check if 'T' key is pressed while feeding
stepper.stop(); // Stop the motor
isFeeding = false;
lcd.setCursor(7, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(7, 1);
lcd.print("STOP");
delay(1000);
lcd.setCursor(7, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(0, 1);
lcd.print("PULSE:");
}
else if (Key == 'R'){
useSubstitutePins = !useSubstitutePins;
if (useSubstitutePins){
AccelStepper stepper(AccelStepper::FULL4WIRE, 8, 9);
}
else {
AccelStepper stepper(AccelStepper::FULL4WIRE, 9, 8);
}
stepper.setMaxSpeed(100); // Reconfigure stepper parameters
stepper.setAcceleration(50);
lcd.setCursor(11, 0);
lcd.print(" "); // Clear the display
lcd.setCursor(11, 0);
if (useSubstitutePins){
lcd.print("LEFT");
invertedOperation = true; // Set direction to inverted
}
else {
lcd.print("RIGHT");
invertedOperation = false; // Set direction to normal
}
}else if (Key == 'A') { // Start feeding with 'A' key
if (!isFeeding) {
isFeeding = true;
if (invertedOperation) {
stepper.setSpeed(-1000); // Set a negative speed for inverted direction
} else {
stepper.setSpeed(1000); // Set a positive speed for normal direction
}
}
}
}
}
void speedSetting(){
int stepsToMove = speed + mmPerStep;
if(stepsToMove > 1){
if (invertedOperation){
stepsToMove = -stepsToMove; // Invert direction
}
stepper.moveTo(stepsToMove); // Use moveTo to set target position
lcd.setCursor(7, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(7, 1);
lcd.print("RUNNING"); // Display "Feeding..." while the motor is running
while (stepper.isRunning()){
stepper.run();
if (keypad.getKey() == 'T') { // Check if 'T' key is pressed during feeding
stepper.stop(); // Stop the motor
isFeeding = false;
lcd.setCursor(7, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(7, 1);
lcd.print("STOP");
delay(1000);
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the display
lcd.setCursor(0, 1);
lcd.print("PULSE:");
return;
}
}
speed += abs(stepsToMove) * mmPerStep * (invertedOperation ? -1 : 1);
}
}
//void keypadEvent(KeypadEvent key){
//switch (keypad.getState()){
// case PRESSED:
//switch (key){
// case 'U':
//lcd.setCursor(0, 1);
//lcd.print(" ");
// lcd.setCursor(0, 0);
// lcd.print(" ");
//lcd.setCursor(0, 0);
// lcd.print("Enter Speed:");
// break;
//}
// case HOLD:
// switch (key){
// case 'L':
// lcd.setCursor(0, 0);
// lcd.print(" ");
// lcd.setCursor(0, 0);
// lcd.print("Enter Operation:");
// break;
//}
//}
//}