[ create a new paste ] login | about

Link: http://codepad.org/cwQxIZ4u    [ raw code | fork ]

C, pasted on Jul 27:
/*
 * =====================================================================================
 *
 *       Filename:  AXBGTest1.pde
 *
 *    Description:  Test 1 of Arduino, Xbee, Bluetooth, GPS stuff.
 *
 *        Created:  07/05/2010 08:12:21 PM
 *
 *         Author:  Nikhilesh S (nikki)
 *
 * =====================================================================================
 */

#include <SoftwareSerial.h>
#include <avr/wdt.h>

#define CM_BEGIN '<'
#define CM_END '>'

#define CM_LED_OFF 'A'
#define CM_LED_ON 'B'

#define CM_MOTOR_OFF 'C'
#define CM_MOTOR_ON 'D'

//Pins.
#define PIN_LED 13
#define PIN_MOTOR 9

char command[10]; //Commands are read into this.
int commLen; //The current index of command read-in (and thus also the length of the command).
int logSkip; //Don't write back every loop.

void setup();
void loop();
void execCommand();

/* 
 * =====================================================================================
 *     Function:  setup
 *  Description:  Run on Arduino startup.
 * =====================================================================================
 */

void setup()
{
    wdt_disable(); //Disable watchdog timer.

    pinMode(PIN_LED, OUTPUT);
    digitalWrite(PIN_LED, LOW);
    pinMode(PIN_MOTOR, OUTPUT);
    digitalWrite(PIN_MOTOR, LOW);

    //--- Bluetooth stuff -----------------------------------------------

    //Broadcast connection.
    delay(2000);
    Serial.begin(115200);
    delay(2000);
    Serial.print("AT+JSEC=1,1,1,04,1111\r\n");
    delay(2000);
    Serial.print("AT+JDIS=3\r\n");
    delay(2000);
    Serial.print("AT+JRLS=1101,11,Serial Port,01,000000\r\n");
    delay(2000);
    Serial.print("AT+JAAC=1\r\n");
    delay(2000);

    //Flash LED saying "We're discoverable now!".
    digitalWrite(PIN_LED, HIGH);
    delay(200);
    digitalWrite(PIN_LED, LOW);
    delay(200);
    digitalWrite(PIN_LED, HIGH);
    delay(200);
    digitalWrite(PIN_LED, LOW);
    delay(200);
    digitalWrite(PIN_LED, HIGH);
    delay(200);
    digitalWrite(PIN_LED, LOW);

    //Take connection.
    Serial.flush();  
    int val = Serial.read();
    while (val != 'R')
    {
        val = Serial.read();
    }
    delay(1000);
    Serial.print("AT+JSCR\r\n");

    Serial.println("--- SETUP ---\n\n");
    Serial.println("Bluetooth connection established.");

    //At this point, we're connected, light LED. :-)
    digitalWrite(PIN_LED, HIGH);

    //--- I/O pins setup ------------------------------------------------


    //--- Misc. stuff ---------------------------------------------------

    commLen = 0;
    logSkip = 0;

    //-------------------------------------------------------------------

    Serial.print("\n--- SETUP DONE ---\n\n");
}

/* 
 * =====================================================================================
 *     Function:  loop
 *  Description:  Run every 'tick'.
 * =====================================================================================
 */

void loop()
{
    if (Serial.available())
    {
        char c = Serial.read();

        if (c == CM_BEGIN)
            commLen = 0; //New command starting.
        else if (c == CM_END)
        {
            //Command ended, execute it.
            command[commLen++] = '\0';
            execCommand();
            commLen = 0;
        }
        else
            command[commLen++] = c; //Middle of command, store it.
    }

    /*
    ++logSkip;
    if (logSkip > 50)
    {
        Serial.println("<log>");
    }
    */
}

/* 
 * =====================================================================================
 *     Function:  execCommand
 *  Description:  Execute the command stored in 'command'.
 * =====================================================================================
 */

void execCommand()
{
    switch (command[0])
    {
        case CM_LED_OFF:
            digitalWrite(PIN_LED, LOW);
            break;

        case CM_LED_ON:
            digitalWrite(PIN_LED, HIGH);
            break;

        case CM_MOTOR_OFF:
            digitalWrite(PIN_MOTOR, LOW);
            break;

        case CM_MOTOR_ON:
            digitalWrite(PIN_MOTOR, HIGH);
            break;
    }
}


Create a new paste based on this one


Comments: