PWM Servo via IR

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.

Great stuff...

I think you have the movement figures a bit screwed. You don't need PSVal and PSPos as you have:

PanServoVal = PanServoMovement + PanServoPos;
     PanServo.write(PanServoVal);

PSMovement is always 15 and PSPos stays at 0 so you're always writing the same value I think. Debug it with some serial write of PSVal which is what the servo's getting and I think you'll see it doesn't change.

Try something like this:

PanServoPos = PanServoMovement + PanServoPos;
     PanServo.write(PanServoPos);

where now the written value changes each time thru.

BTW wth is this thing you're building... lasers? triggers?

Am I aiding and abetting some kind of Inter-Galalctic Star Wars thing here? 8)

My daughter is really keen to mount some kind of weaponry on her Magician. She's thinking Airsoft, I'm thinking more ping pong balls

Yea, I was viewing some tutorials and reading through some blogs, fourms and webpages while I was working hard at work today, lol. I saw something in there that I kind of liked for the math part of it. I will post it in the AM though, if you don't mind me picking your brain a little more, could you tell me if something like this would work in a sketch like this and why or why not it would or would not work. I'm trying to learn as much as I can and people that I know with the acception of one person looks at me like I have 3 eyes when I talk about anything related to my project.

Again, I can't thank you enough for all your help and patients with me. You're AWESOME and I hope to absorbe enough knowladge to be proficient in programming, to help people in the future. I have no formal background in any kind of electronics. However, due to my interests in them, I have come a long way since 2007. I find it to be very satisfying that I am self taught and people like you have been giving me the guidance in new ventures since I have started working on desktop computers back then, to BGA welding, and todays Arduino/C Programming . If it wasn't for people like you, I wouldn't be able to have a very nice extra income monthly, or extend my hobbies like this. I truly am greatful for your help, more than I can express. Thank you for everything. If you ever have any questions regarding PCs or re-flowing/ re-balling BGAs please let me know, I work on everything from Tvs to portable devices and if you ever want to run something by someone please let me help you, as payment for helping me with programming.

My 4 month old son woke up so I'm going to have to have to get at you in the AM! Thanks again!

Well, I have always enjoyed shooting guns. Back in 2006 I had a unfortunate accident (cleaning up tree debris after hurricane Katrinain New Orleans) and I lost my right index finger so I have a problem shooting smaller handguns. I'm building this "IR turret" for a .25 caliber handgun that has been collecting dust since my accident. Honestly, the only reason why I started this is because I was loosing interest in learning how to do programming by making LEDs blink. So, I went looking around the house for something to make that would be very rewarding at the end of the project. I stubbled accross my hand gun collection and said "a turret for my. 25 would be pretty cool". Now, a month and a half later this is where I'm at. If you want to give me your email address, I will send you some pictures and or videos of my progress thus far (in the AM). I have removed the firing pin from the gun so it's safer until I get the project done and I will re-assemble the gun and bring it out to our land in the sticks, stand a good safe distance (hence the IR), and bangoff some rounds! I can't tell you how the anticipation of that day is just eating at me now. It has become an addiction, working on this with every spare moment I have. Lol! I have learned more in the month and a half, since the start of this project than I have playing with LEDs for the 3 months prior.

Please tell me you're not trusting firearm safety to your programming skills