Analog comms with xbee. Program for the Coordinator.

Dependencies:   C12832_lcd mbed xbeeLibDannelly

Fork of Xbee_Hello_World_B by Tristan Hughes

Committer:
dannellyz
Date:
Sun Feb 15 17:10:17 2015 +0000
Revision:
2:9b7d98c295d9
Parent:
1:4ce1378831fe
Added visuals with rgb light actuation by pots

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tristanjph 0:122702c9168a 1 #include "mbed.h"
dannellyz 1:4ce1378831fe 2 #include "xbee.h" // Include for xbee code
dannellyz 1:4ce1378831fe 3 #include "C12832_lcd.h" // Include for LCD code
dannellyz 1:4ce1378831fe 4 #include <string.h> //Iclude for string handeling
dannellyz 1:4ce1378831fe 5 xbee xbee1(p9,p10,p30); //Initalise xbee_lib varName(rx,tx,reset)
dannellyz 1:4ce1378831fe 6 DigitalOut rst1(p30);
dannellyz 1:4ce1378831fe 7 Serial pc(USBTX, USBRX); //Initalise PC serial comms
dannellyz 1:4ce1378831fe 8 C12832_LCD lcd; //Initialize LCD Screen
dannellyz 2:9b7d98c295d9 9 PwmOut speaker(p26); //Speaker with PWM driver
dannellyz 2:9b7d98c295d9 10 //RGB LED with 3 PWM outputs for dimmer control
dannellyz 2:9b7d98c295d9 11 PwmOut r(p23);
dannellyz 2:9b7d98c295d9 12 PwmOut g(p24);
dannellyz 2:9b7d98c295d9 13 PwmOut b(p25);
tristanjph 0:122702c9168a 14
tristanjph 0:122702c9168a 15 int main()
tristanjph 0:122702c9168a 16 {
dannellyz 1:4ce1378831fe 17 //In AT mode we are constantly transmitting so it is
dannellyz 1:4ce1378831fe 18 //necessary to implement an external protocol
dannellyz 1:4ce1378831fe 19 //see https://developer.mbed.org/users/dannellyz/notebook/at-vs-api-when-why-how/#
dannellyz 1:4ce1378831fe 20
dannellyz 1:4ce1378831fe 21 //For this we will use two buffers one for each
dannellyz 1:4ce1378831fe 22 //expected reading from the appBoard pot1/pot2
dannellyz 1:4ce1378831fe 23 char readData1[202]; //Xbee buffer size is 202 bytes
dannellyz 1:4ce1378831fe 24 char readData2[202]; //Xbee buffer size is 202 bytes
tristanjph 0:122702c9168a 25
dannellyz 1:4ce1378831fe 26 // reset the xbees (at least 200ns)
dannellyz 1:4ce1378831fe 27 rst1 = 0;
dannellyz 1:4ce1378831fe 28 wait_ms(1);
dannellyz 1:4ce1378831fe 29 rst1 = 1;
dannellyz 1:4ce1378831fe 30 wait_ms(1);
dannellyz 1:4ce1378831fe 31
dannellyz 1:4ce1378831fe 32 //Setup LCD screen
dannellyz 1:4ce1378831fe 33 lcd.cls();
dannellyz 1:4ce1378831fe 34 lcd.locate(0,1);
dannellyz 1:4ce1378831fe 35
dannellyz 2:9b7d98c295d9 36 //Setup speaker
dannellyz 2:9b7d98c295d9 37 speaker.period(1.0/800.0); // 800hz period
dannellyz 2:9b7d98c295d9 38
dannellyz 2:9b7d98c295d9 39 //Setup RGB
dannellyz 2:9b7d98c295d9 40 r=1.0;
dannellyz 2:9b7d98c295d9 41 g=1.0;
dannellyz 2:9b7d98c295d9 42 b=1.0;
dannellyz 2:9b7d98c295d9 43
dannellyz 1:4ce1378831fe 44
tristanjph 0:122702c9168a 45 while(1) {
dannellyz 1:4ce1378831fe 46
dannellyz 1:4ce1378831fe 47 //RecieveDataChar allows you to read data in until the
dannellyz 1:4ce1378831fe 48 //seperator which is the second argument
dannellyz 1:4ce1378831fe 49
dannellyz 1:4ce1378831fe 50 //Recieve data format POT2a
dannellyz 1:4ce1378831fe 51 xbee1.RecieveDataChar(readData1,'a'); //Read data from the XBee
dannellyz 1:4ce1378831fe 52 //Recieve data format POT1b
dannellyz 1:4ce1378831fe 53 xbee1.RecieveDataChar(readData2,'b'); //Read data from the XBee
dannellyz 1:4ce1378831fe 54 //Needed to keep in sync with other xbee
dannellyz 1:4ce1378831fe 55 wait(0.1);
dannellyz 1:4ce1378831fe 56
dannellyz 1:4ce1378831fe 57 //Clean data by removing delimeter
dannellyz 1:4ce1378831fe 58 readData1[strlen(readData1)-1] = 0;
dannellyz 1:4ce1378831fe 59 readData2[strlen(readData2)-1] = 0;
dannellyz 1:4ce1378831fe 60
dannellyz 1:4ce1378831fe 61 //Change values back to floats for possible use later
dannellyz 1:4ce1378831fe 62 float pot1 = atof(readData2);
dannellyz 1:4ce1378831fe 63 float pot2 = atof(readData1);
dannellyz 1:4ce1378831fe 64
dannellyz 2:9b7d98c295d9 65
dannellyz 2:9b7d98c295d9 66 //Change red value with pot1
dannellyz 2:9b7d98c295d9 67 r = 1- pot1;
dannellyz 2:9b7d98c295d9 68 //Change blue value with pot2
dannellyz 2:9b7d98c295d9 69 b = 1- pot2;
dannellyz 2:9b7d98c295d9 70
dannellyz 1:4ce1378831fe 71 //Echo data
dannellyz 1:4ce1378831fe 72 pc.printf("Pot1= %.2f Pot2= %.2f \n\r",pot1,pot2);
dannellyz 1:4ce1378831fe 73 lcd.printf("Pot1= %.2f Pot2= %.2f \n\r",pot1,pot2);
tristanjph 0:122702c9168a 74 }
dannellyz 1:4ce1378831fe 75 }
dannellyz 1:4ce1378831fe 76