////////////////////// // Global variables // ////////////////////// // pos_result is one of many volatile unsigned int pos_result; ... ////////////////////////////////////////////////////////////// // In main(). // // If button press to drive motor down -> move_down(), etc. // ////////////////////////////////////////////////////////////// // Position variables hardcoded for testing purposes. // Customizable in final application volatile unsigned int current_position; volatile unsigned int favorite_position = 250; volatile unsigned int max_position = 575; volatile unsigned int min_position = 68; ... if(rf_input_buffer == RF_CLOSE){ //Command routine move_closed(max_position); } else if(rf_input_buffer == RF_OPEN){ //Command routine move_favorite(favorite_position); } else if(rf_input_buffer == RF_UP){ //Command routine move_up(max_position); } else if(rf_input_buffer == RF_DOWN){ //Command routine move_down(min_position); } ... #pragma vector = ADC10_VECTOR __interrupt void ADC_ISR(void) { //Decrement ADC read adc_count--; ... //Saving other channels ... //Save A1, Potentiometer input else if(adc_count == 1) && position_check){ //Save potentiometer position position_result = ADC10MEM0; //Enable conversion of next channel ADC10CTL0 |= ADC10SC; } //Reading channel that else{ //Enable conversion of next channel ADC10CTL0 |= ADC10SC; } } ///////////////////////////// // Move routines for motor // ///////////////////////////// void move_down(unsigned int min_position) { //Detect current position current_position = pos_read(); //Limit check, and button held while((current_position > min_position) && (PJIN_RFIN & RX_D1)){ //Drive motor downwards P2OUT_MOTOR &= ~MOTOR_CCW; P2OUT_MOTOR |= MOTOR_CW; //Detect current position current_position = pos_read(); } //Stop motor P2OUT_MOTOR &= ~(MOTOR_CW + MOTOR_CCW); //Disable potentiometer P1OUT_POT &= ~POT_EN; } void move_up(unsigned int max_position) { //Detect current position current_position = pos_read(); //Limit check and button held down while((current_position < max_position) && (P1IN_RFIN & RX_D3)){ //Drive motor upward P2OUT_MOTOR &= ~MOTOR_CW; P2OUT_MOTOR |= MOTOR_CCW; //Detect current position current_position = pos_read(); } //Stop motor P2OUT_MOTOR &= ~(MOTOR_CW + MOTOR_CCW); //Disable potentiometer P1OUT_POT &= ~POT_EN; } void move_closed(unsigned int max_position) { //Detect current position current_position = pos_read(); //loop until at the limit while(current_position <= max_position){ //Drive motor up P2OUT_MOTOR &= ~MOTOR_CW; P2OUT_MOTOR |= MOTOR_CCW; //Detect current position current_position = pos_read(); } //Stop motor P2OUT_MOTOR &= ~(MOTOR_CW + MOTOR_CCW); //Disable potentiometer P1OUT_POT &= ~POT_EN; } void move_favorite(unsigned int favorite_position) { //Detect current position current_position = pos_read(); //Determine the direction of drive needed //Below favorite position if(current_position < favorite_position){ //Loop until close to favorite while(current_position < favorite_position){ //Drive upwards P2OUT_MOTOR &= ~MOTOR_CW; P2OUT_MOTOR |= MOTOR_CCW; //Detect current position current_position = pos_read(); } } //Above favorite position else if(current_position > favorite_position){ //Loop until close to favorite while(current_position > favorite_position){ //Drive down P2OUT_MOTOR &= ~MOTOR_CCW; P2OUT_MOTOR |= MOTOR_CW; //Detect current position current_position = pos_read(); } } //Stop motor P2OUT_MOTOR &= ~(MOTOR_CW + MOTOR_CCW); //Disable potentiometer P1OUT_POT &= ~POT_EN; } unsigned int pos_read(void) { //Enable interrupt ADC10IE = ADC10IE0; //Enable, start conversion ADC10CTL0 |= ADC10ENC + ADC10SC; while(ADC10CTL1 & ADC10BUSY);{ return pos_result; } }