PWM Servo via IR

Apart from the servo write in setup, you're always writing 15 to the servo.

AWOL:
Apart from the servo write in setup, you're always writing 15 to the servo.

I thought that's how I would be able to move the servo 15 degreesor millis every time I hit the button. I'm just tring to use 2 buttons on a remote to move the servo in either direction. I clearly do not have the knowladge to do this and there is nothing similar online that I have found to use for a clear referance.

You update PanServoPos, the write PanServoMovement.
That's going to result in a static servo, because the latter is always 15.

What you want to do is this (not proper code):

new = old + change
move to new

What you're doing is:

new = old + change
move to change

So it stays at the degree position of change, not moving to new.

JimboZA:
What you want to do is this (not proper code):

new = old + change

move to new




What you're doing is:



new = old + change
move to change




So it stays at the degree position of *change*, not moving to *new*.

Thanks Jimbo!!!! I see what you're saying now that you put it like that.
I don't know if you can tell due to the amount of changes I did to it, but I have stated this sketch off of the one you gave me, it really did help me piece together this sketch a lot! Thanks again! I will post the sketch with the changes on it soon.

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


Servo PanServo;
int PanServoPos= (90);
int PanServoMovement = 15;  //how many degrees per move

int irReceiver = 2; 
int IROutput = 10;

IRrecv irrecv(irReceiver);
decode_results results;

void setup() {
  pinMode (IROutput, OUTPUT);
  PanServo.attach(10);
  PanServo.write(0);
  
  irrecv.enableIRIn();
}
    
void loop() {
  

  if (irrecv.decode(&results))  {
    if (results.value == 0x20DFC03F){
  PanServoPos = PanServoMovement + PanServoPos;
  PanServo.write(PanServoPos);
    }
     if (results.value == 0x20DF40BF) { 
      PanServoPos = PanServoMovement - PanServoPos;
      PanServo.write(PanServoPos);
   
    }     
  }
  irrecv.resume();
}

This is what I have, this thing is still not working though.

AWOL, At this point I will take the static... As long as there is some kind of movement. I think I might have to look into putting a PrintIn on this sketch as WildBill has mentioned so I can see what's going on.

Yes, putting some prints to the monitor will be a big help.... like in the "if" you got to when a particular key was pressed, if the servo's not moving, well you don't know if the "if" worked in the first place. So put in a line inside the "if" that sends something to the monitor along the lines of "ok, key was pressed" then at least you know you're inside the "if" and it didn't fly over it. Or blink an led, anything that lets you know you got into the "if".

EDIT: There are a bunch of those printing lines in my original sketch, just put them back and change the message. Don't forget the Serial.begin(9600); at the top.

Also reload the sketch with a different value in the PanServo.write(0); inside setup()... make sure the servo is in fact obeying any commands at all!

I had did that earlier with putting a different value in and it works with no problem. If you don't mind I'm going to put together the PrintIn, if you could please look it over for me. I have not done anything so far with that function. I'm going to look for a tutorial real quick and work off of that.

There are a bunch of those printing lines in my original sketch, just put them back and change the message. Don't forget the Serial.begin(9600); at the top.

Oh, okay! Thanks!

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


Servo PanServo;
int PanServoPos = (90);
int PanServoMovement = (15);  //how many degrees per move

int irReceiver = 2; 
int IROutput = 10;

IRrecv irrecv(irReceiver);
decode_results results;

void setup() {
  pinMode (IROutput, OUTPUT);
  PanServo.attach(10);
  PanServo.write(0);
  Serial.begin(9600);
  
  irrecv.enableIRIn();
}
    
void loop() {
  

  if (irrecv.decode(&results))  {
    if (results.value == 0x20DFC03F){
     PanServoPos = PanServoMovement + PanServoPos;
     PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DFC03F WORKING (LEFT)");
  Serial.println(PanServoPos);
    }
     if (results.value == 0x20DF40BF) { 
      PanServoPos = PanServoMovement - PanServoPos;
      PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DF40BF WORKING (RIGHT)");
  Serial.println(PanServoPos);
   
    }     
  }
  irrecv.resume();
}

I'm not getting anything on the Serial Monitor. Not sure if I set up the Serial.print wrong, or if this is why it's not working.

Put a serial print in the setup too, to say "started" or something, and a few more in loop(), maybe above the "if" so you know it got there.... anywhere where you need a marker that you got to that point.

Did you switch the monitor on in the Arduino ide? (Control + shift + M or the icon top right of ide screen.)

As a matter of interest, how do you know the codes that your keys generate?- did you use that dump sketch or whatever it's called to put them up on the monitor?

Well, I have a different sketch that decodes the IR signals, I think I got that from the "Arduino Cookbook". I use this remote on a different sketch that I have and it works fine with that set up. I just use it to turn on and off relays, much simpler things than this clearly is. Oh and yes, I know how to get into the Serial Monitor, I have the baud set at 9600 as well.

okay, with this sketch the serial monitor is coming up with "STARTED" at least but not responding to the buttons still. So, I would say I have an issue with the IR part of the sketch somewhere.

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


Servo PanServo;
int PanServoPos = (90);
int PanServoMovement = (15);  //how many degrees per move

int irReceiver = 2; 
int IROutput = 10;

IRrecv irrecv(irReceiver);
decode_results results;

void setup() {
  pinMode (IROutput, OUTPUT);
  PanServo.attach(10);
  PanServo.write(0);
  Serial.begin(9600);
  Serial.print("STARTED"); 

  irrecv.enableIRIn();
}
    
void loop() {
  

  if (irrecv.decode(&results))  {
    if (results.value == 0x20DFC03F){
     PanServoPos = PanServoMovement + PanServoPos;
     PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DFC03F WORKING (LEFT)");
  Serial.println(PanServoPos);
    }
     if (results.value == 0x20DF40BF) { 
      PanServoPos = PanServoMovement - PanServoPos;
      PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DF40BF WORKING (RIGHT)");
  Serial.println(PanServoPos);
   
    }     
  }
  irrecv.resume();
}

Hmmmm... then put a print in as shown below to see if there's any IR in the system maybe?

 if (irrecv.decode(&results))  {
       // <<<<=============================put a print here to see if it got to this line
    if (results.value == 0x20DFC03F){

If it doesn't get there, then your IR isn't working at all, if it does, but not the next print, then you're looking for the wrong code...

It's not working at all....

/*Help from JimboZA, AWOL, WildBill & PeterH from Arduino Fourm. Sample sketch provided by JimboZA  */ 
#include <Servo.h> 
#include <IRremote.h>
#include <IRremoteInt.h>


Servo PanServo;
int PanServoPos = (90);
int PanServoMovement = (15);  //how many degrees per move

int irReceiver = 2; 
/*int IROutput = 10;*/

IRrecv irrecv(irReceiver);
decode_results results;

void setup() {
 /* pinMode (IROutput, OUTPUT);*/
  PanServo.attach(10);
  PanServo.write(0);
  Serial.begin(9600);
  Serial.print("STARTED"); 

  irrecv.enableIRIn();
}
    
void loop() {
  

  if (irrecv.decode(&results))  {
    if (results.value == 0x20DFC03F){
      Serial.print("Are you here yet");
     PanServoPos = PanServoMovement + PanServoPos;
     PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DFC03F WORKING (LEFT)");
  Serial.println(PanServoPos);
    }
     if (results.value == 0x20DF40BF) { 
      PanServoPos = PanServoMovement - PanServoPos;
      PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DF40BF WORKING (RIGHT)");
  Serial.println(PanServoPos);
   
    }     
  }
  irrecv.resume();
}

On my other sketch it works fine so this is what I think I'm going to do, I will take the setups and all the if statements and put that into my sketch that this belongs to anyways (that the IR is working with no problems) and see if I can get it working in there.

That print "are u here yet" you added is one line too low... it's inside the if looking for the key press, it's not where I suggested... it should be between the two ifs.

Opps... Sorry about that. However, I'm getting the same results, nothing.

/*Help from JimboZA, AWOL, WildBill & PeterH from Arduino Fourm. Sample sketch provided by JimboZA  */ 
#include <Servo.h> 
#include <IRremote.h>
#include <IRremoteInt.h>


Servo PanServo;
int PanServoPos = (90);
int PanServoMovement = (15);  //how many degrees per move

int irReceiver = 2; 
/*int IROutput = 10;*/

IRrecv irrecv(irReceiver);
decode_results results;

void setup() {
 /* pinMode (IROutput, OUTPUT);*/
  PanServo.attach(10);
  PanServo.write(0);
  Serial.begin(9600);
  Serial.print("STARTED"); 

  irrecv.enableIRIn();
}
    
void loop() {
  

  if (irrecv.decode(&results))  {
  Serial.print("Are you here yet");
    if (results.value == 0x20DFC03F){
     PanServoPos = PanServoMovement + PanServoPos;
     PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DFC03F WORKING (LEFT)");
  Serial.println(PanServoPos);
    }
     if (results.value == 0x20DF40BF) { 
      PanServoPos = PanServoMovement - PanServoPos;
      PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DF40BF WORKING (RIGHT)");
  Serial.println(PanServoPos);
   
    }     
  }
  irrecv.resume();
}

My bright idea of putting it on the other sketch didn't work either. I will have to look this over in some detail in the morning and see if I can find where I went wrong. I don't think the if statements are correct. This is the entire sketch with all the other working things on there along with what I have added in this sketch. It's a giant mess, but it does work... Well other than this.

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


Servo triggerservo;
Servo PanServo;
             
 int pos = 0;     
 int button = 7;   //<---- Nothing in pin (DO NOT USE PIN 7)
 
 const int laserButton = 8;  
 const int irReceiverPin = 2; 
 int Saftey =5;
 int RelayTrig = 4;
 int RelayLaser = 3;
 
 int PanServoPos = (90);
 int PanServoMovement = (15);
 
 IRrecv irrecv(irReceiverPin); 
 decode_results results; 

 
 void setup() 
{ 
  triggerservo.attach(9);  
 pinMode(pos, OUTPUT);
 pinMode(button, INPUT); 
 digitalWrite (button, LOW);
 
  PanServo.attach(10);
 PanServo.write(0);
 Serial.begin(9600);
 Serial.print("STARTED");
 
 pinMode (Saftey, OUTPUT);
 pinMode(RelayTrig, OUTPUT);
 pinMode(RelayLaser, OUTPUT);
 irrecv.enableIRIn(); 
}
 
 int on = 0;
 unsigned long last = millis();
 
 
 void loop() 
{ 
  
    if (digitalRead(button) == LOW)

  for(pos = 0; pos < 90; pos += 90)   //
  {                                   
    triggerservo.write(pos);               
                 
  } 
  if (digitalRead(button) == HIGH) 
  
  for(pos=90; pos>=0; pos-=90)      //-----> Original
  {                                
     triggerservo.write(pos);               
                                
                           
  }
{
  if (irrecv.decode(&results)) {    
    if (results.value == 0x20DFC23D) { //Menu Button: pulls/releases trigger only if saftey is off (LED on when released).
      if (millis() - last > 250) {
        on = !on;
        digitalWrite(RelayTrig, on ? HIGH : LOW);
      }
      last = millis();
    }    
  }
  
  if (irrecv.decode(&results)) {    
    if (results.value == 0x20DF10EF) { //Power Button: Turns on/off the saftey on the trigger 
      if (millis() - last > 250) {
        on = !on;
        digitalWrite(Saftey, on ? HIGH : LOW);
      }
      last = millis();
    }    
   
  }
{
  if (irrecv.decode(&results)) {
    if (results.value == 0x20DF906F) { //Mute Button: Turns on/off the targeting laser.
    if (millis() - last > 250) {
        on = !on;
        digitalWrite(RelayLaser, on ? HIGH : LOW);
     }
      last = millis();
      
      
    }    
   
}
}
  }
   if (irrecv.decode(&results))  {
  Serial.print("Are you here yet");
    if (results.value == 0x20DFC03F){
     PanServoPos = PanServoMovement + PanServoPos;
     PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DFC03F WORKING (LEFT)");
  Serial.println(PanServoPos);
    }
     if (results.value == 0x20DF40BF) { 
      PanServoPos = PanServoMovement - PanServoPos;
      PanServo.write(PanServoPos);
  Serial.print(PanServoMovement);
  Serial.print("20DF40BF WORKING (RIGHT)");
  Serial.println(PanServoPos);
   
    }     
  }
   irrecv.resume(); 
}

It's morning here and I've been at work a couple hours from 6 my time.... when I get home I'll see if you've made any headway and if not load your sketch and see if it works for me. I'll change the key codes to codes that I know my remote generates of course, and see if I get into the right part of the sketch.

All good clean fun eh? 8)