Skip to end of metadata
Go to start of metadata

You are viewing an old version of this content. View the current version.

Compare with Current View Version History

Version 1 Next »

Objective

The objective of this tutorial is to reproduce the behavior of a mechanism for which we want to limit
the movement using a limit switch. Limit switches are often use to indicate that a mechanism has
reached a certain position, or to automatically stop the motor that moves a mechanism that has
reached the limit of its reach. In simpler terms: we want the motor to stop before is breaks
everything!

Wiring schematics and complete presentation

Code

Download the Arduino IDE .ino file:

This is a transcript of the code:

//**************************************************************************************************************************
// 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

//**************************************************************************************************************************
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
  }
}
//**************************************************************************************************************************
  • No labels