const int pulsePin = 9; // Connect to PUL on the drive
const int dirPin = 8; // Connect to DIR on the drive
const int startPin = 7; // Connect to Start button
const int stopPin = 6; // Connect to Stop button
const int ledButtonPin = 4; // Connect to button for LED on/off (pneumatic valve control)
const int ledPin = 5; // Connect to LED for motor status indication
const int potPin = A0; // Connect to the potentiometer
// Motor control variables
float pulseFrequency = 1000.0; // Frequency in Hz (adjust for desired speed)
const float minFrequency = 300.0; // Minimum frequency
const float maxFrequency = 1500.0; // Maximum frequency
// Debounce variables
unsigned long lastDebounceTimeStart = 0; // Last time the start button state changed
unsigned long lastDebounceTimeLED = 0; // Last time the LED button state changed
unsigned long debounceDelay = 50; // Debounce time in milliseconds
bool lastStartButtonState = HIGH; // Previous state of the start button
bool lastLEDButtonState = HIGH; // Previous state of the LED button
bool motorRunning = false; // Track if the motor is currently running
bool ledState = false; // Track the LED state (pneumatic valve state)
void setup() {
pinMode(pulsePin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(startPin, INPUT_PULLUP); // Active low (button pressed = LOW)
pinMode(stopPin, INPUT_PULLUP); // Active low (button pressed = LOW)
pinMode(ledButtonPin, INPUT_PULLUP); // Button for controlling the LED/pneumatic valve
pinMode(ledPin, OUTPUT); // LED pin
pinMode(potPin, INPUT); // Potentiometer input
Serial.begin(9600);
}
void loop() {
int readingStart = digitalRead(startPin);
int readingStop = digitalRead(stopPin);
int readingLED = digitalRead(ledButtonPin); // Read the LED button state
// Read the potentiometer value and map it to the frequency range
int potValue = analogRead(potPin);
pulseFrequency = map(potValue, 0, 1023, minFrequency, maxFrequency);
// Serial.println(pulseFrequency);
// Debounce the start button
if (readingStart != lastStartButtonState) {
lastDebounceTimeStart = millis(); // Reset the debounce timer
}
// Check if the start button state has stabilized
if ((millis() - lastDebounceTimeStart) > debounceDelay) {
if (readingStart == LOW && !motorRunning && ledState) {
// Start button pressed, motor is not running, and valve is closed
motorRunning = true;
Serial.println("Motor started. Valve is closed.");
}
}
// Check the stop button to stop the motor
if (readingStop == LOW && motorRunning) {
// Stop button pressed and motor is running, stop the motor
motorRunning = false;
Serial.println("Motor stopped. Valve is now open.");
}
// Check LED button to control the pneumatic valve
if (readingLED != lastLEDButtonState ) {
lastDebounceTimeLED = millis(); // Reset the debounce timer for LED button
}
// Check if the LED button state has stabilized
if ((millis() - lastDebounceTimeLED) > debounceDelay && motorRunning==0) {
if (readingLED == LOW) { // LED button pressed
if (motorRunning) {
// If the motor is running, ignore the LED button press (valve cannot be opened)
Serial.println("Cannot open valve while motor is running.");
} else {
// If the motor is stopped, toggle the valve
ledState = !ledState; // Toggle the LED state
digitalWrite(ledPin, ledState ? HIGH : LOW); // Set the LED (close or open the valve)
Serial.println(ledState ? "Valve closed." : "Valve opened.");
// Wait for button release to prevent multiple toggles
while (digitalRead(ledButtonPin) == LOW) {}
}
}
}
// If the motor is running, send pulses continuously
if (motorRunning) {
sendContinuousPulses(pulseFrequency);
}
// Update last button states
lastStartButtonState = readingStart;
lastLEDButtonState = readingLED;
}
void sendContinuousPulses(float frequency) {
unsigned long delayMicrosecondsx = (1.0 / frequency) * 1e6 / 2; // Convert frequency to microseconds for delay
// Generate continuous pulses
digitalWrite(pulsePin, HIGH);
delayMicroseconds(delayMicrosecondsx);
digitalWrite(pulsePin, LOW);
delayMicroseconds(delayMicrosecondsx);
}