How to control electromotors with IR?

I am starting project with remote control car and I can not find solution how to control motors in such way that when you press and hold button UP it accelerates and when i release Button UP it stops? Can someone help me with code for that?

#include <IRremote.h>

int IRpin = 8;  // pin for the IR sensor
  
IRrecv irrecv(IRpin);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  //pinMode(5, OUTPUT);
  pinMode(6,OUTPUT);
  //pinMode(7,OUTPUT);
}

void loop() 
{
   
  if(irrecv.decode(&results)) 
  {
      irrecv.resume();   
      Serial.println(results.value);  // Receive the next value
   
  
     if(results.value == 1253111734)  // this is where you put in your IR remote button #
     {
       digitalWrite(6, HIGH);  
         
            
      }
              
      }
      if(results.value == 2575714394) 
      {
        digitalWrite(6, LOW);
         
       
      }
                  // keeps the transistion smooth
        
        
}

You need to use a analogWrite on a PWM pin and use an increment something like this:

val= val + inc;
analogWrite(6,  val);

can you help me more please because I am beginner:)?

Here’s a sketch I wrote 2 years ago to control motor speeds based on various IR codes. You’ll need to put in the codes for whatever your remote transmits.

You’re welcome to use it, YMMV.

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

/* well now it controls 2x motors on MotoMama (298) board
 
 Jim Brown June 2012
 jim.brownza at gmail dot com
 
 *******   PWM on pin3 seems disabled when using IR?
 
 Uses a Philips remote (with RC5 coding)
 under rc5, the keys have two toggled values
 speed:
 vol down:    411 or c11 hex; 1041 or 3089 dec
 vol up:      410 or c10 hex; 1040 or 3088 dec
 stop:
 standby:     3148 or 1100
 direction:
 cd (fwd):    3391 or 1343
 tuner (rev): 3199 or 1141 
 */


#include <IRremote.h>

int RECV_PIN = 11;

//get the motors sorted
int motorA_speed_pin = 5;   // seems PWM not work on 3 with IR library This is the MM EnA
int motorA_speed = 0;  //always start at rest
//int motorA_speed_temp;  //for dir change
int motorA_dir_pin1 = 2; // MM ln1
int motorA_dir_pin2 = 4; // MM ln2
int motorB_dir_pin1 = 7; // MM ln3
int motorB_dir_pin2 = 8; // MM ln4
int motorB_speed_pin = 6;   // seems PWM not work on 3 with IR library This is the MM EnB
int motorB_speed = 0;  //always start at rest
//int motorB_speed_temp;  //for dir change
int motor_speed_step = 10;     // and change in pwm steps of 10 same for both motors
float turn_diff_factor = 0.8; 
int turn_time = 1000; //milliseconds

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(motorA_speed_pin, OUTPUT);
  analogWrite(motorA_speed_pin, motorA_speed);
  pinMode(motorA_dir_pin1, OUTPUT);
  pinMode(motorA_dir_pin2, OUTPUT);
  digitalWrite(motorA_dir_pin1, 1); //start forward
  digitalWrite(motorA_dir_pin2, 0); //start forward

  pinMode(motorB_speed_pin, OUTPUT);
  analogWrite(motorB_speed_pin, motorB_speed);
  pinMode(motorB_dir_pin1, OUTPUT);
  pinMode(motorB_dir_pin2, OUTPUT);
  digitalWrite(motorB_dir_pin1, 1); //start forward
  digitalWrite(motorB_dir_pin2, 0); //start forward
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC);
    irrecv.resume(); // Receive the next value

      //now act on various key presses

    // *********************** STOP
    if (results.value == 3148 || results.value == 1100) {   
      motorA_speed = 0;
      motorB_speed = 0;
      Serial.print("Stopping ");
      Serial.print(motorA_speed);
      Serial.print("\t");
      Serial.println(motorB_speed);

      analogWrite(motorA_speed_pin, motorA_speed);
      analogWrite(motorB_speed_pin, motorB_speed);
    } //end of stopping
    // *********************** END OF STOP 

    // *********************** SET STRAIGHT LINE SPEED
    //straight line speed.... both motors the same
    //    only time they differ is during a turn and it restores the changed one
    //          after a turn anyway
    //
    if (results.value == 1041 || results.value == 3089) {
      //slowing
      //slow motorA
      motorA_speed = motorA_speed - motor_speed_step;
      if (motorA_speed < 0) {
        motorA_speed = 0;
      }
      //slow motorB
      motorB_speed = motorB_speed - motor_speed_step;
      if (motorB_speed < 0) {
        motorB_speed = 0;
      }

      Serial.print("Going down to ");
      Serial.print(motorA_speed);
      Serial.print("\t");
      Serial.println(motorB_speed);

      analogWrite(motorA_speed_pin, motorA_speed);
      analogWrite(motorB_speed_pin, motorB_speed);
    } //end of slowing

    //
    if (results.value == 1040 || results.value == 3088) {
      //speeding
      //speed motorA
      motorA_speed = motorA_speed + motor_speed_step;
      if (motorA_speed > 255) {
        motorA_speed = 255;
      }
      //speed motor2
      motorB_speed = motorB_speed + motor_speed_step;
      if (motorB_speed > 255) {
        motorB_speed = 255;
      }

      Serial.print("Going up to ");
      Serial.print(motorA_speed);
      Serial.print("\t");
      Serial.println(motorB_speed);

      analogWrite(motorA_speed_pin, motorA_speed);
      analogWrite(motorB_speed_pin, motorB_speed);
    }  //end of speeding
    // *********************** END OF SET STRAIGHT LINE SPEED

    // *************************** SET DIRECTION
    // for the hell of it, gna stop and continue at same after setting speed
    if (results.value == 3391 || results.value == 1343) {  
      // forward
      Serial.print("Setting direction Forward");
      //stop
      analogWrite(motorA_speed_pin, 0);
      analogWrite(motorB_speed_pin, 0);
      // set dir forward
      digitalWrite(motorA_dir_pin1, 1); 
      digitalWrite(motorA_dir_pin2, 0); 
      digitalWrite(motorB_dir_pin1, 1); 
      digitalWrite(motorB_dir_pin2, 0);
      // back to speed
      analogWrite(motorA_speed_pin, motorA_speed);
      analogWrite(motorB_speed_pin, motorB_speed);
    }  // end of forward

      if (results.value == 3199 || results.value == 1141) {  
      // reverese
      Serial.print("Setting direction Reverse");
      //stop
      analogWrite(motorA_speed_pin, 0);
      analogWrite(motorB_speed_pin, 0);
      // set dir forward
      digitalWrite(motorA_dir_pin1, 0); 
      digitalWrite(motorA_dir_pin2, 1); 
      digitalWrite(motorB_dir_pin1, 0); 
      digitalWrite(motorB_dir_pin2, 1);
      // back to speed
      analogWrite(motorA_speed_pin, motorA_speed);
      analogWrite(motorB_speed_pin, motorB_speed);
    }  // end of reverse
    // *********************** END OF SET DIRECTION

    // ************************* TURNING
    if (results.value == 3169 || results.value == 1121) {  
      // turn left... slow motorA
      Serial.println("Turning LEFT");
      analogWrite(motorA_speed_pin, turn_diff_factor * motorA_speed);
      delay(turn_time);
      analogWrite(motorA_speed_pin, motorA_speed);        
    } //end of left

    if (results.value == 3168 || results.value == 1120) {   
      // turn right... slow motorB
      Serial.println("Turning RIGHT");
      analogWrite(motorB_speed_pin, turn_diff_factor * motorB_speed);
      delay(turn_time);
      analogWrite(motorB_speed_pin, motorB_speed);
    } //end of right 
  } //end of ir
} //end of loop

Thank you! :slight_smile: