Hey,
so for now I got it working. I had to get rid of the delay, since it stopped all actions involved.
I am working now with this code:
enum { On = HIGH, Off = LOW };
#undef MyHW
#ifdef MyHW
# define limitSwitchA A1
# define limitSwitchB A2
# define solenoidA 13
# define solenoidB 12
class AccelStepper {
byte pin;
public:
AccelStepper (int a, int b, int p) {
pin = p;
pinMode (pin, OUTPUT);
digitalWrite (pin, Off);
};
void setAcceleration (int x) { };
void setMaxSpeed (int x) { };
void setSpeed (int x) { };
void runSpeed (void) {
digitalWrite (pin, On);
};
void stop (void) {
digitalWrite (pin, Off);
};
};
AccelStepper StepperA(1, 26, 10);
AccelStepper StepperB(1, 13, 11);
#else
# include <AccelStepper.h>
# define limitSwitchA 5
# define limitSwitchB 4
# define solenoidA 10
# define solenoidB 11
AccelStepper StepperA(1, 13, 12);
AccelStepper StepperB(1, 9, 8);
#endif
int state = 0;
// ---------------------interval ---------------------
unsigned long Interval = 15000;
// ---------------------millis ---------------------
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousMillis = 0; // will store last time the steppers were updated
//==============================================================================
void setup()
{
// Set all default speeds and accelerations
StepperA.setMaxSpeed(12000);
StepperA.setAcceleration(10000);
StepperA.setSpeed(12000);
StepperB.setMaxSpeed(12000);
StepperB.setAcceleration(10000);
StepperB.setSpeed(12000);
pinMode(limitSwitchA, INPUT_PULLUP);
pinMode(limitSwitchB, INPUT_PULLUP);
pinMode(solenoidA, OUTPUT);
pinMode(solenoidB, OUTPUT);
digitalWrite(solenoidA, Off);
digitalWrite(solenoidB, Off);
Serial.begin(115200);
}
// -----------------------------------------------------------------------------
void run (
AccelStepper stepper,
byte pinLimitSwitch )
{
if (digitalRead (pinLimitSwitch) == On) {
stepper.runSpeed ();
} else
stepper.stop ();
}
//==============================================================================
void
drop (
byte pinSolenoid,
byte pinLimitSwitch
)
{
Serial.println("drop");
if (digitalRead (pinLimitSwitch) == Off) {
digitalWrite (pinSolenoid, On);
} else if (digitalRead(pinLimitSwitch) == On) {
digitalWrite(pinSolenoid, Off);
}
}
//==============================================================================
void loop()
{
currentMillis = millis(); // capture the latest value of millis()
if (currentMillis - previousMillis >= Interval) {
drop (solenoidA, limitSwitchA);
drop (solenoidB, limitSwitchB);
dropStart (solenoidA, limitSwitchA);
dropStart (solenoidB, limitSwitchB);
previousMillis = currentMillis;
}
run (StepperA, limitSwitchA);
run (StepperB, limitSwitchB);
}
But I am wondering now how to use different intervals, so the time the solenoid is on for the drop and the time it waits until it is on can be controlled better. I thought about using something like the blink without delay which is triggered through a boolean and tried something, but I guess the time interval in the void loop() stops it from being executed? Here is my tried code
void drop
(
byte pinSolenoid,
byte pinLimitSwitch
)
{
Serial.println("drop");
const unsigned long OnTime = 10000; // milliseconds of on-time
const unsigned long OffTime = 5000; // milliseconds of off-time
static int solenoidState = Off;
static unsigned long duration = OnTime;
static unsigned long lastTime;
if ( millis() - lastTime < duration ) {
return;
}
lastTime = millis();
if (pinLimitSwitch == On && solenoidState == Off) {
solenoidState = On; // Turn it off
duration = OffTime;
}
else {
solenoidState = Off; //turn it on
duration = OnTime;
}
digitalWrite(pinSolenoid, solenoidState);
}