How would it work?

We will again modify the remote controlled car we built in the previous blog to make it work using joystick instead. Instead of using the remote control to control the car, we will use the IR Transmitter to send the IR signal/code. We will use joystick movement to decide when to send IR code for forward, backward, left and right movement of the car. Yes, we need to create two separate programs for this project. One for the remote controlled car, and one for the joystick. We will need two Arduino Uno boards for this project.

I first programmed the car with the IR receiver and printed the received IR codes in the serial monitor, connected that Arduino board to one computer and kept monitoring the serial monitor. I then programmed the second Arduino board for the joystick and the IR transmitter. Using same IRremote library which I was using for receiving the IR codes, I sent different IR codes for different joystick positions and confirmed that they are being received by my car (IR receiver) as they were being printed in the serial monitor running in the first computer. Rest is easy as you can see in the program described below.

Components needed

  • L298N Motor Driver
  • 2 DC motors
  • 2 wheels
  • IR Receiver
  • IR Transmitter
  • Joystick
  • Jumper cables
  • Arduino Uno board x 2
  • Power supply

I bought IR receiver and IR transmitter from Amazon. Here is the link if you are interested.

MagicW Digital 38khz Ir Receiver 38khz Ir Transmitter Sensor Module Kit for Arduino Compatible

IR Transmitter and IR Receiver ( Image courtesy Amazon )

IR Receiver (program for the car)

#include <IRremote.h>
int irPin = 7;
IRrecv irRecive(irPin);
decode_results results;
// left motor
int leftMotorSpeedPin = 3;
int leftMotorForwardPin = 4;
int leftMotorBackwardPin = 5;
// right motor
int rightMotorSpeedPin = 11;
int rightMotorForwardPin = 12;
int rightMotorBackwardPin = 13;
int direction = 0; //0 = stop, 1 = forward, 2 = bakward
void setup() {
  pinMode(leftMotorSpeedPin, OUTPUT);
  pinMode(leftMotorForwardPin, OUTPUT);
  pinMode(leftMotorBackwardPin, OUTPUT);
  pinMode(rightMotorSpeedPin, OUTPUT);
  pinMode(rightMotorForwardPin, OUTPUT);
  pinMode(rightMotorBackwardPin, OUTPUT);
  digitalWrite(leftMotorSpeedPin, HIGH);
  digitalWrite(rightMotorSpeedPin, HIGH);
  //analogWrite(leftMotorSpeedPin, i);
  //analogWrite(rightMotorSpeedPin, i);
  Serial.begin(9600);
  irRecive.enableIRIn();
}
void loop() {
  if (irRecive.decode(&results)) {
    switch (results.value) {
      case 11111:
        Serial.println("case: goForward");
        goForward();
        break;
      case 22222:
        Serial.println("case: goBackwards");
        goBackward();
        break;
      case 33333:
        Serial.println("case: goLeft");
        goLeft();
        break;
      case 44444:
        Serial.println("case: goRight");
        goRight();
        break;
      case 00000:
        stop();
        break;
      default:
        Serial.println("case: default");
       
    }
    irRecive.resume();
  }
}
void goForward() {
  Serial.println("goForward() function called");
  digitalWrite(leftMotorForwardPin, HIGH);
  digitalWrite(leftMotorBackwardPin, LOW);
  digitalWrite(rightMotorForwardPin, HIGH);
  digitalWrite(rightMotorBackwardPin, LOW);
  direction = 1;
}
void goBackward() {
  Serial.println("goBackward() function called");
  digitalWrite(leftMotorForwardPin, LOW);
  digitalWrite(leftMotorBackwardPin, HIGH);
  digitalWrite(rightMotorForwardPin, LOW);
  digitalWrite(rightMotorBackwardPin, HIGH);
  direction = 2;
}
void goRight() {
  Serial.println("goRight() function called");
  digitalWrite(leftMotorForwardPin, HIGH);
  digitalWrite(leftMotorBackwardPin, LOW);
  digitalWrite(rightMotorForwardPin, LOW);
  digitalWrite(rightMotorBackwardPin, LOW);
}
void goLeft() {
  Serial.println("goLeft() function called");
  digitalWrite(leftMotorForwardPin, LOW);
  digitalWrite(leftMotorBackwardPin, LOW);
  digitalWrite(rightMotorForwardPin, HIGH);
  digitalWrite(rightMotorBackwardPin, LOW);
}
void stop() {
  Serial.println("stop() function called");
  digitalWrite(leftMotorForwardPin, LOW);
  digitalWrite(leftMotorBackwardPin, LOW);
  digitalWrite(rightMotorForwardPin, LOW);
  digitalWrite(rightMotorBackwardPin, LOW);
  direction = 0;
}

IR trANSMITTER (PROGRAM FOR THE JOYSTICK)

#include <IRremote.h>
const int SW_pin = 2; // digital pin connected to switch output
const int x = A0; // analog pin connected to X output
const int y = A1; // analog pin connected to Y output
int xValue, yValue;
IRsend irsend;
void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(9600);
  
}
void loop() {
   
  xValue = analogRead(x);
  yValue = analogRead(y);
  
  if(xValue == 518 && yValue == 518) {
    Serial.println("Stop");
    irsend.sendNEC(00000, 32);
  } else if (xValue > 518 && yValue >= 254 && yValue <= 700) {
    Serial.println("go forward");
    irsend.sendNEC(11111, 32);
  } else if (xValue < 518 && yValue >= 254 && yValue <= 700) {
    Serial.println("go backward");
    irsend.sendNEC(22222, 32);
  } else if (xValue >= 254 && xValue <= 700 && yValue < 518) {
    Serial.println("go left");
    irsend.sendNEC(33333, 32);
  } else if (xValue >= 254 && xValue <= 700 && yValue > 518) {
    Serial.println("go right");
    irsend.sendNEC(44444, 32);
  }
  delay(1000);
}

Demo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s