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:
#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);
}
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.
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: