#include <Wire.h> // Include Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include LCD library
#include <AccelStepper.h>
// LCD setup: Specify I2C address and dimensions
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Pressure transducer setup
const int pressureInputPin = A0; // Analog input pin that the sensor is attached to
const float voltageMin = 0.5; // Voltage corresponding to minimum pressure (0 psi)
const float voltageMax = 4.5; // Voltage corresponding to maximum pressure (100 psi)
const float pressureMin = 0; // Minimum pressure value
const float pressureMax = 200; // Maximum pressure value
float pressure = 0; // Pressure value
float adcValue = 200; // Maximum pressure value
float target = 180; // Pressure Target - (The pressure at which the motor will turn)
long interval = 250; // Interval to check sensor and move motor
bool isMotorMoving = false;
//#define motorE A1
//#define motorS 9
//#define motorD A3
AccelStepper stepper1(1, 9, A3); // (Typeof driver: with 2 pins, STEP, DIR)
unsigned long previousMillis = 0; //Used for the Millisdelay
// Function to map a floating point number from one range to another
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
pinMode(pressureInputPin, INPUT); // Set the pressure sensor pin as input
pinMode(LED_BUILTIN, OUTPUT); // Set Built in LED
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
// pinMode(motorD, OUTPUT);
// pinMode(motorS, OUTPUT);
// pinMode(motorE, OUTPUT);
stepper1.setMaxSpeed(2000); // Set maximum speed value for the stepper
stepper1.setAcceleration(500); // Set acceleration value for the stepper
stepper1.setCurrentPosition(0); // Set the current position to 0 steps
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
void loop() {
int adcValue = analogRead(pressureInputPin); // Read the analog value from sensor
float voltage = adcValue * (5.0 / 1023.0); // Convert ADC reading to voltage
pressure = mapfloat(voltage, voltageMin, voltageMax, pressureMin, pressureMax) - 4.5; // Map voltage to pressure
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
lcd.setCursor(0, 0);
lcd.print("Pressure: ");
lcd.print(pressure, 1);
lcd.print(" psi");
lcd.setCursor(0, 1);
lcd.print("ADC Value: ");
lcd.print(adcValue);
lcd.setCursor(0, 3);
lcd.print("Target Value: ");
lcd.print(target);
}
if (pressure > target && !isMotorMoving) {
// Start moving the motor if pressure is above target and motor is not already moving
stepper1.moveTo(10000); // Set a long target to ensure continuous movement
isMotorMoving = true;
} else if (pressure <= target && isMotorMoving) {
// If pressure drops below target and motor is moving, initiate stop
stepper1.stop(); // This will start deceleration
isMotorMoving = false;
}
if (isMotorMoving) {
// This ensures the stepper is continuously moving towards the target or stopping smoothly
stepper1.run();
} else {
// Optionally, you can add code here to handle the motor once it has stopped, if needed
digitalWrite(LED_BUILTIN, LOW);
}
}
This A4988 Driver is representative of the Driver inteneded to be used, which is a CL57T.
Link is as follows: https://www.omc-stepperonline.com/closed-loop-stepper-driver-0-8-0a-24-48vdc-for-nema-17-23-24-stepper-motor-cl57t
This Potentiometer is to represent
the pressure sensor. It can be roatated
to increase pressure.
The pressure sensor uses is a 0-200psi
sensor where 0 psi is 0.5v and 200 psi
is 4.5v.