Hi everyone. Long time lurker, first time poster. Please be gentle.
I make cider and have been using Arduino to automate. I have built a labelling machine that will apply both a main label and a neck label to bottles, using three stepper motors. One motor spins the bottle, the second turns the main label roll and the third turns the neck label roll.
The hardware works. I'm using a CNC shield to control the motors. The software also works - mostly. I based the code off of Robin2's examples, using millis() to control several motors at once, each running at different speeds.
The bug I can't understand is the effect that one specific Serial.println() is having. If I leave that line in, the code works. if I delete that one line (because I want the motors to run faster by eliminating the time taken up by the printing to the serial monitor), then the code stops working. Instead of running for the alloted time defined by the nested if functions, the motors each move one step and stop.
I am baffled. I could just leave that Serial.println() in - all that happens is that the machine runs a bit slower than I would like - but I cannot get my head around what is happening here and would like to tap the hive mind to see if I can get an explanation. Here is the code. The offending Serial.println is identified with the comment *** THIS IS THE LINE ***
byte directionPinBottleTurner = 5;
byte directionPinMainLabel = 6;
byte directionPinNeckLabel = 7;
byte stepPinBottleTurner = 2;
byte stepPinMainLabel = 3;
byte stepPinNeckLabel = 4;
byte buttonpin = A5;
boolean buttonpressed = false;
int stepperCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCounter = 1;
unsigned long curMillis;
unsigned long prevStepMillisBottleTurner = 0;
unsigned long prevStepMillisMainLabel = 0;
unsigned long prevStepMillisNeckLabel = 0;
unsigned long millisBetweenStepsBottleTurner = 1; // milliseconds
unsigned long millisBetweenStepsMainLabel = 1; // milliseconds
unsigned long millisBetweenStepsNeckLabel = 4; // milliseconds
void setup() {
Serial.begin(9600);
pinMode(directionPinBottleTurner, OUTPUT);
pinMode(directionPinMainLabel, OUTPUT);
pinMode(directionPinNeckLabel, OUTPUT);
pinMode(stepPinBottleTurner, OUTPUT);
pinMode(stepPinMainLabel, OUTPUT);
pinMode(stepPinNeckLabel, OUTPUT);
pinMode(buttonpin, INPUT_PULLUP);
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void readButtons()
{
buttonpressed = false;
buttonState = digitalRead(buttonpin);
if (buttonState != lastButtonState)
{
// if the state has changed, increment the counter
if (buttonState == LOW)
{
buttonPushCounter++;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
Serial.println(buttonPushCounter); // *** THIS IS THE LINE ***
if (buttonPushCounter == 2)
{
buttonpressed = true;
}
}
void actOnButtons() {
if (buttonpressed == true)
{
if (stepperCounter = 1199)
{
buttonPushCounter = 0;
}
digitalWrite(directionPinBottleTurner, LOW);
digitalWrite(directionPinMainLabel, HIGH);
digitalWrite(directionPinNeckLabel, HIGH);
for (stepperCounter = 0; stepperCounter < 1200; stepperCounter++)
{
curMillis = millis();
Serial.println(stepperCounter);
if (stepperCounter == 0)
{
delay(500);
}
if (stepperCounter < 1200) // Bottle turner subroutine
{
if (curMillis - prevStepMillisBottleTurner > millisBetweenStepsBottleTurner)
{
prevStepMillisBottleTurner = curMillis;
digitalWrite(stepPinBottleTurner, HIGH);
digitalWrite(stepPinBottleTurner, LOW);
}
}
if (stepperCounter > 263) // Main label subroutine
{
if (stepperCounter < 1050) // Will want the Main labeller to stop 1/4 rotation after the neck labeller
{
if (curMillis - prevStepMillisMainLabel > millisBetweenStepsMainLabel)
{
prevStepMillisMainLabel = curMillis;
digitalWrite(stepPinMainLabel, HIGH);
digitalWrite(stepPinMainLabel, LOW);
}
}
}
if (stepperCounter < 650) //Neck label subroutine
{
if (curMillis - prevStepMillisNeckLabel > millisBetweenStepsNeckLabel)
{
prevStepMillisNeckLabel = curMillis;
digitalWrite(stepPinNeckLabel, HIGH);
digitalWrite(stepPinNeckLabel, LOW);
}
}
}
}
}