First off, here’s a video:
If you’re a long time follower of the blog, than you may notice that it looks like I’m backtracking here. Let me go over the differences between the two systems I’ve developed.
The one I “finished” a few months ago can be found here. While the code is “good” and it works well for what it does, there are a few inherent problems with it. The first being that it has to have a computer to be used. The second being that the computer running the intermediate program has to be extremely fast, so it’s not totally feasible for field use. It would also be very hard for it to go wireless, but I had built that groundwork in so it could happen.
The one I’m working on now doesn’t require a computer as an intermediate, and is going to be totally wireless as from the start.
This morning I finished the handshake data exchange over xbee. Right now it’s just dimming a few LED’s but if you take a peek at the following code, it’s very expandable.
Here’s the working code:
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#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 in_LED = 10; int out_LED = 11; int input = 2; int sendval; //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(in_LED, OUTPUT); pinMode(out_LED, OUTPUT); pinMode(input, INPUT); Serial.begin(9600); xbee_serial.begin(9600); xbee_serial.print("0,0,0."); // this is very important as it starts of the loop because it makes "xbee_serial.avalible() > 0. } 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(in_LED, atoi(subStr(serialbuf, "," , 1))); sendval = map(analogRead(input), 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(out_LED , sendval); } } //delay(10); } 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; } |