Help with the Servo

Lower Canada College

Published on 2019-11-27

Good afternoon everyone,

We are having some problems with the code for the 9880. What we are trying to do is simply make an attached servo move to test the connection. However, as you can imagine, we were unsuccessful. Due to the wiki being incomplete, it wasn't the easiest task to figure out how to do that simple of a task. Anyways, anything wrong with the code? When I run it, the Arduino just makes a constant buzzer noise with no pattern, furthermore complicating the situation as the error we're experiencing is undocumented. In addition, is it possible to have a code snippet posted in order to achieve our goal? 

- Bogdan


Code made to run servo:

#include <CrcLib.h>

using namespace Crc;

#define  MOTOR CRC_PWM_5


void setup()

{

    CrcLib::Initialize();


    CrcLib::InitializePwmOutput(CRC_PWM_5);


    CrcLib::SetDigitalPinMode(CRC_DIG_4, OUTPUT);


    //Not necessary

    Serial.begin(9600);

}


void loop()

{

    CrcLib::SetPwmOutput(CRC_PWM_5, 50);

    delay(10000);

    //Not necessary

    unsigned int deltaMicros = CrcLib::GetDeltaTimeMicros();

    Serial.println(deltaMicros);

}


/*void Test(unsigned char pin, BUTTON button1, BUTTON button2){

  if(CrcLib::ReadDigitalChannel(button1) || CrcLib::ReadDigitalChannel(button2){

    CrcLib::SetDigitalOutput(pin, HIGH);

  }

  else{

    CrcLib::SetDigitalOutput(pin, LOW);

  }

} */

Answers

Marco Purich

Published on 2019-11-27

Hi Bogdan, it looks like there might be a few issues with the code that you provided.

Firstly, as you can see in the documentation, Update must be called in the loop function. This is probably why the buzzer starts and never stops, because the library is being initialized, but never updated.

Secondly, you might also have some issues with timing and delays. The line:

delay(10000);

will cause the program to flat out stop for 10 seconds. We strongly recommend AGAINST using the delay Arduino function, because it's not the proper way to handle timing and coordination, and can easily lead to issues.

Aside from that, maintaining the serial print messages are good for debugging, but consider commenting them out if you have any issues with speed or performance.

Something that I noticed is that you define "MOTOR" but then never use it. The principle of using defines like that is so that you can replace a more complicated symbol with one that's easier to remember. So as you have it, you could have used "MOTOR" instead of "CRC_PWM_5" in the initialize and set function calls.

(I also see that you never call your test function, which I assume is intentional since it's commented, but just in case, in order for that stuff to run, it will have to be called from somewhere, likely your loop)

Hope that answers your questions!