Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagecpp
//**************************************************************************************************************************
// CrcDuino Tutorial 1 - Limiting a motor using a limit switch
//NOTE: Continuous Servos often have a calibration screw on their side.
//      If you're sending a "0" command to the servo, bu it still runs, turn the screw until the motor stops.
//      This calibrates the neutral signal of the servo, telling it "Ok buddy, the signal you're receiving corresponds to
//      a stop command ".
//**************************************************************************************************************************
#include <CrcLib.h>  //CrcLib program requirement

//Rename IOs with a meaningful name (good practice)
#define SERVO_CONT_1 CRC_PWM_5
#define BTN_RUN CRC_DIG_1
#define LS1_SERVO_CONT_1 CRC_DIG_2

using namespace Crc; //CrcLib program requirement
//**************************************************************************************************************************
void setup() {
  // put your setup code here, to run once at the beginning:
  CrcLib::Initialize(); //Sets up the CrcDuino
  
  CrcLib::InitializePwmOutput(SERVO_CONT_1); //Sets up the motor output
  CrcLib::SetDigitalPinMode(BTN_RUN, INPUT); //Sets up the digital port as an Input
  CrcLib::SetDigitalPinMode(LS1_SERVO_CONT_1, INPUT);
}
//**************************************************************************************************************************
void loop() {
  // put your main code here, to run repeatedly:
  CrcLib::Update(); //Refreshes the CrcDuino

  if( CrcLib::GetDigitalInput(BTN_RUN) == LOW )  //When the button is pressed (GND is connected to the input, so CrcLib::GetDigitalInput() returns LOW)
  {
    if( CrcLib::GetDigitalInput(LS1_SERVO_CONT_1) == HIGH )  //But only if the limit switch is not pressed (5V applied to the input, so CrcLib::GetDigitalInput() returns HIGH)
    {
      CrcLib::SetPwmOutput(SERVO_CONT_1, 127);  //127=Full speed
    }
    else  //when the limit switch is pressed...
    {
      CrcLib::SetPwmOutput(SERVO_CONT_1, 0);  //0=stop
    }
  }
  else  //when the button is not pressed...
  {
    CrcLib::SetPwmOutput(SERVO_CONT_1, 0);  //0=stop
  }
}
//**************************************************************************************************************************