Skip to content

Commit

Permalink
many fixes after first live night test
Browse files Browse the repository at this point in the history
Numerous dumb errors with == fixed, new time comparison functions
because of byte rollover errors in prev function.  changes to logic of
sleeping/waking/squawking.
  • Loading branch information
pixelpusher committed Apr 9, 2013
1 parent 17fe777 commit 7c0eec0
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 35 deletions.
23 changes: 15 additions & 8 deletions Arduino/CrossSquawkBirdBrains/CrossSquawkBirdBrains.ino
Expand Up @@ -17,7 +17,7 @@
#include "Sounds.h"
#include <JeeLib.h>

const int ListenBaseTimeOut = 6000;
const int ListenBaseTimeOut = 4000;

#define AMP_PWR_PIN A3 // using the Mono Amp breakout board from Sparkfun which has on/off capability: https://www.sparkfun.com/products/11044
//#define DEBUG 1
Expand All @@ -26,17 +26,17 @@ const int ListenBaseTimeOut = 6000;
#ifdef DEBUG
const int RandomTime = 1;
#else
const int RandomTime = 250;
const int RandomTime = 500;
#endif


#ifdef DEBUGLED
Port three (3);
#endif

const int SleepFactor = 3; // proportion of time to sleep for vs. listening (2 = double listen time)
const int SleepFactor = 10; // proportion of time to sleep for vs. listening (2 = double listen time)
const int speakerPin = 3;
const byte SLEEP_MINUTES = 1; // whole minutes to sleep for when in sleep mode
const byte SLEEP_MINUTES = 15; // whole minutes to sleep for when in sleep mode

char payload[] = "tw"; // what we broadcast
char sleepPayload[] = "sl"; // what we broadcast
Expand Down Expand Up @@ -153,15 +153,19 @@ void loop()
}

rf12_sleep (0); // in 32 * ms, so 32 seconds in this case
Sleepy::loseSomeTime((ListenBaseTimeOut + random (1, 9) * RandomTime)*SleepFactor);
Sleepy::loseSomeTime(ListenBaseTimeOut + random (1, 9) * RandomTime);

Sleepy::loseSomeTime(ListenBaseTimeOut*SleepFactor);
Sleepy::loseSomeTime(ListenBaseTimeOut*SleepFactor);

delay(10); // settle
rf12_sleep (-1); // wake up radio
delay(10); // settle

mode = LISTEN; // listen for other birds
listenStartTime = millis();
//ListenTimeOut = ListenBaseTimeOut + random (1, 9) * 250;
ListenTimeOut = ListenBaseTimeOut + random (1, 9) * RandomTime;
ListenTimeOut = ListenBaseTimeOut;
}
else
{
Expand All @@ -180,8 +184,11 @@ void loop()

int timeDiff = millis() - listenStartTime;
// has time run out?
if ( timeDiff > ListenTimeOut )
startSquawking(); // start squawking again
if ( timeDiff > ListenTimeOut )
{
if ( mode != SLEEPING )
startSquawking(); // start squawking again
}
else if (doneReceiving && (rf12_crc == 0) )
{
byte state = matchPayload();
Expand Down
4 changes: 2 additions & 2 deletions Arduino/CrossSquawkBirdBrains/Sounds.h

Large diffs are not rendered by default.

199 changes: 174 additions & 25 deletions Arduino/CrossSquawkController/CrossSquawkController.ino
@@ -1,8 +1,10 @@
#include <JeeLib.h>

#define DEBUG
//#define DEBUG

#define SLEEPYTIME 1500 // how long to between sleep messages, in milliseconds
#define SLEEPYTIME 1250 // how long to between sleep messages, in milliseconds

#define TIMES_TO_BROADCAST 12

// boilerplate for low-power waiting
ISR(WDT_vect) {
Expand All @@ -20,15 +22,91 @@ Port two (2);
enum Mode {
SQUAWK, WAITING, SLEEPING }; // all possible modes

Mode mode = SLEEPING;
Mode mode = WAITING;

//
// time variables
//
byte now[6];

byte wakeTimeArray[] = {
9, 30, 0}; // wake in morning
int wakeTime=0;

byte wakeTimeEndArray[] = {
10, 15, 0}; // wake in morning

byte sleepTimeArray[] = {
19, 0, 0}; // sleep at night
int sleepTime=0;

byte squawkTimeIntervalArray[] = {
8, 15, 0}; // time to squawk for
int squawkTimeInterval=0;

int lastWakeTime = 0; // last time we woke, in seconds
//
//


#ifdef DEBUG
byte testTime[] = {
2, 1, 3};
#endif



//
// Greater-than time?
// Compares left to right, returns true if left is later in time than right
// assuming 24 hr clock cycles where 12am is earliest
//
boolean gtTime( byte* tl, byte* tr )
{
// 00 11 22
// hh mm ss

#ifdef DEBUG
Serial.print(tl[0]);
Serial.print(":");
Serial.println(tr[0]);
#endif

if (tl[0] < tr[0]) return false;
if (tl[0] > tr[0]) return true;

#ifdef DEBUG
Serial.println("meq");
#endif

// if equal...
if (tl[1] > tr[1]) return true;

#ifdef DEBUG
Serial.println("tru");
#endif

if (tl[1] < tr[1]) return false;


#ifdef DEBUG
Serial.println("seq");
#endif

// if equal...
if (tl[2] > tr[2]) return true;
if (tl[2] < tr[2]) return false;

return false;
}


void setup () {
#ifdef DEBUG
Serial.begin(57600);
delay(100);
Serial.println("[starting Cross Squawk Controller]");
delay(100);
#endif

Sleepy::loseSomeTime(32);
Expand All @@ -41,15 +119,19 @@ void setup () {
Sleepy::loseSomeTime(2000);
}



//
// LOOP
//

void loop () {

blinkBlue(1);


Sleepy::loseSomeTime(1000);

int millivolt = map(analogRead(6), 0, 1023, 0, 6600);


#ifdef DEBUG
delay(50);
Expand All @@ -72,13 +154,13 @@ void loop () {
// yy mm dd hh mm ss

#ifdef DEBUG
delay(50);
Serial.print("rtc:");
for (byte i = 0; i < 6; ++i) {
Serial.print(' ');
Serial.print((int) now[i]);
}
Serial.println();
delay(50);
#endif


Expand All @@ -87,9 +169,32 @@ void loop () {
// 0 1 2 3 4 5
// yy mm dd hh mm ss

if ( (now[3] < 8 && now[4] < 30) || now[3] > 15)

#ifdef DEBUG
Serial.print("now: ");
Serial.print(nowTime);
Serial.print(", wake: ");
Serial.print(wakeTime);
Serial.print(", sleep: ");
Serial.println(sleepTime);

byte now2[] = {
now[3], now[4], now[5] };

Serial.println( (int)gtTime(now2,wakeTimeArray) );
delay(100);
#endif


byte nowShifted[] = {
now[3], now[4], now[5] };

//Serial.println( (int)!gtTime(nowShifted,wakeTimeArray) );
//Serial.println( (int)gtTime(nowShifted,sleepTimeArray) );

if ( !gtTime(nowShifted,wakeTimeArray) || gtTime(nowShifted,sleepTimeArray) )
{
mode == SLEEPING;
mode = SLEEPING;
#ifdef DEBUG
delay(50);
Serial.println("SLEEP MODE");
Expand All @@ -99,23 +204,38 @@ void loop () {
Serial.print((int) now[i]);
}
Serial.println();
delay(100);
#endif
}
else if (now[3] == 8 && now[4] > 30)
else if ( gtTime(nowShifted, wakeTimeArray ) && !gtTime(nowShifted,wakeTimeEndArray) )
{
mode == SQUAWK;
#ifdef DEBUG
delay(50);
Serial.println("SQUAWK MODE");
delay(60);
#endif

mode = SQUAWK;
}
else
mode == WAITING;
mode = WAITING;



if ( mode == SLEEPING )
{

#ifdef DEBUG
delay(50);
Serial.println("SLEEP BROADCASTING");
Serial.println();
delay(60);
#endif

rf12_sleep (-1); // wake up radio
delay(20);

for (int v=0; v<10; ++v)
for (int v=0; v<TIMES_TO_BROADCAST; ++v)
{
delay(20);
rf12_recvDone(); // must call this to clear the buffer otherwise can't send!
Expand All @@ -131,7 +251,7 @@ void loop () {

blinkBlue(2);
rf12_sendStart(0, sleepPayload, sizeof (sleepPayload));
delay(60);
delay(100);
Sleepy::loseSomeTime(SLEEPYTIME);
}

Expand All @@ -141,7 +261,7 @@ void loop () {
Serial.println("long sleep");
#endif

Sleepy::loseSomeTime(6000);
Sleepy::loseSomeTime(12000);
}
else if (mode == WAITING)
{
Expand All @@ -161,10 +281,10 @@ void loop () {
Serial.println();
#endif

//sleep for 30 minutes
//sleep for 10 minutes
blinkBlue(4);
for (byte c=0; c<30; ++c)
Sleepy::loseSomeTime(100);
for (byte c=0; c<10; ++c)
Sleepy::loseSomeTime(60000);

// rf12_sendStart(0, payload, sizeof (payload));

Expand All @@ -175,23 +295,49 @@ void loop () {
#ifdef DEBUG
delay(50);
Serial.println("squawking");
Serial.print("rtc");
for (byte i = 0; i < 6; ++i) {
Serial.print(' ');
Serial.print((int) now[i]);
}
Serial.println();
delay(50);
#endif

//sleep for 30 minutes
blinkBlue(6);
blinkBlue(5);

rf12_sendStart(0, payload, sizeof (payload));
Sleepy::loseSomeTime(2000);
rf12_sleep (-1); // wake up radio
delay(20);

for (int v=0; v<4; ++v)
{
delay(20);
rf12_recvDone(); // must call this to clear the buffer otherwise can't send!
boolean cleared = false;
unsigned int cnt = 0;

while (cnt < 5000 && !cleared)
{
delay(1);
++cnt;
cleared = rf12_canSend();
}

blinkBlue(1);
rf12_sendStart(0, payload, sizeof (payload));
delay(80);
Sleepy::loseSomeTime(SLEEPYTIME);
}


#ifdef DEBUG
delay(50);
Serial.println("long sleep");
#endif

Sleepy::loseSomeTime(4000);
}
}




void blinkBlue(byte times)
{
// blink the blue LED just because we can
Expand Down Expand Up @@ -233,3 +379,6 @@ static byte bcd2bin (byte val) {






0 comments on commit 7c0eec0

Please sign in to comment.