While loop keep simultaneously running

Hi all,
I need your help. I'm doing an Arduino project to control step motor go up and down till I release button. But when I push the button, my step just keeps running over and over. Could you please tell me what I'm doing wrong?
Thank you so much for taking time to read my noob question.

Here is my code

//                chuong trinh chay may han cell pin 
//         A0 noi chan giua trimer chinh thoi gian
//         D2 ra Pedal dieu khien han
//         D3 chan dieu khien step
//         D12 tin hieu ra SSR
//         D13 den bao hien thi
//         D9 stepp
//         D8 Dir
//          
//
//
//
//
//--------------------------------------------------------------------------------------
//                                         INCLUDES
//--------------------------------------------------------------------------------------
#include <Wire.h>                           // I2C Library
#include <Adafruit_SSD1306.h>               // OLED driver Library
//--------------------------------------------------------------------------------------
//                                        DEFINES
//--------------------------------------------------------------------------------------

#define OLED_RESET     4
#define timerPin       12
#define DIR_PIN         8
#define STEP_PIN        9
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//--------------------------------------------------------------------------------------
//                                        OBJECTS
//--------------------------------------------------------------------------------------
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);       //create instance of OLED

//--------------------------------------------------------------------------------------
//                                       VARIABLES
//--------------------------------------------------------------------------------------

int controlValue = 0;
int timerValue = 0;
int j = 0;
int period = 500;
unsigned long time_now = 0;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

//--------------------------------------------------------------------------------------
//                                       MAIN PROGRAM
//--------------------------------------------------------------------------------------

  void setup()
  {
    Serial.begin(9600);
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    delay(10);
    display.clearDisplay();
    display.setTextSize(1);                   // set text size
    display.setTextColor(WHITE);
    display.setCursor(25,0);                   // set cursor upper left
    display.println("Timer Control");
    display.println("  ");
    display.println("Software V2.2");
    display.println("  ");
    display.println("by Embehu");
    display.display();
    delay(1000);
    display.clearDisplay();
    pinMode(timerPin, OUTPUT);
    pinMode(2, INPUT_PULLUP);         // set D2 as input
    pinMode(3, INPUT_PULLUP);         // set D3 as input
    pinMode(DIR_PIN, OUTPUT); 
    pinMode(STEP_PIN, OUTPUT); 
    digitalWrite(STEP_PIN, LOW);
    digitalWrite(DIR_PIN, LOW);

   }

void loop() 
{
  time_now = millis();
 int Switch = digitalRead(2);
 int Switch1 = digitalRead(3);
 int controlValue = analogRead(A0);
 int timerValue = map (controlValue, 0, 1023, 0, 100);
 j = timerValue*2;

  //-----------------------Display Function--------------------------------------------
  //----------------------------------------------------------------------------------
    display.setTextSize(1);                   // set text size
    display.setCursor(0,0); 
    display.print("Time Setting");                   // print title
    display.drawLine(0, 10, 128, 10, WHITE);
    display.setCursor(32,32);
    display.setTextSize(4);  
    display.print(j);                    // print timer
    display.setTextSize(1);  
    display.print(" mS");  
    display.display();                        // display all we just printed
    while(millis() < time_now + period){        //wait approx. [period] ms
    display.clearDisplay();                   // clear display
    }

  // check if the pushbutton is pressed. If it is, the buttonState is LOW:
 
  if ( (millis() - lastDebounceTime) > debounceDelay) {
      if (Switch == LOW) {
        rotateDeg(1800, 1);
        digitalWrite(timerPin, HIGH);
        digitalWrite(13, HIGH);
        delay(timerValue); // pre-weld time
        digitalWrite(timerPin, LOW);
        digitalWrite(13, LOW);
        delay(500);
        digitalWrite(timerPin, HIGH);
        digitalWrite(13, HIGH);
        delay(j); // weld time
        digitalWrite(timerPin, LOW);
        digitalWrite(13, LOW);
        rotateDeg(-1800, 1);
        lastDebounceTime = millis(); //set the current time 
                  }
 }

if (j == 10) {
while (Switch1 == LOW){
digitalWrite(DIR_PIN, HIGH);
digitalWrite(STEP_PIN, HIGH); 
delayMicroseconds(70); 
digitalWrite(STEP_PIN, LOW); 
delayMicroseconds(70); 
if (Switch1 == HIGH){
break;
}
}
}
if (j == 14) {
while (Switch1 == LOW){
digitalWrite(DIR_PIN, LOW);
digitalWrite(STEP_PIN, HIGH); 
delayMicroseconds(70); 
digitalWrite(STEP_PIN, LOW); 
delayMicroseconds(70); 
if (Switch1 == HIGH){
break;
}
}
}
}   


void rotateDeg(float deg, float speed){ 
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
  int dir = (deg > 0)? HIGH:LOW;
  digitalWrite(DIR_PIN,dir); 

  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 
    delayMicroseconds(usDelay); 
    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
}

Hi,

while (Switch1 == LOW){
digitalWrite(DIR_PIN, LOW);
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(70);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(70);
if (Switch1 == HIGH){
break;
}

The condition in your while loop is Switch1.
Where in the loop do you check to see if Switch1 has changed.
Where is the

Switch1 = digitalRead(3);

in the while loop.
Before reposting your code in a new post, press CTRL-T keys to auto format your code to make it easier to read.

Thanks.. Tom... :slight_smile:

Better still, replace the WHILE with IF and allow loop() to do the repetition. Then the digitalRead()s at the top of loop() will be updated every time.

...R

Dear Tom
I thought in the loop I put
int switch1==digitalread(3)
So this is not enough for breaking the while loop?
Dear Robin
I did try but the step go 1 turn then pause a little bit (maybe 0.5s or less) then go till I release button.

Thanks all, Trung

Embehu:
Dear Tom
I thought in the loop I put
int switch1==digitalread(3)
So this is not enough for breaking the while loop?

No,once you are in the while loop, you are stuck in the while loop until the condition is false.
This can only happen by doing a digitalRead, to check the condition, from within the while loop.

@Robin2 suggestion is much better for program flow.

Tom... :slight_smile: