Hello, Noob working on an IR-triggered robot claw here. I've pared down the Example for IRServo to use two buttons on the remote, one to open, one to close.
Here's a video of it working:
My problem is that sometimes the claw does not stop in the open position, but merely does an open-and-close cycle. Sometimes. Do I need to assign the (pos) variable? idears? Thanks!
#include <IRLib.h>
#include <Servo.h>
#define MY_PROTOCOL NEC
#define RIGHT_ARROW 0xFFE01F //Move servo clockwise
#define LEFT_ARROW 0xFFA857 //Move servo counterclockwise
IRrecv My_Receiver(11);//Receive on pin 11
IRdecode My_Decoder;
Servo My_Servo; // create servo object to control a servo
int pos; // variable to store the servo position
int Speed; // Number of degrees to move each time a left/right button is pressed
void setup()
{
My_Receiver.No_Output();//Turn off any unused IR LED output circuit
My_Servo.attach(9); // attaches the servo on pin 9 to the servo object
pos = 0; // start at 0 degrees
Speed = 160; // servo opens/closes when left/right is pushed
My_Servo.write(pos); // Set initial position
My_Receiver.enableIRIn(); // Start the receiver
}
void loop()
{
if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();
if(My_Decoder.decode_type==MY_PROTOCOL) {
switch(My_Decoder.value) {
case LEFT_ARROW: pos=min(160,pos+Speed); break;
case RIGHT_ARROW: pos=max(0,pos-Speed); break;
}
My_Servo.write(pos); // tell servo to go to position in variable 'pos'
}
My_Receiver.resume();
}
}
I've also removed the variables from the Loop and just entered values of (0) and (160) and the same cycling happens. I don't want to go all the way to (180) because the claw mechanism can jam when fully open
I don't understand what you mean by "removed the variables from the Loop and just entered values of (0) and (160)"
Can you also post that version of the code?
...R
void loop()
{
if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();
if(My_Decoder.decode_type==MY_PROTOCOL) {
switch(My_Decoder.value) {
case LEFT_ARROW: pos=0; break; // go to 0 degrees
case RIGHT_ARROW: pos=180; break; // go to 180
}
My_Servo.write(pos); // tell servo to go to position in variable 'pos'
}
My_Receiver.resume();
}
}
The servo will sometimes come to a stop at 180, but if I enter a smaller value (160) it will open that far and then return to 0. why is that those?
thanks
Earlier (in Reply #1) you said there is a problem if you go to 180, so why are you using that in your test program ? Doesn't make any sense adding one problem on top of another and expecting to find a solution.
The way your code is written it will update the servo even if something other than LEFT_ or RIGHT_ARROW is received - could that be the problem?
I suggest that you print the value that is received immediately before the SWITCH line so that you can see what is being received
Another thought is that the IR and Servo libraries may interfere with each other. It might be worth trying the ServoTimer2 library - just note that it uses millisecs, not degrees.
...R
Hi,
Are you trying to power everything off that 9V Battery? ? ? ?
There's your problem if you are. . .
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Tom....
I am trying to run everything off that 9v battery. I'm using the examples I could find on google to wire a servo and a IR sensor to an Arduino board, powering the servo off the 5v pin and the IR sensor off the 3v. I thought I could run a small servo right off the board...
No, servos can draw an amp when moving. Square 9V battery is only good for 200-300mA.
ok, I have a BEC I can use to supply 5v to the servo...so I need to run one ground to that and another ground back to the board for the signal return?
wired a BEC to supply the servo, common ground on the breadboard, now it's working just fine.
thanks a bunch!
#include <IRLib.h>
#include <IRLibMatch.h>
#include <IRLibRData.h>
#include <IRLibTimer.h>
#include <IRLib.h>
#include <Servo.h>
#define MY_PROTOCOL SONY
#define RIGHT_ARROW 0xDCB47 //Move servo clockwise
#define LEFT_ARROW 0x3CB47 //Move servo counterclockwise
IRrecv My_Receiver(11);//Receive on pin 11
IRdecode My_Decoder;
Servo My_Servo; // create servo object to control a servo
int pos; // variable to store the servo position
int Speed; // Number of degrees to move each time a left/right button is pressed
void setup()
{
My_Receiver.No_Output();//Turn off any unused IR LED output circuit
My_Servo.attach(9); // attaches the servo on pin 9 to the servo object
pos = 0; // start at 0 degrees
Speed = 20; // servo opens/closes when left/right is pushed
My_Servo.write(pos); // Set initial position
My_Receiver.enableIRIn(); // Start the receiver
}
void loop()
{
if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();
if(My_Decoder.decode_type==MY_PROTOCOL) {
switch(My_Decoder.value) {
case LEFT_ARROW: pos=0; break;
case RIGHT_ARROW: pos=160; break;
}
My_Servo.write(pos); // tell servo to go to position in variable 'pos'
}
My_Receiver.resume();
}
}