Hello,
im currently writing a programm which runs a specific program for a certain amount of time. So far so good.
Attached to the controller is a SHIFT register which can read a switch button state. So far so good.
If position 1 of the switch button is selected -> Programm enabled, If position 0 is selected -> programm disabled.
This all works perfectly. But now it comes to intercept a for loop IF the programm switch is disabled.
for (clock_t t = millis(); (clock_t)(millis() - t) < HOUR && programmRunning == 1; ) {
enableStepper();
setDir(HIGH);
[b][color=red]Serial.println(programmRunning); // THIS IS REQUIRED TO WORK? IF OMITTED THE BELOW WHILE NEVER GETS INTERCEPTED! LOL[/color][/b]
clock_t t0 = millis();
while (programmRunning == 1 && (clock_t)(millis() - t0) < 1 * MINUTE) {
step(delayStep);
}
delay(1000);
t0 = millis();
while (programmRunning == 1 && (clock_t)(millis() - t0) < 1 * MINUTE) {
step(delayStep);
}
}
I stumbled upon a thing which i really dont know WHY this is happening. If i run the code the Serial.println() which you see there IS required to actually intercept the while loop below. If i remove this line the interception DOES NOT work anymore for the first while loop.
For the second while loop it is perfectly working at any time.
Any ideas WHY i need to make an explicit serial println to get this working?
Full Code:
#include <Arduino.h>
#include <TimerOne.h>
#include "main.h"
// Define Connections to 74HC165
typedef unsigned long clock_t;
const clock_t SECOND = 1000L; // 1 secpmd = 1000 ms
const clock_t MINUTE = 60L * SECOND; // 1 minute = 60000 ms
const clock_t HOUR = 60L * MINUTE; // 1 hour = 3600000 ms
// --- SHIFT REGISTER PIN AND STATE
const int loadPin = 4;
const int clockEnablePin = 5;
const int dataInPin = 7;
const int clockInPin = 6;
uint8_t lastByteFetched;
// --- STEPPER PINS
const int stepPinMot1 = 10;
const int dirPinMot1 = 9;
const int enablePinMot1 = 8;
const int stepPinMot2 = 13;
const int dirPinMot2 = 12;
const int enablePinMot2 = 11;
// -- STEP DELAYS
const int defaultDelay = 3000;
int delayStep = defaultDelay;
int programmRunning = 0;
int selectedProgram = 0;
void setup() {
Serial.begin(9600);
// Setup 74HC165 connections
pinMode(loadPin, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockInPin, OUTPUT);
pinMode(dataInPin, INPUT);
pinMode(dirPinMot2, OUTPUT);
pinMode(enablePinMot2, OUTPUT);
pinMode(stepPinMot2, OUTPUT);
pinMode(dirPinMot1, OUTPUT);
pinMode(enablePinMot1, OUTPUT);
pinMode(stepPinMot1, OUTPUT);
digitalWrite(enablePinMot2, HIGH);
digitalWrite(dirPinMot2, LOW);
digitalWrite(enablePinMot1, HIGH);
digitalWrite(dirPinMot2, LOW);
delay(1000);
Timer1.initialize(1000000*1);
Timer1.attachInterrupt(refreshSwitchState);
}
void enableStepper() {
digitalWrite(enablePinMot1, LOW);
digitalWrite(enablePinMot2, LOW);
}
void disableStepper() {
digitalWrite(enablePinMot1, HIGH);
digitalWrite(enablePinMot2, HIGH);
}
void refreshSwitchState() {
// Write pulse to load pin
digitalWrite(loadPin, LOW);
delayMicroseconds(5);
digitalWrite(loadPin, HIGH);
delayMicroseconds(5);
// Get data from 74HC165
digitalWrite(clockInPin, HIGH);
digitalWrite(clockEnablePin, LOW);
uint8_t incoming = shiftIn(dataInPin, clockInPin, LSBFIRST);
digitalWrite(clockEnablePin, HIGH);
if (lastByteFetched == incoming) {
return;
}
lastByteFetched = incoming;
int enableProgramm4 = bitRead(incoming, 0);
int enableProgramm3 = bitRead(incoming, 1);
int enableProgramm2 = bitRead(incoming, 2);
int enableProgramm1 = bitRead(incoming, 3);
int speed4 = bitRead(incoming, 4);
int speed3 = bitRead(incoming, 5);
int speed2 = bitRead(incoming, 6);
int speed1 = bitRead(incoming, 7);
if (enableProgramm1 == 0 || enableProgramm2 == 0 || enableProgramm3 == 0 || enableProgramm4 == 0) {
enableStepper();
programmRunning = 1;
Serial.println(programmRunning);
} else {
disableStepper();
programmRunning = 0;
Serial.println(programmRunning);
}
delayStep = defaultDelay;
if (speed1 == 0) {
delayStep = 2600;
}
if (speed2 == 0) {
delayStep = 2200;
}
if (speed3 == 0) {
delayStep = 1800;
}
if (speed4 == 0) {
delayStep = 1400;
}
}
void step(unsigned int us) {
digitalWrite(stepPinMot1, HIGH);
digitalWrite(stepPinMot2, HIGH);
digitalWrite(stepPinMot1, LOW);
digitalWrite(stepPinMot2, LOW);
delayMicroseconds(us);
}
void setDir(uint8_t direction) {
digitalWrite(dirPinMot1, direction);
digitalWrite(dirPinMot2, direction);
}
void loop() {
if (!programmRunning) {
return;
}
Serial.println("Starting Program 1");
Serial.println(programmRunning);
for (clock_t t = millis(); (clock_t)(millis() - t) < HOUR && programmRunning == 1;) {
enableStepper();
setDir(HIGH);
clock_t t0 = millis();
while ((clock_t)(millis() - t0) < 1 * MINUTE && programmRunning == 1) {
step(delayStep);
}
setDir(LOW);
t0 = millis();
while ((clock_t)(millis() - t0) < 1 * MINUTE && programmRunning == 1) {
step(delayStep);
}
disableStepper();
t0 = millis();
while ((clock_t)(millis() - t0) < 5 * MINUTE && programmRunning == 1) { }
}
for (clock_t t0 = millis(); (clock_t)(millis() - t0) < 3 * HOUR && programmRunning == 1; ) { }
Serial.println("Finishing Program 1");
}
main.cpp (3.96 KB)