Wireless Sensor Motor Control

Program Download:
Arduino Project Code
PIC_Arduino_Comm.ino

PIC MPLABX Project
Wireless_IR_Motor.X.zip

The Software
           There are two firmware programs that are used for this project:

    -The Transmitter Code
    -The Receiver Code



The Arduino UNO is acting as a transmitter and its job is to transmit the commands for controlling the motor based off of the IR sensor analog input. Initially the Arduino Uno sets up its hardware serial communication module for 9600 baud, then it goes into a forever while loop, checking the analog input and outputting a correlating serial command to the XBee module.

Arduino Transmitter Code

------------« Begin Code »------------
#include <LiquidCrystal.h>

#define     dist_6cm     634
#define     dist_8cm     562
#define     dist_10cm    470
#define     dist_12cm    419
#define     dist_14cm    368
#define     dist_16cm    327
#define     dist_18cm    296
#define     dist_20cm    265
#define     dist_22cm    245

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned int inByte = 0;
unsigned int sensorPin = A0;
unsigned int result = 0;

void setup() {
  lcd.begin(16, 2);
  Serial.begin(9600);
  lcd.print(" PyroElectro.com");
  lcd.setCursor(0, 1);
  lcd.print("Speed: ");
}

void loop() {
  unsigned char inChar = 0x00;

  while (1) {        
    
    result = analogRead(sensorPin);

    lcd.setCursor(7,1);

        //Update Motor Speed Setting
        if(result < dist_6cm && result > dist_8cm){
            Serial.write("8");
            lcd.print("88888888");
        }
        else if(result < dist_8cm && result > dist_10cm){
            Serial.write("7");
            lcd.print("7777777 ");
        }
        else if(result < dist_10cm && result > dist_12cm){
            Serial.write("6");
            lcd.print("666666  ");
        }
        else if(result < dist_12cm && result > dist_14cm){
            Serial.write("5");
            lcd.print("55555   ");
        }
        else if(result < dist_14cm && result > dist_16cm){
            Serial.write("4");
            lcd.print("4444    ");
        }
        else if(result < dist_16cm && result > dist_18cm){
            Serial.write("3");
            lcd.print("333     ");
        }
        else if(result < dist_18cm && result > dist_20cm){
            Serial.write("2");
            lcd.print("22      ");
        }
        else if(result < dist_20cm && result > dist_22cm){
            Serial.write("1");
            lcd.print("1       ");
        }
        else if(result < dist_22cm){
            Serial.write("0");
            lcd.print("        ");
        }
       
    delay(10);     
  }  
  
}
------------« End Code »------------

           The compiler used for the receiver side of this project is the C18 Compiler Provided Free From Micropchip. The first part of the code intializes the PIC 18LF4520's hardware serial communication module for 9600 baud. Then after that, it constantly waits to receive serial data and evaluate it to know how the motor should be controlled.

PIC 18LF4520 Receiver Code

------------« Begin Code »------------
while(1){                       
    switch(data){
            case '0' :
                PORTD = 0x00;
                SetDCPWM1( speed_0 );
                break;
            case '1' :
                PORTD = 0x80;
                SetDCPWM1( speed_1 );
                break;
            case '2' :
                PORTD = 0xC0;
                SetDCPWM1( speed_2 );
                break;
            case '3' :
                PORTD = 0xE0;
                SetDCPWM1( speed_3 );
                break;
            case '4' :
                PORTD = 0xF0;
                SetDCPWM1( speed_4 );
                break;
            case '5' :
                PORTD = 0xF8;
                SetDCPWM1( speed_5 );
                break;
            case '6' :
                PORTD = 0xFC;
                SetDCPWM1( speed_6 );
                break;
            case '7' :
                PORTD = 0xFE;
                SetDCPWM1( speed_7 );
                break;
            case '8' :
                PORTD = 0xFF;
                SetDCPWM1( speed_8 );
                break;
            default  :
                PORTD = 0x00;
                break;
    }        
 Delay10KTCYx(10);
 }
------------« End Code »------------

           The firmware for each device looks different because different compilers are used to build the downloadable hex. However, the same basic ideas are at work here--we're using their internal hardware modules to either send or receive commands, a very basic input/output type of system.



;