Jump to content
nnielzz

IR signal will play a different command

Recommended Posts

Hi,
I am quite new with launchpad and Energia and I am trying to code that when I point with my remote control at the IR receiver my two Servo motors will go to a specific point and repeat over and over again, but when I press the same button again they will stop moving. So I got 5 buttons on my small remote control and I want them to each play a different command. I already made something, but the problem with that is that the servo's won't stop moving. Can someone please help me with this? :)

 

  • #include <IRremote.h>
    #include <Servo.h>
     
    Servo motorA; //the name of the servo motor
    Servo motorB;  
    IRrecv irrecv(7); // the receiver connected with pin 7
    decode_results results; 
     
    void setup()
    {
      Serial.begin(9600);  //serial communication
      irrecv.enableIRIn(); // start the receiver
      pinMode(GREEN_LED,OUTPUT);
      digitalWrite(GREEN_LED,HIGH);
      digitalWrite(RED_LED,HIGH);
      motorA.attach(13); // connect the servo with pin 13 and 10
      motorB.attach(10);
    }
     
    void loop() 
    {
      if (irrecv.decode(&results)) { 
        Serial.print("IR code: ");
        Serial.println(results.value, HEX); 
     
        int key = results.value % 16;  
        Serial.print("key: "); 
        Serial.println(key); 
     
        switch (key)
        {
          case 2:
            doKey2();
            break;
          case 3:
            doKey3();
            break;
          case 4:
            doKey4();
            break;
          case 7:
            doKey7();
            break;
          case 8:
            doKey8();
            break;
      }
  • }
    }
     
      void doKey2(){ //DOWN button on remote control
      digitalWrite(GREEN_LED,LOW); 
        delay(300);           
        digitalWrite(GREEN_LED,HIGH); 
        motorA.write(0);
        motorB.write(160);      
    }  
     
    void doKey3(){ //STROBE button on remote control
      digitalWrite(GREEN_LED,LOW);  
        delay(300);           
        digitalWrite(GREEN_LED,HIGH); 
        motorA.write(88);
        motorB.write(126);    
        delay(400);              
        motorA.write(10);
        motorB.write(120);          
    }  
     
    void doKey4(){ //SPEED button on remote control
      digitalWrite(RED_LED,LOW);  
        delay(300);           
        digitalWrite(RED_LED,HIGH); 
        motorA.write(49);
        motorB.write(160);    
        delay(1000);               
        motorA.write(13);
        motorB.write(93);  
    }  
     
    void doKey7(){ //SELECT button on remote control
      digitalWrite(GREEN_LED,LOW);  
        delay(300);           
        digitalWrite(GREEN_LED,HIGH); 
        motorA.write(90);
        motorB.write(179);  
        delay(800);              
        motorA.write(138);
        motorB.write(5);  
    }  
     
    void doKey8(){ //UP button on remote control
      digitalWrite(RED_LED,LOW);  
        delay(300);           
        digitalWrite(RED_LED,HIGH); 
        motorA.write(78);
        motorB.write(120);   
        delay(300);               
        motorA.write(130);
        motorB.write(12);    
    }  
     
    void flashLed(int nr)
    {
      Serial.print("Flashled: "); 
    }

Share this post


Link to post
Share on other sites

Have you tried the Detach command?  Looks like that will stop the pulse to the servo. You will need to add some tracking to monitor duplicate button pushes as well - I don't see that in your current code. 

Are the servos moving back and forth, or just twitching?  

Share this post


Link to post
Share on other sites

Have you tried the Detach command?  Looks like that will stop the pulse to the servo. You will need to add some tracking to monitor duplicate button pushes as well - I don't see that in your current code. 

Are the servos moving back and forth, or just twitching?  

No,I haven't tried that one yet. Could you give me a example of this please :)? Also at the moment the servos are moving back and forth, they are going from position 1 to position 2, and since they're 180 degrees servo motors, they can only move from 0 to 180.

Share this post


Link to post
Share on other sites

The command would just be motorA.detach();

If you're seeing your servos moving back and forth - there's a good chance you're running into a code issue.  I'd probably start another energia project and start with basics and work your way back up.  It helps you isolate problems.  It also may be helpful to add some serial debugging.

One item I noticed - once you're done receiving an IR signal - you're supposed to call irrecv.resume();  I'm wondering if that might be causing the same signal to be received over and over - causing repeated calls.  

 

If that doesn't get you anywhere - could you let me know what exactly you're trying to achieve? That will help me analyze your code to make sure it's mapping up with your logic.

Share this post


Link to post
Share on other sites

The command would just be motorA.detach();

If you're seeing your servos moving back and forth - there's a good chance you're running into a code issue.  I'd probably start another energia project and start with basics and work your way back up.  It helps you isolate problems.  It also may be helpful to add some serial debugging.

One item I noticed - once you're done receiving an IR signal - you're supposed to call irrecv.resume();  I'm wondering if that might be causing the same signal to be received over and over - causing repeated calls.  

 

If that doesn't get you anywhere - could you let me know what exactly you're trying to achieve? That will help me analyze your code to make sure it's mapping up with your logic.

 I need to make this for a school project so I can't really stop with this project and start another one :/. The second thing is that I removed irrecv.resume(); on purpose; I wanted it to loop the command but that doesn't really work when I had  irrecv.resume(); in the code, so I removed it.

 

To explain what I want to achieve:

So I got 2 Servos and 1 IR receiver.

The 2 Servos are on pin 13 and 10.

The IR receiver is on pin 7.

 

I got a remote control and I want to code it that when I press a button on the remote control, the Servos will start going to a point(degrees) from 0-180 in a loop.

Every single button on the remote control will let the Servos go to a point and repeat it. The servos will never stop moving until I press a button which only can stop the Servos from moving, but I should still be able to press the buttons after it stopped.

 

If my plan is to difficult to achieve I'd like to do the following:

 

When I press on a button on the remote control, it doesn't matter which one, the Servo's will start moving repeatedly. Once I click on a button again, the servo's will stop moving.

See it as a 'Pause' and 'Play' command.

 

Please help me out :) 

Share this post


Link to post
Share on other sites

you do need ir.resume() else it won't pick up the new command.

 

You can easily hop back and forth between 2 energia sketches too while you isolate issues.

 

 

I'd actually take a slightly altered approach from your code. Have a variable to store the current mode, in loop() a switch statement executes the right action for each mode (with a stop mode being included). Then when IR data is available, set the mode variable accordingly, if you are in a mode already, switch to the stop mode else go to whichever one the IR command dictates.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...