Hi y'all,
I have the task to let multiple steppers run in various modes. (One Drop to a ceratain height, a random movement, an assertation of length, and and do nothing mode)
So i thought i could write these into functions, and let them be executed by a switch case structure.
The modes would be set by 4 push buttons, which would change the mode in a neat Interrupt.
This did not work, so i wrote a serial print into the ISR (I know i shouldn't) and it alway gives me back the same numbers on startup. I pressed no pin. Nothing happens if i press them.
Serial Monitor is
1
2
0
I don't know where i got it wrong. I measured the Buttons, and read a negative 3,3V, which dropped to 0 when i pressed them.
Appreciate the help.
Board is an Arduino Due.
Code:
// define the pins for the mode buttons
const byte homingbutton = 11;
const byte randombutton = 13;
const byte dropbutton = 12;
const byte nobutton = 10;
/* define the pins for the home switch
const int homingswitch1 = 50;
const int homingswitch2 = 49;
const int homingswitch3 = 48;
const int homingswitch4 = 45;
const int homingswitch5 = 46;
*/
// set the mode to 0, a do nothing case in the loop
volatile int mode = 0;
// defines an array of five position to be driven to (in absolute mm from top), to be used in later random number choosing
int microsteps = 200;
// RandomPosition is calculated for 10mm Spool. Number of rotations is equal to distance divided by the cirumference of the spool. In our case
long RPosSteps[] ={0,1600,3200,4800,6400};
// define Drop Position 48 approx. 1500 mm
long DPosSteps = 9600;
// defining speed variables
const int globalspeed = 2000; // all speeds remain the same throuout the programm
const int steps = 200; // fullstepping = 200
void setup() {
Serial.begin(9600);
// set up of the three interrupts for the buttons
attachInterrupt(digitalPinToInterrupt(homingbutton), homingchange, LOW);
attachInterrupt(digitalPinToInterrupt(randombutton), randomchange, LOW);
attachInterrupt(digitalPinToInterrupt(dropbutton), dropchange, LOW);
attachInterrupt(digitalPinToInterrupt(nobutton), nochange, LOW);
// set up the 3 Pins as Input Pullups, pin number according to the variables
pinMode (homingbutton,INPUT_PULLUP);
pinMode (randombutton,INPUT_PULLUP);
pinMode (dropbutton,INPUT_PULLUP);
pinMode (nobutton,INPUT_PULLUP);
// set up the 5 Homing Switches
/*
pinMode (homingswitch1,INPUT_PULLUP);
pinMode (homingswitch2,INPUT_PULLUP);
pinMode (homingswitch3,INPUT_PULLUP);
pinMode (homingswitch4,INPUT_PULLUP);
pinMode (homingswitch5,INPUT_PULLUP);
*/
// give the steppers to the MultiStepper to handle
steppers.addStepper(stepper1);
steppers.addStepper(stepper2);
steppers.addStepper(stepper3);
steppers.addStepper(stepper4);
steppers.addStepper(stepper5);
}
// set up of the isr to change the buttonstatus
void nochange() {
mode = 0;
Serial.println("0");
}
void homingchange() {
Serial.println("1");
mode = 1;
}
void randomchange() {
Serial.println("2");
mode = 2;
}
void dropchange() {
Serial.println("3");
mode = 3;
}
void homing(){
stepper1.setCurrentPosition(6600);
stepper2.setCurrentPosition(6600);
stepper3.setCurrentPosition(6600);
stepper4.setCurrentPosition(6600);
stepper5.setCurrentPosition(6600);
/*
long initial_homing =-1;
while (digitalRead(homingswitch1)== LOW) { // Make the Stepper move CCW until the switch is activated
stepper1.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper1.run(); // Start moving the stepper
delay(5);
}
stepper1.setCurrentPosition(0); // Set the current position as zero for now
stepper1.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper1.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing=1;
while (digitalRead(homingswitch[motornumber] == HIGH)) { // Make the Stepper move CW until the switch is deactivated
stepper1.moveTo(initial_homing);
stepper1.run();
initial_homing++;
delay(5);
}
stepper1.setCurrentPosition(0);
*/
}
void randomise(){
long positions[5];
positions[0]= RPosSteps[random(6)];
positions[1]= RPosSteps[random(6)];
positions[2]= RPosSteps[random(6)];
positions[3]= RPosSteps[random(6)];
positions[4]= RPosSteps[random(6)];
stepper1.setMaxSpeed(globalspeed);
stepper2.setMaxSpeed(globalspeed);
stepper3.setMaxSpeed(globalspeed);
stepper4.setMaxSpeed(globalspeed);
stepper5.setMaxSpeed(globalspeed);
steppers.moveTo(positions);
steppers.runSpeedToPosition();
delay(500);
}
void drop(){
long dropposition[5];
dropposition[0] = DPosSteps;
dropposition[1] = DPosSteps;
dropposition[2] = DPosSteps;
dropposition[3] = DPosSteps;
dropposition[4] = DPosSteps;
stepper1.setMaxSpeed(globalspeed);
stepper2.setMaxSpeed(globalspeed);
stepper3.setMaxSpeed(globalspeed);
stepper4.setMaxSpeed(globalspeed);
stepper5.setMaxSpeed(globalspeed);
steppers.moveTo(dropposition);
steppers.runSpeedToPosition();
delay(5000);
}
void loop() {
// the switch case loops through the 3 subroutines as long as the button state remains equal
switch (mode) {
case 0:
break;
case 1:
homing();
break;
case 2:
randomise();
break;
case 3:
drop();
break;
default:
break;
}
}