Playing with codes

Here’s my current codes for the monitor and the receiver. I have not gotten the mobile platform running yet so I am using LEDs to test the transmission of accelerometer readings.

Here is the monitor:

/*
This sketch is written for a RBBB based remote monitor/controller for a mobile sensor platform.
Data monitoring is done by a 20×4 LCD display. (ModernDevice)
Controls are inputed through an accelerometer and buttons (to be added at a later date). (ModernDevice)
Wireless data transmission is done through a HopeRF HM-TR TTL transceiver. (Futurlec)
*/
/*
Special thanks to the following sources of reference:
General – http://tronixstuff.wordpress.com/tutorials/
LCD – http://www.ladyada.net/learn/lcd/charlcd.html
HopeRF – http://www.open.com.au/mikem/arduino/HopeRF/
*/
// include the library code:
#include
#include
// Declare the HRFMessage to use Serial1 for IO with the HM-TR module
HRFMessage server(&Serial);
// pin setups for RBBB
const int ACCxpin = A5; // accelerometer
const int ACCypin = A4;
const int ACCzpin = A3;
const int TRconfigpin = 2; // tranceiver
const int TRenablepin = 3;
const int LCDRSpin = 5; // lcd
const int LCDENpin = 7;
const int LCD11pin = 9;
const int LCD12pin = 10;
const int LCD13pin = 11;
const int LCD14pin = 12;
// variable values
int xval = 0;
int yval = 0;
int zval = 0;
int xavgval = 0;
int yavgval = 0;
int zavgval = 0;
long lastAccelTestTime = 0;
long lastAccelDisplayTime = 0;
long lastTransmitTime = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(LCDRSpin, LCDENpin, LCD11pin, LCD12pin, LCD13pin, LCD14pin);
// arduino setup – runs once when powered
void setup() {
//set speed for transceiver
Serial.begin(9600);
// activate transciever – enable HIGH, config LOW
pinMode(TRconfigpin, OUTPUT);
pinMode(TRenablepin, OUTPUT);
digitalWrite(TRconfigpin, LOW);
digitalWrite(TRenablepin, HIGH);
// set up the LCD’s number of columns and rows:
lcd.begin(20, 4);
// print a message to the LCD.
lcd.print(“Accelerometer Test”);
}
// arduino loop – runs continueously when powered
void loop() {
// universal time counter for all loop functions
long thisTime = millis();
// read values from accelerometer every 50ms
if (thisTime > lastAccelTestTime + 50) {
xval = xval + analogRead(ACCxpin);
yval = yval + analogRead(ACCypin);
zval = zval + analogRead(ACCzpin);
lastAccelTestTime = thisTime;
}
// transmit drive commands to platform every 250ms
if (thisTime > lastTransmitTime + 250) {
// get averaged values
xavgval = xval/5;
yavgval = yval/5;
zavgval = zval/5;
// reset values
xval = 0;
yval = 0;
zval = 0;
if (xavgval 390){
senddata(1,2);
}
if (yavgval 390){
senddata(2,2);
}
else {
senddata(9,999);
}
lastTransmitTime = thisTime;
}
// display averaged accelerometer data every 500ms
if (thisTime > lastAccelDisplayTime + 500) {
// set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(“X = “);
lcd.print(xavgval);
lcd.setCursor(0, 2);
lcd.print(“Y = “);
lcd.print(yavgval);
lcd.setCursor(0, 3);
lcd.print(“Z = “);
lcd.print(zavgval);
// reset xyz values
lastAccelDisplayTime = thisTime;
}
}
void senddata(int sensor, int value){
value = sensor*1000+value;
char Str[6];
sprintf(Str, “%d”, value);
server.send((uint8_t*)Str, strlen(Str));
}




Here is the receiver:

#include
// Declare the HRFMessage to use Serial1 for IO with the HM-TR module
HRFMessage client(&Serial);
long lastSendTime = 0;
int msg;
void setup()
{
Serial.begin(9600);
pinMode(2,OUTPUT);
digitalWrite(2, LOW);
pinMode(3,OUTPUT);
digitalWrite(3, HIGH);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}
void loop()
{
// But always look for a reply
uint8_t buf[HRF_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (client.recv((uint8_t*)&buf, &len))
{
msg = atoi((const char*)buf);
}
switch(msg)
{
case 1001:
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
break;
case 1002:
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
break;
case 2001:
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
break;
case 2002:
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
break;
case 9999:
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(7, LOW);
break;
}
msg =0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s