Created
March 30, 2012 13:18
-
-
Save fbrosser/2251484 to your computer and use it in GitHub Desktop.
Basstation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Basstation | |
#include <Coordinator.h> | |
#include <Radio.h> | |
#include <XBee.h>U | |
#include <SoftwareSerial.h> | |
#include <Analog.h> | |
Coordinator coordinator = Coordinator(); | |
const int errorPin = 22; | |
const int assocPin = 24; | |
unsigned long int blinkTimer = 0; | |
unsigned long int tempTimer = 0; | |
boolean assocPinStatus = false; | |
SoftwareSerial nss = SoftwareSerial(30, 32); | |
uint8_t sendData[120]; | |
uint8_t s = 0; | |
void errorCB() { | |
digitalWrite(errorPin, HIGH); | |
} | |
void setup() { | |
for(int i=0; i<72; i++) sendData[i] = '1'; | |
for(int i=72; i<120; i++) sendData[i] = '2'; | |
attachInterrupt(0, pairUp, FALLING); | |
nss.begin(9600); | |
nss.println(" "); | |
nss.println(" "); | |
nss.println("***** RESET *****"); | |
nss.println(" "); | |
pinMode(errorPin, OUTPUT); | |
pinMode(assocPin, OUTPUT); | |
digitalWrite(errorPin, LOW); | |
digitalWrite(assocPin, LOW); | |
coordinator.setData(sendData, sizeof(sendData)); | |
coordinator.begin(Serial1); | |
blinkTimer = 0; | |
tempTimer = 0; | |
} | |
void loop() { | |
delay(10); | |
uint8_t st = coordinator.tick(); | |
uint8_t s = coordinator.getState(); | |
assocLed(s); | |
readTemperature(); | |
printState(s); | |
if(st == 1 || st == 3) { | |
digitalWrite(errorPin, HIGH); | |
} | |
else { | |
digitalWrite(errorPin, LOW); | |
} | |
} | |
void assocLed(uint8_t state) { | |
if(state == 7) { | |
blinkPairUp(); | |
} | |
else { | |
if(coordinator.getAssoc()) { | |
digitalWrite(assocPin, HIGH); | |
} | |
else { | |
digitalWrite(assocPin, LOW); | |
} | |
} | |
} | |
void pairUp() { | |
coordinator.pairUp(); | |
} | |
void blinkPairUp() { | |
if(millis() >= blinkTimer) { | |
digitalWrite(assocPin, (assocPinStatus = !assocPinStatus)); | |
blinkTimer = millis() + 500; | |
} | |
} | |
void printState(uint8_t s) { | |
nss.print("State ["); | |
nss.print(s); | |
nss.print("] = ["); | |
nss.print(coordinator.getStateName(s)); | |
nss.println("]"); | |
} | |
void readTemperature() { | |
if(millis() >= tempTimer) { | |
int temperature = (getVoltage(8)-0.5)*100; | |
nss.print("Temperature (C): ["); | |
nss.print(temperature); | |
nss.println("]"); | |
tempTimer = millis() + 20000; | |
} | |
} | |
float getVoltage(int pin) { | |
return (analogRead(pin) * 0.004882814); | |
} | |
void readBattery() { | |
nss.println("Reading battery..."); | |
int batteryLevel = analogRead(0); | |
// Voltage divider divides voltage by 2 | |
// Value from analog port has resolution of 49mV | |
// Adjust for voltage drop across BJT (approximation) | |
nss.print("Analog read ["); | |
nss.print(batteryLevel); | |
nss.println("]"); | |
float Vbatt = (2*batteryLevel*0.0049) - 0.04*(batteryLevel/408); | |
nss.print("Battery Voltage ["); | |
nss.print(Vbatt); | |
nss.println("V]"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment