I am currently working on a kinetic sculpture that is basically objects falling down on strings and being pulled up again in loops.
To reach specific locations I am using steppers to pull the objects. After some time a solenoid is used to release an object attached to a string and then it should be pulled up again by a stepper. The stepper runs constantly until a limit switch is activated. After that the loop should start all over again.
For now I got my code and hardware working for a single objects or two combined in the same loop. But I want them to work independently especially the limit switches should work for each object so they stop at the right height.
This is my code so far for two objects (end goal is the amount of around ten).
Can someone direct me in the right direction, please?
#include <AccelStepper.h>
AccelStepper StepperA(1, 26, 25);
AccelStepper StepperB(1, 13, 12);
#define limitSwitchA 4
#define limitSwitchB 5
#define solenoidA 8
#define solenoidB 9
int state = 0;
// ---------------------interval ---------------------
unsigned long Interval = 10000;
// ---------------------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(2000);
StepperA.setAcceleration(1000);
StepperA.setSpeed(2000);
StepperB.setMaxSpeed(2000);
StepperB.setAcceleration(1000);
StepperB.setSpeed(2000);
pinMode(limitSwitchA, INPUT_PULLUP);
pinMode(limitSwitchB, INPUT_PULLUP);
pinMode(solenoidA, OUTPUT);
pinMode(solenoidB, OUTPUT);
Serial.begin(115200);
}
//================================================================================
void loop()
{
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
run(); //call your function
StepperA.runSpeed();// call the functions that do the work
StepperB.runSpeed();
}
//========================================
void run() {
if (currentMillis - previousMillis >= Interval) {
while (digitalRead (limitSwitchA) == HIGH) {
Serial.println ("up");
StepperA.runSpeed ();
Serial.println ("high");
StepperA.runSpeed ();
delay(6000 * 2);
digitalWrite (solenoidA, LOW);
delay (6000 * 2);
Serial.println ("loose");
digitalWrite (solenoidA, HIGH);
Serial.println ("down");
delay(6000);
digitalWrite (solenoidA, LOW);
}
while (digitalRead (limitSwitchB) == HIGH) {
Serial.println ("up");
StepperB.runSpeed ();
Serial.println ("high");
StepperB.runSpeed ();
delay(6000 * 2);
digitalWrite (solenoidB, LOW);
delay (6000 * 2);
Serial.println ("loose");
digitalWrite (solenoidB, HIGH);
Serial.println ("down");
delay(6000);
digitalWrite (solenoidB, LOW);
}
previousMillis = currentMillis;
}
}```
(Futhermore it is part of a greater concept and which is why I am using a stepper because I want to expand the concept of my project and will probably use certain locations to move to as well according to sensor inputs.)