#define SOLONOID_DELAY 10 //solenoid on/off delay in miliseconds (50Hz)
#define STEP_DELAY 1 //stepper motor speed value
#define solenoidPin 9 // Define the pin to which the solenoid is connected
#define sensorPin 2 // Define the pin to which the proximity sensor is connected
int sensorValue; // Variable to hold the sensor reading
// Motor pin definitions
#define stepPin 5
#define dirPin 6
// Button pin definition
#define startButtonPin 4
int stepDelay = 0;
long soloLastTime = 0;
long stepLastTime = 0;
void setup() {
pinMode(solenoidPin, OUTPUT); // Set the solenoid pin as an output
pinMode(sensorPin, INPUT_PULLUP); // Set the sensor pin as an input
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(startButtonPin, INPUT_PULLUP);
// Move the motor in one direction continuously
digitalWrite(dirPin, LOW);
soloLastTime = millis();
stepLastTime = millis();
}
void loop() {
// Wait for the start button to be pressed
while (digitalRead(startButtonPin) == HIGH) {
delay(10);
}
while (digitalRead(startButtonPin) == LOW) {
sensorValue = digitalRead(sensorPin); // Read the sensor value
while (sensorValue == LOW) { // If object is detected, wait for 2 seconds
delay(2000);
sensorValue = digitalRead(sensorPin); // Read the sensor value again
}
while (digitalRead(sensorPin) == HIGH && digitalRead(startButtonPin) == LOW) {
if (millis() - soloLastTime >= SOLONOID_DELAY){
digitalWrite(solenoidPin, !digitalRead(solenoidPin));
soloLastTime = millis();
}
if (millis() - stepLastTime > STEP_DELAY){
digitalWrite(stepPin, !digitalRead(stepPin));
stepLastTime = millis();
}
}
digitalWrite(stepPin, LOW);
digitalWrite(solenoidPin, LOW);
}
// Stop the motor when the start button is released
digitalWrite(stepPin, LOW);
digitalWrite(solenoidPin, LOW);
//currentSpeed = 0;
}