The purpose of the program is to rotate the stepper motor for a specific LDR measurement value if the button (TIA1HUA) is not pressed. Similarly, it will have reverse rotation for the remaining LDR measurement values with the limit of another button press. What happens is: that the rotation is done as intended, but when the button is pressed the rotation stops and does not start again. I am surprised that the text (that supposed to be printed from line in Bold form) is not even printed before the command that stops the motor (see the serial monitor output below).
#include <Stepper.h>
// Konstanten werden sich nicht ändern. Sie werden hier zum Festlegen von Pin-Nummern verwendet:
const int buttonOben = 7;
const int buttonUnten = 6;
const int ledPin = 2; // die Nummer des LED-Pins
const int greenled = 2; // die Nummer des LED-Pins
const int blueled = 3;
int LDRPin = 0;
int LDRLesungAnfang;
int LDRLesung, sensorMax = 0, sensorMin = 1000;
// Definiert die Anzahl der Schritte pro Umdrehung
const int stepsPerRevolution = 2038;
// Pins in der Reihenfolge IN1-IN3-IN2-IN4 für die richtige Schrittfolge eingegeben
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
// Variablen ändern sich:
int buttonState6 = 0; // Variable zum Auslesen des Tasterzustands
int buttonState7 = 0;
bool flag = false, LED_blue_on_off = false, LED_green_on_off = false;
int i = 0;
int Licht = 0;
void setup() {
Serial.begin(9600);
// den LED-Pin als Ausgang initialisieren:
pinMode(ledPin, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(blueled, OUTPUT);
// Initialisierung des Tasterpins als Eingang:
pinMode(buttonOben, INPUT);
pinMode(buttonUnten, INPUT);
// calibrate during the first five seconds
while (millis() < 5000) {
LDRLesung = analogRead(LDRPin);
// record the maximum sensor value
LDRLesung = constrain(LDRLesung, 100, 1000);
if (LDRLesung > sensorMax) { sensorMax = LDRLesung; }
// record the minimum sensor value
if (LDRLesung < sensorMin) { sensorMin = LDRLesung; }
}
LDRLesungAnfang = int((sensorMax + sensorMin) / 2);
Serial.print(" ");
Serial.print("sensorMax = ");
Serial.println(sensorMax);
Serial.print("sensorMin = ");
Serial.println(sensorMin);
Serial.print("average = ");
Serial.println(LDRLesungAnfang);
delay(5000);
}
void loop() {
delay(500);
LDRLesung = analogRead(LDRPin);
Serial.print("Analog Lesung = ");
Serial.println(LDRLesung);
while (LDRLesung > LDRLesungAnfang) {
myStepper.setSpeed(10);
myStepper.step(stepsPerRevolution);
buttonState7 = digitalRead(buttonOben);
if (buttonState7 == LOW) {
Serial.println("Button up pressed!! ");
myStepper.setSpeed(0);
}
LDRLesung = analogRead(LDRPin);
Serial.print("Going up = ");
Serial.println(LDRLesung);
if (LED_blue_on_off == false) {
digitalWrite(blueled, HIGH);
LED_blue_on_off = true;
}
}
digitalWrite(blueled, LOW);
LED_blue_on_off = false;
while (LDRLesung < LDRLesungAnfang) {
myStepper.setSpeed(10);
myStepper.step(-stepsPerRevolution);
buttonState6 = digitalRead(buttonUnten);
if (buttonState6 == LOW) {
myStepper.setSpeed(0);
Serial.println("## Button down pressed!! ");
}
LDRLesung = analogRead(LDRPin);
Serial.print("Going down = ");
Serial.println(LDRLesung);
if (LED_green_on_off == false) {
digitalWrite(greenled, HIGH);
LED_green_on_off = true;
}
}
digitalWrite(greenled, LOW);
LED_green_on_off = false;
}
This is what I'm getting from the serial monitor:
sensorMax = 460
19:08:20.463 -> sensorMin = 450
19:08:20.497 -> average = 455
19:08:25.969 -> Analog Lesung = 459
19:08:31.957 -> Going up = 441
19:08:37.984 -> Going down = 450
19:08:43.978 -> Going down = 439
19:08:49.973 -> Going down = 464
19:08:50.482 -> Analog Lesung = 462
19:08:56.489 -> Going up = 463
19:09:02.501 -> Going up = 465
19:09:08.501 -> Bu
Thank you very much for your help!!!

