Can someone help me to improve my code? IR remote control to control Motor

Hi All, can someone assist me to improve a code below? it is working as i would like but i need to loop on the condition when i press button number "1", so it keeps rotating motor from and back using function void rotateMotor(). but i also would like to be able to stop this function whenever pressing any other button or in example can be a button "5" that in my function it stop spinning motor at all. Will attach photo of the project as well so you may understand how it connected.


#include <IRremote.h>

IRrecv IR(2);
const int motorPWM = 9;  // PWM pin for motor speed control
const int motorDir1 = 10; // Motor direction pin 1
const int motorDir2 = 11; // Motor direction pin 2
const int BUTTON_CODE = 0xEA15BF00;

decode_results results;

void setup()
{
  IR.enableIRIn();
  Serial.begin(9600);
  pinMode(motorPWM, OUTPUT);
  pinMode(motorDir1, OUTPUT);
  pinMode(motorDir2, OUTPUT);
}

void check(){
  if (Serial.println(IR.decodedIRData.decodedRawData, HEX) == 0xEA15BF00){
    digitalWrite(motorDir1, LOW);
    digitalWrite(motorDir2, HIGH);
    analogWrite(motorPWM, 255);  // Full speed
    delay(5000);
  }
  else {
    rotateMotor(4, 50, motorDir1);  
  	rotateMotor(4, 50, motorDir2);
  }
}


void rotateMotor(int rotations, int speed, int motdirec) {
  for (int i = 0; i < rotations; i++) {
    if (motdirec == 10){
      digitalWrite(motorDir1, HIGH);
	  digitalWrite(motorDir2, LOW);
      analogWrite(motorPWM, 126); // 255 max speed
      delay(speed * 10);
      digitalWrite(motorDir1, LOW);
      //delay(500); // Pause between rotations
    }
    else{
      digitalWrite(motorDir2, HIGH);
	  digitalWrite(motorDir1, LOW);
      analogWrite(motorPWM, 126);
      delay(speed * 10);
      digitalWrite(motorDir2, LOW);
      //delay(500);  // Pause between rotations
      //check();
  }
  }  
}

void loop()
{
  if(IR.decode()){
    Serial.println(IR.decodedIRData.decodedRawData, HEX);
    // 1 repeat 4 times front and back
    if(IR.decodedIRData.decodedRawData == 0xEF10BF00){
          rotateMotor(4, 50, motorDir1);  // Mode 1: 4 rotations, half speed
          rotateMotor(4, 50, motorDir2); // Mode 1: 4 rotations, half speed in reverse (clockwise)
    }
    //2 go in full speed
    if(IR.decodedIRData.decodedRawData == 0xEE11BF00){
        digitalWrite(motorDir1, HIGH);
        digitalWrite(motorDir2, LOW);
        analogWrite(motorPWM, 255);  // Full speed
        delay(1000);
    }
    //3 go in reverse
    if(IR.decodedIRData.decodedRawData == 0xED12BF00){
        digitalWrite(motorDir1, LOW);
        digitalWrite(motorDir2, HIGH);
        analogWrite(motorPWM, 255);  // Full speed
        delay(1000);
    }
    // 5 stop motor
    if(IR.decodedIRData.decodedRawData == 0xEA15BF00){
      analogWrite(motorPWM, 0);
      delay(10);
    }
    //delay(500);
    IR.resume();
  }
}

Would you also describe this?

1 Like

well, what i mean is i have 4 programmed button on IR remote control:
Buttons:
1 - spin motor clockwise 4 times with pause and counter clockwise 4 times.
2 - spin motor on maximum.
3 - spin motor in reverse on maximum.
5 - Stop motor.

So what i need help is to make whenever press button "1" motor start spinning clockwise and counter clockwise infinitely. But whenever i press any other button such as "2" or "5" it will come out from infinite function and perform actions from pressed button.
i hope this can help you to understand what i want to achive.
thank you

Try this... before setup(), create an "infinite flag" and reset as "off"

int infinite = 0; // 0 = NOT infinite

Next, create a condition outside the button press tests "if the infinite flag is set... run your motors"

if (infinite) { // infinite flag was set... make the motors run
      rotateMotor(4, 50, motorDir1);  // Mode 1: 4 rotations, half speed
      rotateMotor(4, 50, motorDir2); // Mode 1: 4 rotations, half speed in reverse (clockwise)
}

Then, if you press "button 1" you will need to turn the infinite flag "on" inside the "button 1" condition

infinite = 1;

Then, inside button "2", "3" and "5" you need to turn the infinite flag OFF so the "infinite" condition fails...

infinite = 0;
2 Likes

That worked perfectly!! thank you very much, was very easy to solve but i haven't seen this solution on my own!
thank you again!

Very nice. Even better, you did the work. Have fun!

[edit]By the way... you just created a "finite state machine" (FSM) that does not rely on an action ("the button IS pressed") rather the action occurred ("the button WAS pressed") and then you changed states of the "infinite" flag. You will use FSM quite a bit when controlling devices. This is one example, but I would recommend finding other points of view on the FSM...

2 Likes

In your projects, suggest you avoid using delay(. . .).

delay( ) can make your code, pause, miss events, and stutter.

1 Like

But why did you use 1, 2, 3 and 5? Something you don't like about 4? :expressionless:

Maybe

1    2    3

     5

AmIRight?

a7

1 Like

Really appreciate for this tip, i think on the example below i won't be able to avoid delay(), so i can create a pause that will make motor works for specific period of time before it will shutdown. Is there any other recommendation can be regarding if i need to make motor runs for a while?
delay

hahahaha, nice point :slight_smile:
No i don't have anything against number 4, i left it for a future function.

Very interesting, much appreciated, will note it and see how may it work for my project, but i feel what you mean.
thanks

  • In the future, do not attach pictures of your code.

  • Instead, attach your code as text so we can copy it and do changes to it.

  • The only time it might be acceptable to use delay(...) is when it is very short and doesn't repeat too often.
    If delay()s are long and frequent, you should consider using the State Machine programming technique to incorporate non blocking delay periods,

1 Like

I was going to then I saw @xfpd has already mentioned the FSM concept and linked to one of dozens of explainers in #6 above.

google

arduino FSM

if that doesn't make sense right away, many ppl have published what they believe to be the best examples and code.

One or more will work for you and your life will change.

Well at least the way you program things like this will.

a7

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