Hey guys, I'm having a problem with my switch cases, whereby certain functions within the switch case take upwards of 2 seconds to react/execute.
For example, in case 1 and case 2, DETECTED == HIGH is when there is no object infront of the laser receiver, and DETECTED == LOW is when there is an object infront of the laser receiver. This will then trigger another function. Unfortunately, it takes upwards of 2 seconds for DETECTED == HIGH to change to LOW when I block the laser module, whilst either case 1 or case 2 are triggered (via serial monitor).
Currently my serial monitor is set to "No Line Ending".
My question is, is there a way to loop through these switch cases faster?
#include <AccelStepper.h>
const int stepsPerRevolution = 200;
AccelStepper ballHopperStepper = AccelStepper (2, 3, 4);
AccelStepper oscillationStepper = AccelStepper (5, 6, 7);
AccelStepper angleStepper = AccelStepper (8, 9, 10);
#define DETECT 28 // pin 28 for sensor
#define pwmPin1 12
#define pwmPin2 13
#define controlPin1 A0
#define controlPin2 A1
#define stepsPerRevolution 200
int val = 0;
int state = 0;
int start = 0;
int stop1 = 49;
int count = 0;
unsigned long previousMillis = 0;
const long interval = 1;
void setup() {
pinMode(DETECT, INPUT);
ballHopperStepper.setMaxSpeed(2000);
ballHopperStepper.setAcceleration(2000);
oscillationStepper.setMaxSpeed(1000);
oscillationStepper.setAcceleration(500);
angleStepper.setMaxSpeed(1000);
angleStepper.setAcceleration(500);
Serial.begin(9600);
}
bool myFlag = false;
void loop() {
unsigned long currentMillis = millis();
if (Serial.available() > 0) {
state = Serial.read() - 48;
if (state == 3) myFlag = true;
}
int detected = digitalRead(DETECT);
//Serial.println(ballHopperStepper.currentPosition());
if ((state < 0) || (state > 9)) {
state = 0; //return to idle
} else {
switch (state) { // Beginner Start Button Pressed
case 0:
Serial.println("Waiting for user to select mode");
break;
case 1:
spinMainMotors();
rotate25Beginner();
if (currentMillis - previousMillis >= interval && detected == LOW) {
previousMillis = currentMillis;
rotate90Beginner();
} else {
Serial.println("Waiting for ball in play1");
}
break;
case 2: // Intermediate Start Button Pressed
spinMainMotors();
rotate25Advanced();
if (detected == LOW) {
rotate90Advanced();
} else {
Serial.println("Waiting for ball in play2");
}
case 3: // Shoot One Ball
if (myFlag == true) {
spinMainMotors();
rotate90Advanced();
myFlag = false;
}
break;
case 4:
Serial.println("Ball detected!");
ballHopperStepper.moveTo(ballHopperStepper.currentPosition() + 400);
ballHopperStepper.runToPosition();
myFlag = false;
break;
}
}
}
void rotate90Beginner() {
Serial.println("Ball detected!");
//delay(1000);
ballHopperStepper.moveTo(ballHopperStepper.currentPosition() + 400);
ballHopperStepper.run();
//delay(1000);
}
void rotate90Advanced() {
Serial.println("Ball detected!");
ballHopperStepper.moveTo(ballHopperStepper.currentPosition() + 400);
ballHopperStepper.runToPosition();
}
void rotate25Beginner() {
Serial.println("Rotating");
oscillationStepper.moveTo(ballHopperStepper.currentPosition() - 111);
oscillationStepper.run();
oscillationStepper.moveTo(ballHopperStepper.currentPosition() + 111);
oscillationStepper.run();
}
void rotate25Advanced() {
Serial.println("Rotating");
oscillationStepper.moveTo(ballHopperStepper.currentPosition() - 111);
oscillationStepper.runToPosition();
oscillationStepper.moveTo(ballHopperStepper.currentPosition() + 111);
oscillationStepper.runToPosition();
}
void spinMainMotors() {
i