Using the IR remote control to end the infinite while loop

Hello, I am currently working on one project which is using IR remote to control the stepper motor.
I made an infinite while loop for running the stepper motor when I press "0" on the remote.
However, I also put break inside of the while loop when I press "power" on the remote, it should stop the stepper motor. But, it did not, even I press the "power" button. It was kept running. Does anyone know how do I stop the motor (which is break the while loop) if I press "power" button?
Here is my code.
Thank you

#include "Stepper.h"
#include "IRremote.h"

/*----- Variables, Pins -----*/
#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 6; // Signal Pin of IR receiver to Arduino Digital Pin 6
String myCom;

/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4

Stepper small_stepper(STEPS, 8, 10, 9, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'

void setup()
{ 
  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
if (irrecv.decode(&results)) // 

  {

    if (results.value == 0xFF6897) {
      myCom="zero";
      Serial.println(myCom);
    }
    if (results.value == 0xFFA25D) {
      myCom="pwr";
      Serial.println(myCom);
    }
    if (results.value == 0xFF906F){
      myCom="up";
      Serial.println(myCom);
    }
    if (results.value == 0xFFE01F){
      myCom="dn";
      Serial.println(myCom);
    }    


    if (myCom=="up"){
      small_stepper.setSpeed(300);
      Steps2Take = -28.5; // Rotate 5 degree CCW
      small_stepper.step(Steps2Take);
      delay(200);
    }

    if (myCom=="dn"){
      small_stepper.setSpeed(300);
      Steps2Take = 28.5; // Rotate 5 degree CW
      small_stepper.step(Steps2Take);
      delay(200);
    }

    
    while (myCom=="zero"){
        small_stepper.setSpeed(100); //Max seems to be 700
        Steps2Take  =  -398; //70 degree CCW   //-512  // Rotate 90 degree CCW
        small_stepper.step(Steps2Take);
        delay(1000); //30secs delay
        small_stepper.setSpeed(100);
        Steps2Take  =  398; // Rotate 70 degree CW
        small_stepper.step(Steps2Take);
        delay(1000); //30secs delay
        if (myCom =="pwr")
        {
          break;
        }
        delay(100);
        }


      delay(100);
      irrecv.resume(); // receive the next value
  }
}  

Welcome to the forum

    while (myCom == "zero")

you have no code to change the value of myCom so how will the while loop ever end ?
Inside this while loop

        if (myCom =="pwr")
        {
          break;
        }
        delay(100);
        }

I want something like above code if I press "pwr" it end the while-loop, but it did not work.
I am not sure what did I do wrong. Or is there any ways to stop the infinite while loop?

        if (myCom =="pwr")
        {
          break;
        }
        delay(100);
        }

I want something like above code if I press "pwr" button from the remote, it end the while-loop. But, it does not work.
I am not sure what did I do wrong. Or is there any ways to stop the infinite while loop when I press different button from the IR remote while motor is running.


while (myCom=="zero"){

 if (results.value == 0xFFA25D) {
      myCom="pwr";
      Serial.println(myCom);
    }

If myCom is not read when the program is stuck in the while() then myCom will not get updated.

I think this will do what you want. Instead of a separate infinite loop, use the loop() function to repeat the 'zero' command as long as it is the last key code received.

#include "Stepper.h"
#include "IRremote.h"

/*----- Variables, Pins -----*/
#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 6; // Signal Pin of IR receiver to Arduino Digital Pin 6
String myCom;

/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4

Stepper small_stepper(STEPS, 8, 10, 9, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'

void setup()
{
  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
  if (irrecv.decode(&results)) //
  {
    if (results.value == 0xFF6897)
    {
      myCom = "zero";
      Serial.println(myCom);
    }

    if (results.value == 0xFFA25D)
    {
      myCom = "pwr";
      Serial.println(myCom);
    }

    if (results.value == 0xFF906F)
    {
      myCom = "up";
      Serial.println(myCom);
      small_stepper.setSpeed(300);
      Steps2Take = -28.5; // Rotate 5 degree CCW
      small_stepper.step(Steps2Take);
      delay(200);
    }

    if (results.value == 0xFFE01F)
    {
      myCom = "dn";
      Serial.println(myCom);
      small_stepper.setSpeed(300);
      Steps2Take = 28.5; // Rotate 5 degree CW
      small_stepper.step(Steps2Take);
      delay(200);
    }

    delay(100);
    irrecv.resume(); // receive the next value
  }

  // Each time through loop() while the last input was 'zero'
  if (myCom == "zero")
  {
    small_stepper.setSpeed(100); //Max seems to be 700
    Steps2Take  =  -398; //70 degree CCW   //-512  // Rotate 90 degree CCW
    small_stepper.step(Steps2Take);
    delay(1000); //30secs delay
    small_stepper.setSpeed(100);
    Steps2Take  =  398; // Rotate 70 degree CW
    small_stepper.step(Steps2Take);
    delay(1000); //30secs delay
  }
}

I want something that the motor is keep running unless when I said it is stop.
That is why I made the infinite while loop for it and trying to put the if-break inside of the while loop.
However, it did not stop when I said stop.
That is the problem

How would the sketch know that you had commanded the while loop to stop once it was in the while loop ?

One easy way would be that when you see the zero button set a flag and when you see the pwr button then clear the flag. Then outside of the if (irrecv.decode(&results)) code block check to see if the flag is set and if so run the motor. This code will execute over and over until you press the pwr key.

A couple of things to be aware of:

  1. All the delays you have in the code will delay when your button presses are detected
  2. The small_stepper.step() method has an integer as an argument. Why are you trying to pass fractional values?

The infinite loop did not contain irrecv.decode(&results) so it never looked for input from the IR remote. Since you don't look for input, you won't find the code for "pwr" so the motor won't stop.

    while (myCom=="zero"){
      if (irrecv.decode(&results)){
        if (results.value == 0xFFA25D){
          myCom="pwr";
          Serial.println(myCom);
          }
        small_stepper.setSpeed(100); //Max seems to be 700
        Steps2Take  =  -398; //70 degree CCW   //-512  // Rotate 90 degree CCW
        small_stepper.step(Steps2Take);
        delay(1000); //30secs delay
        small_stepper.setSpeed(100);
        Steps2Take  =  398; // Rotate 70 degree CW
        small_stepper.step(Steps2Take);
        delay(1000); //30secs delay
        irrecv.resume();
        
        if (myCom =="pwr")
        {
          small_stepper.setSpeed(0);
          Steps2Take  =  0;
          delay(0);
          break;
        }
        delay(100);
        irrecv.resume();
        }
    }

This is my new while loop and still does not work. The motor is keep running but never stop even I press "pwr" button.

I had little change so I put irrecv.decode(&results) inside of the while loop again, but the while loop still not stop even I press "pwr" button. I am not sure what is wrong. I know it should work on Python, but not good at Arduino, and I am not sure what caused the problem here.

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