DC Motor Control with IR Receiver and VNH2SP30 driver

Hi everyone,

I would like to control my dc motor with IR receiver and motor driver. I can control the dc motor in serial monitor user input. I use the code as below link. I can change motor speed to increase motor speed %10 with 4 input and to decrease 5 keyboard number. It works as well.

https://resultuzen.com.tr/blog/index.php/2021/04/11/vnh2sp30-motor-surucu-kullanimi/

I have already bought IR Receiver then I would like to control motor speed volume button. I know each button are assigned hex code ir library for example vol+ button hex code is FD807F. I tried to merge two code which code as above link and this youtube video https://www.youtube.com/watch?v=uPbwEO5aL2w

I uploaded my code which has already merged but it doesn't work. How can we fix this code and use IR Receiver.

VNH2SP30_kumanda_v02.ino (3.4 KB)

Thank you.

I, and other members, do not like to download code. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

#include <IRremote.h>

int kumandaPin = 2;
int motorAcmaPini = 8;
int motorA1Pini = 9;
int motorB1Pini = 10;
int motorHizPini = 11;
int motorAkimOlcmePini = A0;

IRrecv kumanda(kumandaPin);
decode_results sonuclar;

#define durKomutu            1
#define solaDonKomutu        2
#define sagaDonKomutu        3

short motorHizi = 50;
short motorAkimi = 0;
short motorYonu = durKomutu;

char yapilacakGorev;

void setup()
{
  kumanda.enableIRIn();
  pinMode(motorAcmaPini, OUTPUT);
  pinMode(motorA1Pini, OUTPUT);
  pinMode(motorB1Pini, OUTPUT);
  pinMode(motorHizPini, OUTPUT);
  pinMode(motorAkimOlcmePini, INPUT);
  Serial.begin(9600);

  while (!Serial) {
    ;
  }

  Serial.begin(9600);
  Serial.println("VNH2SP30 Motor Driver with DC Motor Control Program\n");
  Serial.println("Select Program in the menu:");
  Serial.println("1) Motor Stop");
  Serial.println("2) Motor Rotate Right");
  Serial.println("3) Motor Rotate Left");
  Serial.println("4) Motor Speed %10 Increase");
  Serial.println("5) Motor Speed %10 Decrease");
  Serial.println("6) Show Motor Flow\n");

}

void loop()
{

  while (kumanda.decode(&sonuclar))
  {
    Serial.print("Button Code: ");
    Serial.println(sonuclar.value, HEX);
    kumanda.resume();

    digitalWrite(motorAcmaPini, HIGH);

    if (sonuclar.value == 0xFFA25D)
    {
      motoruDurdur();
    }
    if (sonuclar.value == 0xFD807F)
    {
      sagaDon();
    }
    if (sonuclar.value == 0xFFE21D)
    {
      solaDon();
    }
    if (sonuclar.value == 0xFF6897)
    {
      hiziArtir();
    }
    if (sonuclar.value == 0xFFB04F)
    {
      hiziAzalt();
    }
  }
}

delay(100);
}

void motoruDurdur()
{
  Serial.println("Stopping Motor!");

  motorYonu = durKomutu;
  motoraGonder(motorYonu, durKomutu);
}

void solaDon()
{
  Serial.println("Rotate Right!");

  motorYonu = solaDonKomutu;
  motoraGonder(motorYonu, motorHizi);
}

void sagaDon()
{
  Serial.println("Ratote Left!");

  motorYonu = sagaDonKomutu;
  motoraGonder(motorYonu, motorHizi);
}

void hiziArtir()
{
  motorHizi = motorHizi + 10;

  if (motorHizi > 100)
  {
    motorHizi = 100;
  }

  Serial.print("Motor Speed: %");
  Serial.println(motorHizi);

  motoraGonder(motorYonu, motorHizi);
}

void hiziAzalt()
{
  motorHizi = motorHizi - 10;

  if (motorHizi < 0)
  {
    motorHizi = 0;
  }

  Serial.print("Motor Speed: %");
  Serial.println(motorHizi);

  motoraGonder(motorYonu, motorHizi);
}

void akimOlc()
{
  motorAkimi = ((analogRead(motorAkimOlcmePini) / 3) * 100);

  Serial.print("Motor Flow: ");
  Serial.print(motorAkimi);
  Serial.println(" mA/h");

}

void motoraGonder (short hedefYon, short hedefHiz)
{
  if (hedefYon == solaDonKomutu)
  {
    digitalWrite(motorA1Pini, LOW);
    digitalWrite(motorB1Pini, HIGH);
  }

  else if (hedefYon == sagaDonKomutu)
  {
    digitalWrite(motorA1Pini, HIGH);
    digitalWrite(motorB1Pini, LOW);
  }

  else if (hedefYon == durKomutu)
  {
    digitalWrite(motorA1Pini, HIGH);
    digitalWrite(motorB1Pini, LOW);
  }

  else
  {
    digitalWrite(motorA1Pini, LOW);
    digitalWrite(motorB1Pini, LOW);
  }

  analogWrite(motorHizPini, round(hedefHiz * 2.55));
}

Blockquote

One bad thing in the code is: There are no comments telling the intention of the lines of code....

 digitalWrite(motorB1Pini, LOW);

What does "LOW" do? I don't spend time discovering that...

this motorB1Pini is INA pin for the motor driver. INA and INB control motor rotation for example INA = HIGH and INB = LOW mean motor rotate right then INA = LOW and INB = HIGH mean motor rotate left.

What version of the IRremote library are you using? The above code is for the older (<3.0) version of the library. See the GitHub reference for how to write code for the newer version.

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