Is it possible to use IR sensor for more than one purpose?

hi I'm trying to use the IR remote to switch between 2 devices to control a servo motor and one of these devices is the IR remote itself.
This is the schematic diagram of the circuit:

This is the code used:

#include <IRremote.hpp>
#include <Servo.h>

//moving pin is for the variable resistor
const int moverPin = A0;
const int ReceiverPin = 11;
int moverPinStore;
int moverPinServe;//store mapped value of moverPinStore
bool isInUse;//for remote or for resistor(Used in The functions)
int moverRemoteServo = 0;

//bool returned;
//declaring servo object
Servo servoTest;

void setup() {
  //attach servo to pin 9 <PWM>
  servoTest.attach(9);
  pinMode(moverPin, INPUT);
  Serial.begin(9600);
  //initialize receiver and use built in LED as feedback
  IrReceiver.begin(ReceiverPin, 1);
}

//function used to get if we are in remote or resistor
bool IsInUse(bool &holdTrue) {
  if (IrReceiver.decode()) {
    if (IrReceiver.decodedIRData.command == 7) {
      holdTrue = true;
      Serial.println("Entering resistor control --->>");
    }

    else if (IrReceiver.decodedIRData.command == 68) {
      holdTrue = false;
      Serial.println("Entering Remote control --->>");
    }
    IrReceiver.resume();

  }
  return holdTrue;
}

void switcher(bool returner) {
  if (returner) {
    //if returner is true we enter here
    //and use the variable resistor to control
    Serial.println("In resistor control");

    moverPinStore = analogRead(moverPin);

    moverPinServe = map(moverPinStore, 0, 1023, 0, 180);
    Serial.println(moverPinServe);//debugger
    servoTest.write(moverPinServe);
    delay(100);
  }

  else {
    //if returner is false we use remote
    Serial.println("in remote control");

    if (IrReceiver.decode())
    {

      //if remote inpute is ">>" we move servo 5 steps
      if (IrReceiver.decodedIRData.command == 67) {
        moverRemoteServo += 5;
        Serial.println("Increasing");
        delayMicroseconds(100);


      }
      //if remote inpute is "<<" we move servo 5 steps opposite
      else if (IrReceiver.decodedIRData.command == 64) {
        moverRemoteServo -= 5;
        Serial.println("Decreasing");
        delayMicroseconds(100);



      }

      else {
        Serial.println(IrReceiver.decodedIRData.command);
      }


    }
    //This is to constrain the stuff between 0 and 180 degrees
    if (moverRemoteServo < 0) {
      moverRemoteServo = 0;
    }
    if (moverRemoteServo > 180) {
      moverRemoteServo = 180;
    }

    servoTest.write(moverRemoteServo);
  }



}

void loop() {
  bool returned = IsInUse(isInUse);

  switcher(returned);

  delay(10);

}

Yes, that is possible.

could you share an example maybe?

The IR emitter can produce any signal that your code can generate.

If you would take a few moments to explain your goal more clearly, perhaps forum members would have helpful suggestions.

By the way: avoid trouble by powering the servo separately (don't forget to connect the grounds). The electrical noise that servos generate on the power line can reset and even damage Arduinos.

1 Like

My goal is to use the IR remote to switch between using the variable resistor and the IR remote itself to control a servo.
When on the IR remote I would use two other buttons to control the servo.

Then you need to pick a button the remote to to tell your program to use the variable resistor and a separate button to stop the use of the resistor. With more logic, you could make a single remote button switch on and then switch off the resistor use.

Yh ive achieved this
my problem is the IR receiver bugging out and getting random inputs and I think this is a result of the IrReceiver.resume() line of code.
I called it twice cause I have to clear input from this function:

to get input from:

particularly this part:

Then you should have stated the thread with this information.

sorry

That is not hard to do. You will have to invent two new IR commands.

The first command tells your program to "use the information from the pot to control the servo".

The second command tells your program to "use IR commands to control the servo".

A state machine, with one state variable, is used to remember which mode is currently active.

Do you really not have a current limiting resistor on D1?

a7

its to pin 13

And?

It has a built in resistor

no it doesnt

hmm

it has built in LED with resistor but that is its own internal circuit, the pin is exposed without resistor for the external use

Thanks so much
I was able to use this State Machine approach and its finally working.
No bugs ,
thanks a lot man.

Please post your final solution.

TIA

a7

#include <IRremote.hpp>
#include <Servo.h>
const int resistorPin = A0;
int resistorPinStore;
//ReceiverPin is for the IrReceiver
const int IrReceiverPin = 12;
int moverRemoteServo = 0;
int increaser = 10;
int stateVariable = 1;
Servo servo1;

//function prototyping
void IrReceiverStateSwitcher();
int moveServoWithRes();
int moveServo();
//end of prototyping

void setup() {
  pinMode(resistorPin, INPUT);
  pinMode(IrReceiverPin, INPUT);
  servo1.attach(9);
  IrReceiver.begin(IrReceiverPin, 1);

  Serial.begin(9600);

}

void loop() {

  Serial.println(moverRemoteServo);
  switch (stateVariable) {
    case 1:
      servo1.write(moveServoWithRes());
      IrReceiverStateSwitcher();
      break;
    case 2:
      servo1.write(moveServoWithIr());
      break;
    default:
      Serial.print("Error!!!");
      exit(1);
      break;

  }

}

int moveServoWithIr() {

  if (IrReceiver.decode()) {
    if (IrReceiver.decodedIRData.command == 67) {
      moverRemoteServo += increaser;
    }
    if (IrReceiver.decodedIRData.command == 64) {
      moverRemoteServo -= increaser;
    }
    if (IrReceiver.decodedIRData.command == 9) {
      increaser += 1;
    }
    if (IrReceiver.decodedIRData.command == 21) {
      increaser -= 1;
    }
    if (IrReceiver.decodedIRData.command == 7) {
      stateVariable = 1;
    }
    if (IrReceiver.decodedIRData.command == 68) {
      stateVariable = 2;
    }

    IrReceiver.resume();
  }
  if (moverRemoteServo > 180) {
    moverRemoteServo = 180;
  }
  if (moverRemoteServo < 0) {
    moverRemoteServo = 0;
  }
  return moverRemoteServo;

}

int moveServoWithRes() {
  resistorPinStore = map(analogRead(resistorPin), 0, 1023, 0, 180);
  return resistorPinStore;

}

void IrReceiverStateSwitcher() {
  if (IrReceiver.decode()) {
    if (IrReceiver.decodedIRData.command == 7) {
      stateVariable = 1;
    }
    if (IrReceiver.decodedIRData.command == 68) {
      stateVariable = 2;
    }
    IrReceiver.resume();
  }

}

There it is!