PWM Servo via IR

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)

Yea, something is really screwed up with this. My other sketch isn't working at all once I add this to it I will continue my struggle and see what I can come up with on it. I really believe the problem lies within the if statements and the way it was set up. I have my other servo sketch running off of millis and I don't think you can add degrees in there once you use millis, it's either one or the other.

Well, now that I know the trick with the Serial.Print this should make life a little easier and not so much of a guess. Again, thank you everyone for your help thus far, have a goodnight or good morning, or good day depending on where you are! Jimbo, thanks a lot for the help brother, I would still be here pulling my hair out without man! Enjoy work, I'm going to get some sleep. I'll check back in the AM!

Hi.
Interesting thread...
I know that I don't have the level of knowledge that you guys have :frowning:
Might I suggest that you get the IR bit working with Nothing else.. Just the Serial.print to display results.
I have never used Ir so you have lost me in that area.
Will watch this thread with interest ( more like a learning curve).

Regards Antony

I think I'm making some headway.... Still working on it though. I'll update later.

Well I just got home after a loooooong day (and week).... gna help my daughter re-assemble her Magician chassis which is a total load of crap cos the motor holes don't line up and 3 of the 4 motor wires have snapped off.

NICE! You know they don't make anything like they used to! lol! Good luck with that!!! I'm still plugging away over here, I think I might have figured this out... I'll post on it once I finish up what I'm doing to see if it will work. I'm crossing my fingers, praying to god, Buddha and Tom Cruse that this will work!

So, I got the Serial.print to read the "Are you here yet" and the directional IR signals!!!!! The servo however is not moving yet, but I'm getting there!

#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 irReceiverPin = 2; 
 int Saftey =5;
 int RelayTrig = 4;
 int RelayLaser = 3;

 int PanServoVal;
 int PanServoMovement = (15);
 int PanServoPos = (0);
 
 IRrecv irrecv(irReceiverPin); 
 decode_results results; 

 
 void setup() 
{ 
  triggerservo.attach(9);  
  PanServo.attach(10);
  
 pinMode(pos, OUTPUT);
 pinMode(button, INPUT); 
 digitalWrite (button, LOW);
 
pinMode (Saftey, OUTPUT);
pinMode(RelayTrig, OUTPUT);
pinMode(RelayLaser, OUTPUT);
irrecv.enableIRIn(); 

Serial.begin(9600);
Serial.print("!!STARTED!!");

PanServo.write(PanServoVal);
}
 
 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)     
  {                                
     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);
                Serial.print(" TRIGGER ACTION ");
        
      }
      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);
        Serial.print(" SAFTEY ACTION "); 
      }
      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);
        Serial.print(" LASER ACTION ");
     }
      last = millis();
    }    
     if (irrecv.decode(&results))  {
  Serial.print("Are you here yet");
  
    if (results.value == 0x20DFC03F){
     PanServoVal = PanServoMovement + PanServoPos;
     PanServo.write(PanServoVal);
  Serial.print("20DFC03F WORKING (LEFT)");
 
    }
     if (results.value == 0x20DF40BF) { 
      PanServoVal = PanServoMovement - PanServoPos;
      PanServo.write(PanServoVal);
  Serial.print("20DF40BF WORKING (RIGHT)");

   
    }     
    irrecv.resume(); 
}
}
  }
}
}

In the sketch above the "Are you here yet" is in there but I have taken that out because it is working and that line is coming up every time I hit a button. I found it to be annoying so I took it out...

I took a look at the signal going to the servo with my o-scope and AWOL is 100% correct, I got a ton of noise on there. Now, when I reset the Arduino or I load the sketch it moves to the 120 degree position. then if I hit either directional buttons it will move to the 90 degree position. After that, I get no response from the servo. I don't know if this is from my poor code writing skills or the noise. I figure I can hook up an LM7805 with some caps to filter it out, however I don't know if that will do anything if it's because of my code as AWOL has claimed above.

Well, I figured out why the PanServo was not working. It wasn't getting enough power. I hooked it up to an external power supply and it works just fine now. But, with the "if" statements that I have it's going all the way to one side with a single press of a button and then all the way to the other side with the other button, single pressed. I'm going to try and get the math down right on it so it will move 15 degrees every time I hit a button and maybe I will go nuts and program a swing in there along with a stop.