STEPPER MOTOR

Hello, i have a Little problem.

Here i put a part of the code:

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>

#define OLED 0x3C
Adafruit_SSD1306 display(-1);
LiquidCrystal_I2C LCD(0x27, 16, 2); 
#define STEPS 200
Stepper MOTOR ( STEPS, 4,5,6,7);

#define  WHITE_1 8
#define  WHITE_2 9
#define  WHITE_3 10
#define YELLOW_1 11
#define YELLOW_2 12

#define GREEN 13
#define BLUE 1
#define RED 0

void setup() {
  //SET DISPLAYS
  display.begin(SSD1306_SWITCHCAPVCC, OLED);
  display.clearDisplay();
  display.display();
  LCD.init();
  LCD.clear();
  LCD.backlight();

//MOTOR
MOTOR.setSpeed(0);
MOTOR.step(0);

  //PINMODE
pinMode( WHITE_1, OUTPUT);
pinMode( WHITE_2, OUTPUT);
pinMode( WHITE_3, OUTPUT);
pinMode( YELLOW_1, OUTPUT);
pinMode( YELLOW_2, OUTPUT);

pinMode( GREEN, INPUT);
pinMode( BLUE, INPUT);
pinMode( RED, INPUT);

//OLED

display.setTextSize (1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("PRESS GREEN: RESET");
display.setCursor(0,24);
display.print("PRESS RED: INCREASE");
display.display();

  LCD.setCursor(0,0);
  LCD.print("MOTOR LEVEL");
  LCD.setCursor(0,1);
  LCD.print("LEVEL MIN");

digitalWrite(WHITE_1, LOW);
digitalWrite(WHITE_2, LOW);
digitalWrite(WHITE_3, LOW);
digitalWrite(YELLOW_1, LOW);
digitalWrite(YELLOW_2, LOW);

}

void loop() {

// LEVEL ONE //-------------------------------------------------------------------------//
if ( digitalRead (RED)==LOW && digitalRead(WHITE_1)==LOW){
   //DISPLAYS
  
  display.clearDisplay();
  
 
  LCD.clear();
  LCD.backlight();
  
  display.setTextSize (1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("PRESS GREEN: RESET");
display.setCursor(0,24);
display.print("PRESS RED: INCREASE");
display.setCursor(0,48);
display.print("PRESS BLUE: DECREASE");
display.display();

  LCD.setCursor(0,0);
  LCD.print("MOTOR LEVEL");
  LCD.setCursor(0,1);
  LCD.print("LEVEL 1");

  

digitalWrite(WHITE_1, HIGH);
digitalWrite(WHITE_2, LOW);
digitalWrite(WHITE_3, LOW);
digitalWrite(YELLOW_1, LOW);
digitalWrite(YELLOW_2, LOW);
  delay(500);

  MOTOR.setSpeed(50);
MOTOR.step(2000);
 
}


//-------------------------------------------------------------------------//

if ( digitalRead (BLUE)==LOW && digitalRead(WHITE_1)==HIGH && digitalRead(WHITE_2)==LOW){
   //SET DISPLAYS
  
  display.clearDisplay();
  
 
  LCD.clear();
  LCD.backlight();
  
  display.setTextSize (1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("PRESS GREEN: RESET");
display.setCursor(0,24);
display.print("PRESS RED: INCREASE");

display.display();

  LCD.setCursor(0,0);
  LCD.print("MOTOR LEVEL");
  LCD.setCursor(0,1);
  LCD.print("LEVEL MIN");



digitalWrite(WHITE_1, LOW);
digitalWrite(WHITE_2, LOW);
digitalWrite(WHITE_3, LOW);
digitalWrite(YELLOW_1, LOW);
digitalWrite(YELLOW_2, LOW);

  MOTOR.setSpeed(0);
MOTOR.step(0);
}}

my problem begins when i try to set the stepper (MOTOR) inside the sentence if (digitalRead (RED) == …) because i want to set the stepper when i press RED (button defined previously), but i want , while the stepper is setting on, press other button (GREEN or RED again) and the stepper set in stop.

how can i do that?

thanks!

marioval97: my problem begins when i try to set the stepper (MOTOR) inside the sentence if (digitalRead (RED) == ...) because i want to set the stepper when i press RED (button defined previously), but i want , while the stepper is setting on, press other button (GREEN or RED again) and the stepper set in stop.

Please read that to yourself out loud? Can you make sense of it? I certainly can't.

Tell us what your program actually does and what you want it to do that is different?

The regular Stepper library blocks the Arduino from doing anything else until it has completed its move. If you want to be able to stop the motor before the move is complete then either write your code to move one step at a time or maybe look at the AccelStepper library

It may also help to take all the display code out of the program and just concentrate on getting the motor to work the way you want.

...R Stepper Motor Basics Simple Stepper Code

i want to set a stepper with different speeds. I have three buttons, GREEN , RED and BLUE which reset, increase and decrease the speed respectively.

I want to press RED and set a speed of 50 rpm, and when i press RED again i want to increase the speed to 70 rpm, but the sketch doesn't let me do that, cause when i press the first time RED the stepper set in 50 rpm and i must wait till the sentence "if" is finished and the stepper set in off .

So i want to know a way to change this, i mean, press other button and change the speed of the stepper without it sets off.

Sorry, some times i express myself quite bad in my own language, so imagine how difficult could be sometimes in english.

marioval97: Sorry, some times i express myself quite bad in my own language, so imagine how difficult could be sometimes in english.

You have now explained it quite clearly, thank you.

You need to consider what I suggested in Reply #1.

...R

Look at the AccelStepper methods setSpeed() and runSpeed(). runSpeed() you'd call every time round loop, setSpeed() you'd call when you want to change the speed. Do not use delay(), this will prevent runSpeed() from being called frequently (which is a requirement for AccelStepper library).

In setup you should call setAcceleration() (trial and error is always needed to find the max stable acceleration for a stepper motor setup).

Note that this describes how to use AccelStepper for speed control, for position control the methods are moveTo() and run()