PWM Servo via IR

I need some help on this portion of my project. I'm having a hard time trying to figure out where to start with this one. I just need a little guidance, I'm NOT looking for just the answer or a copy of a sketch, just a little help to know what direction I should be going in.

Now I have a servo controlling a platform that will move 180 degrees. I have no problem having it set up with a pot, but I would love to have it wireless like the rest of the project is. I have other servos on this project that move in the action that I need them to but it's a simple sweep from 0 degrees to 90 and then back to 0. However this one I want it to move 5 degrees every time I hit a button (the same button) on an IR remote. I'm assuming that you have to be able to control the timing of the PWM but I have no clue when to start to do that using a single button expecting different values with each touch of the button. Am I on the right path and should I be using the "map" function or "if" in the void loop? Any info someone can give (without telling me the answer) would be great! Thanks in advanced!

Thank you,
Peter

Er,

if <button pressed>
  degrees = degrees + 5;

Or have I not understood the question?

I had a lot of questions in there due to the pure state of confusion I'm in. Well at least I'm on the right track because, I was leaning more twords if, even though I had it working with a pot and the map function. I'm going cross eyed right now from looking at this screen tonight, so I think I will pick up where I left off in the morning.

One more question though, if I have it going clockwise with the +5 degrees on one button. Than ishould be able to just change a different button to -5 for it to go counter Clockwise correct?

Sure. Try it and see, that's what I usually do.

I've programmed a bunch of TV remote buttons to drive a robot. Not a servo admittedly, but one with two dc motors. Various buttons will:

  • Speed up or slow down
  • Emergency stop
  • Select direction forward or reverse
  • Turn left and right.

So in the same way, you'll just have something like this (borrowing from Nick):

if <button pressed>
  degrees = degrees + 5;

if <different button pressed>
  degrees = degrees - 5;

I just tried adding degrees into the void loop and it's not doing the trick. What about changing the pulse? I do that by adding milli to the void loop section of the sketch right?

Peter9DO:
I just tried adding degrees into the void loop and it's not doing the trick. What about changing the pulse? I do that by adding milli to the void loop section of the sketch right?

Not sure what that means, but your best bet at this point would be to post your code.

Pete have you read and implemented the servo tutorial? It's halfway down the right hand side of the Arduino tutorial page.

You use a servo library to do all the hard work behind the scenes and just use simple commands like

myservo.write(val);

where val is where you want it to go to.

Here is the code....

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

Servo PanServo;

int RECV_PIN = 2;
int OUTPUT_PIN = 10;
int val;


IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  PanServo.attach(10);
  pinMode(OUTPUT_PIN, OUTPUT);
  
  irrecv.enableIRIn(); 
}

int on = 0;
unsigned long last = millis();

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 0x20DFC03F) {
      degrees = degrees + 5;               
        digitalWrite(OUTPUT_PIN, val = 0);
      }
      last = millis();
    }   
      if (irrecv.decode(&results)) {
    if (results.value == 0xDF40BF) { 
      degrees = degrees -5;
        digitalWrite(OUTPUT_PIN, val = 1023);
      }
      last = millis();
    }   
    irrecv.resume();
  }

  }
}

Did you mean
digitalWrite(OUTPUT_PIN, val == 0); ?

Wait a minute - you've got the servo attached to that pin - why are you doing a digitalWrite to it?

You should have something like this:

PanServo.write(degrees);

JimboZA:
You should have something like this:

PanServo.write(degrees);

Okay, I will give her a shot!

Peter9DO:
Okay, I will give her a shot!

Cool, but have a close look at the tutorial if you didn't already.

Matter of interest, I used ifs to do my IR buttons too, but am (even as we speak) re-writing that into switch case because it's much neater imo.

JimboZA:

Peter9DO:
Okay, I will give her a shot!

Cool, but have a close look at the tutorial if you didn't already.

Matter of interest, I used ifs to do my IR buttons too, but am (even as we speak) re-writing that into switch case because it's much neater imo.

Are you talking about the tutorial on ifs?

No, the servo tutorial- see reply #7

Peter9DO:
Here is the code....

You are calling irrecv.decode() twice, once for each possible command. You should only need to call it once, and then test the result against your known values to see what command you received.

The point of int OUTPUT_PIN = 10; is that you only need to define the pin number in one place. It kind of defeats the point if you then put PanServo.attach(10);.

I don't know what you intend to do with last, but you aren't doing it. (I would guess this is to control the speed of movement if the button is pressed continuously?)

You don't seem to declare degrees and you don't seem to use val. Instead of digitalWrite(OUTPUT_PIN, val = 1023); you should be using PanServo.write(degrees);.

I have in the past, but not in regards to this question. I will check it out now.

I'm writing this sketch to add to my original project. I have a servo currently on it that runs a simple motion using a single command from an IR signal. Hit the button it goes HIGH hit it again and it goes LOW. I did it off of a relay. It's just trying to get this to move in steps using 2 buttons (one for clockwise and the other for counter-clockwise). Anyways, I will check out the tutorial now and see what I can come up with. I have looked around for a sample sketch, found somethings similar but just doesn't seem to be the easiest way to do it.

Also, today I decided to go buy another Arduino Uno in the event I want to make my life a living hell and make my own remote, instead of using a regular TV remote... What can I say, a pot just feels right when controlling pan and tilt servos, wouldn't you agree?

PeterH:

Peter9DO:
Here is the code....

You are calling irrecv.decode() twice, once for each possible command. You should only need to call it once, and then test the result against your known values to see what command you received.

The point of int OUTPUT_PIN = 10; is that you only need to define the pin number in one place. It kind of defeats the point if you then put PanServo.attach(10);.

I don't know what you intend to do with last, but you aren't doing it. (I would guess this is to control the speed of movement if the button is pressed continuously?)

You don't seem to declare degrees and you don't seem to use val. Instead of digitalWrite(OUTPUT_PIN, val = 1023); you should be using PanServo.write(degrees);.

I threw this up as I was in the middle of working on it. I didn't define degrees as of yet because I didn't really know how to go about doing it with the values I needed. As I said earlier in this thread, I'm so confused on how to make this work. I think at this point I'm being my own worst enemy and making this be much harder than it needs to be. I'm just going to start over with a fresh sketch instead of taking parts from my main project sketch then just merge them together. Thanks for the help, I just need to figure out where to start with this one. I'm noticing that it's nothing like writing a sketch for just a button read, swing servo.

Maybe I should just skip the IR part for now and try to get it to work on just the servo and then put the IR in there. Just take one step at a time. I feel overwhelmed and the confusion is killing me!

Hang in there!

Have a look at the following code, which essentially does exactly what you want, except my "if" is to see if a Light Dependent Resistor is under a certain threshold. If it is, the servo moves the ldr (which is stuck on the horn) to try find some light. So my "if" is analaogous to your "if" which is the remote key press. My servo moves by an increment "JimServoClick" each time the "if" says it's still dark- in your case, a new press of the key.

Only remotely clever thing here is changing the sign of JimServoclick at the ends of the servo's travel so it changes direction.

/* Idea here is to have LDR mounted on servo horn
     and have servo sweep when LDR reads dark, to 
     move LDR so it finds a light area

*/

// sort the servo
#include <Servo.h> 
Servo Jimservo;
int JimservoPos= 90;
int JimservoClick = 15;  //how many degrees per move


//sort the LDR
int LDRpin = A0;
int LDRvalue;
int LDRthreshold = 500; //value under which it seeks

void setup() {
  pinMode(LDRpin, INPUT);
  Jimservo.attach(3);
  Jimservo.write(90);
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
}
    
void loop() {
  

  if (LDRvalue < LDRthreshold)   //then it's dark...
{
  Jimservo.write(JimservoPos);
  Serial.print(JimservoClick);
  Serial.print("\t");
  Serial.println(JimservoPos);
  JimservoPos = JimservoPos + JimservoClick;
  JimservoPos = constrain(JimservoPos, 0, 180);  
    if (JimservoPos < 2 || JimservoPos > 178)  //change direction
  {
    JimservoClick = JimservoClick * (-1);
    Serial.println("Changing direction");
  }
  
} 
  
}

Pretty sure that should get you back on track?

The Fritzing pic shows the setup... it's bad form to drive the servo from the Arduino though, in a later version I have the servo on its own supply with the servo and Arduino grounds linked.

Thanks Jimbo! I'll use this for an outline and see what I can come up with. You have a lot of items in there that I'm not to sure about, so getting a firm grasp on how/why this sketch works will be a project in it's own but I'm sure I will learn plenty from it! I will post my findings in the morning some time! Again, THANK YOU VERY MUCH!!!!