Menu

SERVO in use other pins ?!?

Features
Anonymous
2012-06-22
2012-10-29
  • Anonymous

    Anonymous - 2012-06-22

    Hi,

    I Like to use the pins 4,5,6,7 for input pins at the example "ServoIn". But i don't know how to modify the example code. It is not working. Can you please help?

    Here you will find the modified code:

    /* ---------------------------------------------------------------------------
    ** This software is in the public domain, furnished "as is", without technical
    ** support, and with no warranty, express or implied, as to its usefulness for
    ** any purpose.
    **
    ** servoin_example.pde
    ** Demonstrate Servo Signal Input functionality
    **
    ** Author: Daniel van den Ouden
    ** Project: ArduinoRCLib
    ** Website: http://sourceforge.net/p/arduinorclib/
    ** -------------------------------------------------------------------------*/
    
    #include <ServoIn.h>
    #include <Timer1.h>
    
    #define SERVOS 4
    
    uint16_t g_values[SERVOS];                    // output buffer for ServoIn
    uint8_t  g_workIn[SERVOIN_WORK_SIZE(SERVOS)]; // we need to have a work buffer for the ServoIn class
    
    rc::ServoIn g_ServoIn(g_values, g_workIn, SERVOS);
    
    void setup()
    {
        // Initialize timer1
        rc::Timer1::init();
    
    Serial.begin(9600);
    
        // We use pin 4-7 as Servo input pins
        pinMode(4, INPUT);
        pinMode(5, INPUT);
        pinMode(6, INPUT);
        pinMode(7, INPUT);
    
        // only allow pin change interrupts for PB20-23, digital pins 4-7.
        PCMSK2 = (1 << PCINT20) | (1 << PCINT21) | (1 << PCINT22) | (1 << PCINT23);
    
        // enable pin change interrupt 0
        PCICR = (1 << PCIE2);
    
        // start listening
        g_ServoIn.start();
    }
    
    void loop()
    {
        // update incoming values
        g_ServoIn.update();
    
        // handle servo values here, stored in g_values
            for (int i = 0; i < SERVOS; i++) {
              Serial.print(" Kanal: ");
              Serial.print(i);
              Serial.print(" = ");
              Serial.print(g_values[i]);
            }
              Serial.println("   ");
    }
    
    // Interrupt handling code below, this needs cleaning
    
    static uint8_t lastB = 0;
    
    // Pin change port 0 interrupt
    ISR(PCINT2_vect)
    {
        uint8_t newB = PIND;
        uint8_t chgB = newB ^ lastB; // bitwise XOR will set all bits that have changed
        lastB = newB;
    
        if (chgB)
        {
            if (chgB & _BV(0))
            {
                g_ServoIn.pinChanged(0, newB & _BV(0));
            }
            if (chgB & _BV(1))
            {
                g_ServoIn.pinChanged(1, newB & _BV(1));
            }
            if (chgB & _BV(2))
            {
                g_ServoIn.pinChanged(2, newB & _BV(2));
            }
            if (chgB & _BV(3))
            {
                g_ServoIn.pinChanged(3, newB & _BV(3));
            }
        }
    }
    
     

    Last edit: dvdouden 2012-10-29
    • dvdouden

      dvdouden - 2012-10-29

      Sorry for the late reply, been busy :)

      Your problem is in the ISR, you've set up the interrupts correctly but you need to check for the pin in the interrupt handler. Currently you're checking for bits 0,1,2 and 3 on port D, which maps to pins 16,17,18 and 19. However you're using pins 20, 21, 22 and 23; those are in bits 4,5,6 and 7! It's my mistake for not using defines in the interrupt handler that you missed it.

      Here's what the ISR should look like in your situation:

      // Interrupt handling code below, this needs cleaning
      
      static uint8_t lastD = 0;
      
      // Pin change port 2 interrupt
      ISR(PCINT2_vect)
      {
          uint8_t newD = PIND;
          uint8_t chgD = newD ^ lastD; // bitwise XOR will set all bits that have changed
          lastD = newD;
      
          if (chgD)
          {
              if (chgD & _BV(PCINT20))
              {
                  g_ServoIn.pinChanged(0, newD & _BV(PCINT20));
              }
              if (chgD & _BV(PCINT21))
              {
                  g_ServoIn.pinChanged(1, newD & _BV(PCINT21));
              }
              if (chgD & _BV(PCINT22))
              {
                  g_ServoIn.pinChanged(2, newD & _BV(PCINT22));
              }
              if (chgD & _BV(PCINT23))
              {
                  g_ServoIn.pinChanged(3, newD & _BV(PCINT23));
              }
          }
      }
      
       

      Last edit: dvdouden 2012-10-29
  • Anonymous

    Anonymous - 2017-09-21
    Post awaiting moderation.

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.