L298n Motor Shield and Stepper Motor

Hello,

I have a problem with my L298n Motor Driver and my 12V Stepper Motor.
When I upload my Program on the Arduino the Stepper Motor works one Time how it should be and after that the Motor Activates himself every 2 seconds and moves a little bit.
What is the Problem?


My Code:

#include <Stepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

int Button_Rechts = 2;
int Button_Links = 4;
int Button_Auswahl = 3;
int menu = 1;
int tasterstatus1;
int tasterstatus2;
int tasterstatus3;

const int stepsPerRevolution = 1000;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
pinMode(Button_Rechts, INPUT);
pinMode(Button_Links, INPUT);
pinMode(Button_Auswahl, INPUT);
updateMenu();

myStepper.setSpeed(40);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
tasterstatus1=digitalRead(Button_Rechts);
tasterstatus2=digitalRead(Button_Links);
tasterstatus3=digitalRead(Button_Auswahl);

if (tasterstatus1 == LOW)
{
menu++;
updateMenu();
delay(200);
// while (!digitalRead(Button_Rechts));
}
if (tasterstatus2 == LOW)
{
menu--;
updateMenu();
delay(200);
//while(!digitalRead(Button_Auswahl));
}
if (tasterstatus3 == LOW)
{
executeAction();
updateMenu();

}
}

void updateMenu() {
switch(menu) {
case 0:
menu = 1;
break;
case 1:
lcd.clear();
lcd.print(" Getraenke Auswahl!");
lcd.setCursor(0, 2);
lcd.print(" Cola Korn");
lcd.setCursor(0, 3);
lcd.print("<--- --->");
break;
case 2:
lcd.clear();
lcd.print(" Getraenke Auswahl!");
lcd.setCursor(0, 2);
lcd.print(" Sprite Korn");
lcd.setCursor(0, 3);
lcd.print("<--- --->");
break;
case 3:
lcd.clear();
lcd.print(" Getraenke Auswahl!");
lcd.setCursor(0, 2);
lcd.print(" Spezi Korn");
lcd.setCursor(0, 3);
lcd.print("<--- --->");
break;
case 4:
lcd.clear();
lcd.print(" Getraenke Auswahl!");
lcd.setCursor(0, 2);
lcd.print(" Captain Cola");
lcd.setCursor(0, 3);
lcd.print("<--- --->");
break;
case 5:
menu = 4;
break;
}}

void executeAction() {
switch (menu) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
}}

void action1() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" Cola Korn wird gemacht: ");
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(2000);
}
void action2() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" Sprite Korn wird gemacht: ");
Serial.println("clockwise");
myStepper.step(-stepsPerRevolution);

}
void action3() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" Spezi Korn wird gemacht: ");
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);

// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);

}
void action4() {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" Captain wird gemacht: ");
delay(5000);
}

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

Some of your actions don't have delays and could happen multiple times in quick succession.

Rather than use delays, you should be detecting the button press event to trigger actions,
just like in the StateChangeDetection example.

Scattering large delays through a program to prevent multiple triggering is a crude way to
do this and makes it unresponsive.

You should ideally completely separate out the menu code from the motor code - the action
functions are a mix of both.

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