Tag Archives: embedded system

Bluetooth controlled robot

Microcontroller – PIC12F1840
Bluetooth Module – SparkFun Bluetooth Modem – BlueSMiRF Silver
Operating System – Debian 8
Pic Programmer – PICkitâ„¢ 3 In-Circuit Debugger
IDE – MPLAB X IDE
Compiler Toolchain – MPLAB XC8 Compiler

Code Snippet

#include "mcc_generated_files/mcc.h"
/*
                         Main application
 */
#define MotorA_P RA0
#define MotorA_N RA4
#define MotorB_P RA2
#define MotorB_N RA1
unsigned char ch = '0';
unsigned char oldch = '0';
void pauseMotor();
void main(void) {
    // initialize the device
    SYSTEM_Initialize();
    MotorA_P = 0;
    MotorA_N = 0;
    MotorB_P = 0;
    MotorB_N = 0;
    while (1) {
        ch = EUSART_Read();
        if (ch == 'U') {
            pauseMotor();
            MotorA_P = 1;
            MotorA_N = 0;
            MotorB_P = 1;
            MotorB_N = 0;
        } else if (ch == 'D') {
            pauseMotor();
            MotorA_P = 0;
            MotorA_N = 1;
            MotorB_P = 0;
            MotorB_N = 1;
        } else if (ch == 'L') {
          
            MotorA_P = 1;
            MotorA_N = 0;
            MotorB_P = 0;
            MotorB_N = 0;
            __delay_ms(200);
            MotorA_P = 1;
            MotorA_N = 0;
            MotorB_P = 1;
            MotorB_N = 0;
        } else if (ch == 'R') {
            MotorA_P = 0;
            MotorA_N = 0;
            MotorB_P = 1;
            MotorB_N = 0;
             __delay_ms(200);
            MotorA_P = 1;
            MotorA_N = 0;
            MotorB_P = 1;
            MotorB_N = 0;
        } else if (ch == 'S') {
            pauseMotor();
            MotorA_P = 1;
            MotorA_N = 0;
            MotorB_P = 1;
            MotorB_N = 0;
        } else if (ch == 'X') {
            MotorA_P = 0;
            MotorA_N = 0;
            MotorB_P = 0;
            MotorB_N = 0;
        }
        if (ch != oldch) {
            oldch = ch;
        }
    }
}
void pauseMotor() {
    if (ch != oldch) {
        MotorA_P = 0;
        MotorA_N = 0;
        MotorB_P = 0;
        MotorB_N = 0;
        __delay_ms(400);
    }
}