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:
//************************************************************************************************************************** // CrcDuino Tutorial 1 - Limiting a motor using a limit switch //NOTE: Continuous Servos often have a calibration screw on their side. // When sending a "0" speed command, adjust it so that the motor doesn't run //************************************************************************************************************************** #include <CrcLib.h> //CrcLib program requirement //Rename IOs with 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)) ) //When the button is pressed (BND is connected to the input) { if( CrcLib::GetDigitalInput(LS1_SERVO_CONT_1) ) //But only if the limit switch is not pressed (5V applied to the input) { 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 } } //**************************************************************************************************************************
Add Comment