I made some progress today, here’s a video:
Basically this is a proof of concept. The transmitter is sending a line of data very similar to the one that will be sent by the final controller (as seen in the controller code) the receiving end with be receiving in the same way the vehicle code does (same as above).
Here are a few pictures, this first one is a top down view of my prototyping area:
This second one shows all components involved:
This is the code running on the transmitter side, the side with the pot that’s transmitting it’s analog value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
</div> <div> #include <SoftwareSerial.h> SoftwareSerial xbee_serial(2, 3); int pot_1 = 0; int LED_1 = 5; //this will serve as an LED to compare to int sendval; void setup(){ pinMode(pot_1, INPUT); pinMode(LED_1, OUTPUT); Serial.begin(9600); xbee_serial.begin(9600); } void loop(){ Serial.println(analogRead(pot_1)); sendval = map(analogRead(pot_1), 0, 1023, 0, 255); xbee_serial.print(sendval); // Value that it sends over the serial xbee_serial.print(","); xbee_serial.print("100"); //This second byte is for the purpose of the program, it is not being used. xbee_serial.print("."); //EOP marker analogWrite(LED_1,sendval); //delay(100); } |
This second half is on the receiving end. It may seem way overkill for what it does, but it’s all expandable to a pretty much infinite amount of data to be transmitted. For an example of what it’s capable of see this video:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
</div> <div> #include <SoftwareSerial.h> SoftwareSerial xbee_serial(2, 3); #include <string.h> // we'll need this for subString #define MAX_STRING_LEN 20 // like 3 lines above, change as needed. int LED_1 = 5; int led_val; //serial stuff const char EOPmarker = '.'; //This is the end of packet marker char serialbuf[32]; //This gives the incoming serial some room. Change it if you want a longer incoming. void setup(){ pinMode(LED_1, OUTPUT); Serial.begin(9600); xbee_serial.begin(9600); } void loop(){ if (xbee_serial.available() > 0) { static int bufpos = 0; char inchar = xbee_serial.read(); if (inchar != EOPmarker) { serialbuf[bufpos] = inchar; bufpos++; } else { serialbuf[bufpos] = 0; //restart the buff bufpos = 0; //restart the position of the buff Serial.println(atoi(subStr(serialbuf, "," , 1))); analogWrite(LED_1, atoi(subStr(serialbuf, "," , 1))); } } } char* subStr (char* input_string, char *separator, int segment_number) { char *act, *sub, *ptr; static char copy[MAX_STRING_LEN]; int i; strcpy(copy, input_string); for (i = 1, act = copy; i <= segment_number; i++, act = NULL) { sub = strtok_r(act, separator, &ptr); if (sub == NULL) break; } return sub; } |