28BYJ-48 stops and does not start again

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!!!

I moved your topic to an appropriate forum category @mike_sevd.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

how can I get there?

@mike_sevd you are already here. This is your topic. It has just been recategorized. You don't need to take any action. Just continue to reply here as you already did when you want to add something to the discussion.

1 Like

If You would provide documentation for the code, like flow charts, it had been a lot easier figure things out. Dry reading code is too time consuming for now.

[quote="mike_sevd, post:1, topic:1269265"] ``` const int ledPin = 2; // die Nummer des LED-Pins const int greenled = 2; // die Nummer des LED-Pins ``` [/quote]

This will be trouble. Two variables, same pin.

Disregard: "ledPin" is not used.

[quote="mike_sevd, post:1, topic:1269265"] `int LDRPin = 0;` [/quote] Using Pin 0 will cause problems.

Disregard. This is using A0.

This might be setting the speed at "zero RPM" which will only release the MPU when one revolution has completed (never).

Is there a chance that buttonOben is shorting Vcc to GND?

Please provide a diagram showing all connections. You may draw one by hand and post a picture

Thank you all for your answers! I'll try to make it clearer for you soon.
for mancera1979 - The shorting event is mine back thought too. I'm not sure how this (undocumented!) "button" works. I normally put a 10K resistance to the ground but it seems not to work with this TIA1HUA thing.
for xfpd - I thought *.setSpeed(0) stops the motor until the *.setSpeed(10) comes and begins the rotation.
Please don't mind to answer before I send you some more information (to save your time). Thank you for your kindness!!

That would also help spot incorrect assumptions by OP. Good idea

Hi everybody! I'm sending you a flowchart (I needed to use some software for the first time), where I omitted the LEDs functions to make it simpler. I 've made also a picture of my experimental circuit, where the buttons are on the right, side to side. I hope that's helpful. Thank you in advance!


From Stepper.h if you "setSpeed(0)" you cause a divide by zero and the library will only make one revolution when "time/0 = 1".

/*
 * Sets the speed in revs per minute
 */
void Stepper::setSpeed(long whatSpeed)
{
  this->step_delay = 60L * 1000L * 1000L / this->number_of_steps / whatSpeed;
}

How come time/0 gives 1?

"when" = "never"

(I see I should have use ==)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.