Author Topic: PinWake: Odd Behavior  (Read 1509 times)

ArG

  • RFduino Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
PinWake: Odd Behavior
« on: May 17, 2016, 08:38:33 AM »
Hi There,
First I want to preface that at best I'm a hack...a non-developer trying to be one of the cool kids and develop.  :)  please go easy on me.

I've created a BLE device with the RFDuino chip and it's worked very well.  To conserve battery I added a vibration switch (https://www.adafruit.com/product/1767) that lets the current transfer through pin 5 (wakePin) to wake up the device.  What i'm finding is that in some cases the devices are harder to wake up and it takes a harder crack on the counter to get the vibration sensor to trigger and in some cases it doesn't wake up at all.

I'm including the source code and hoping that you all may find some concerns and offer up some advice to make the code more reliable.

Setup Code:
Code: [Select]
void setup()
{
  // Enable pin for thermistor Voltage Measurement Needs
  pinMode(voltPin, INPUT);  // Needed for Voltage Measurement
 
  // Enable RFDuino and powerlevel
  RFduinoBLE.txPowerLevel = blePOWERLEVEL;
  // updating naming to include a serial number for each device.  This will now be as follows:  CL_nnn  where CL is the color of the orb.
 
  RFduinoBLE.deviceName = "BL_003 ";   // Purple = _PR Green = _GR Blue = _BL Orange = _OR
  pinMode(wakePin, INPUT);

  // take 10 readings and average them to get the best thermistor reading and to read prior to running the loop
 
  int averageReading = 0;
  for (int loop = 0; loop < 10; loop++) {
    averageReading = averageReading + analogRead(thermPin);
  }
  resetTempArray();
}

and the place where I call the RFduino_ULPDelay(Infinite) in the main loop

Code: [Select]
        RFduino_pinWake(wakePin, HIGH);
        RFduino_ULPDelay(INFINITE);   
         
        RFduino_resetPinWake(wakePin);
        RFduino_pinWake(wakePin, DISABLE);

      // set all variables after wake...
        resetTempArray();
        resetVariables();

        int averageReading = 0;
        for (int loop = 0; loop < 100; loop++) {
          averageReading = averageReading + analogRead(thermPin);
        }
       
        averageReading = averageReading / 100;

One thing I have noticed, the first time through the code when it hits the ULPDelay, it doesn't fall asleep and immediately resets.

Any help would be greatly appreciated!

Best,
ArG

<let me know if i need to answer any clarifying questions....I'm not super good at formulating coding questions>

tolson

  • Global Moderator
  • *****
  • Posts: 831
  • Karma: +19/-0
    • View Profile
    • Thomas Olson Consulting
Re: PinWake: Odd Behavior
« Reply #1 on: May 17, 2016, 12:07:48 PM »
That particular sensor says it takes a hard crack to activate it. I wonder about debounce issues with those kind of switches.

Have you perfected your code using a simple contact switch first, in order to know if it is your code or the switch that is the problem?

ArG

  • RFduino Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: PinWake: Odd Behavior
« Reply #2 on: May 17, 2016, 01:02:24 PM »
I'm sure my code isn't awesome, but I have successfully tested the sensor with a volt meter and a power supply.  It triggers quite easily.

What I'm trying to do is be super energy efficient and turn off the wake pin when the device is alive and active.

Tolson,
Could you take a look at the code for setting pinWake, then waking up, and then disabling pinwake to reduce battery consumption.  Does that code look ok?

I really appreciate it!!

tolson

  • Global Moderator
  • *****
  • Posts: 831
  • Karma: +19/-0
    • View Profile
    • Thomas Olson Consulting
Re: PinWake: Odd Behavior
« Reply #3 on: May 17, 2016, 02:06:26 PM »
Start with this to test your switch...

Code: [Select]
// pinWakeTest

#include <RFduinoBLE.h>

int wakePin = 5;

int LEDpin = 2;

void setup()
{
 
  pinMode(wakePin, INPUT);
  RFduino_pinWake(wakePin, HIGH);

  pinMode(LEDpin,OUTPUT);
 
}


void loop() {
       
        RFduino_ULPDelay(INFINITE);

        if(RFduino_pinWoke(wakePin))
        {

          // Do your just woke up stuff here.
          // For example
          digitalWrite(LEDpin,HIGH);
          delay(200);
          digitalWrite(LEDpin,LOW);
         
         
          RFduino_resetPinWake(wakePin);
        }
         
}


ArG

  • RFduino Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: PinWake: Odd Behavior
« Reply #4 on: May 17, 2016, 02:08:10 PM »
One thing that was included in the large code snip.

The RFduino, does an analog read of a thermistor to get the temperature.  It then advertises its current temp, energy, and temp over the last 60 seconds (Temp Slope).  I use the same RFDuino_ULP(1.667) seconds to allow the device to iterate through those three advertisements every 5 seconds. 

The device goes to sleep after certain criteria are reached.  The issue I was running into, while I'm advertising temp, energy, and temp slope if the PinWake is active the advertising can be interrupted by a pin wake instead of broadcasting for the full 5 seconds.

So while advertising the data i need to disable the pinWake, then turn pinWake back on when the device goes to sleep INFINITE.

Does that make sense?

ArG

  • RFduino Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: PinWake: Odd Behavior
« Reply #5 on: May 18, 2016, 06:07:05 AM »
I tested the provided code and it definitely works solidly.  The sensor wakes up nicely and then goes back to sleep on touch.

My next question, what would be the best way to disable pinWake in code to do some processing and advertising, and then re-enable it when I want the device to go back to sleep?

Thanks a ton for the help!!!

ArG

  • RFduino Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: PinWake: Odd Behavior
« Reply #6 on: May 18, 2016, 03:43:14 PM »
I would really appreciate some help on the last post (question)...

tolson

  • Global Moderator
  • *****
  • Posts: 831
  • Karma: +19/-0
    • View Profile
    • Thomas Olson Consulting
Re: PinWake: Odd Behavior
« Reply #7 on: May 18, 2016, 05:50:41 PM »
The next step should be to verify you can keep changing the advertisement as fast as you want. Smart devices don't necessarily pick up on advertisement changes in real time. So your milage may vary. You have to experiment and perfect each step of the way before adding all kinds of complicated library features, etc.

Perhaps a simple test is like so.

Code: [Select]
// pinWakeTest

#include <RFduinoBLE.h>

int wakePin = 5;

int LEDpin = 2;

void setup()
{
  RFduinoBLE.txPowerLevel = 4;
  RFduinoBLE.deviceName = "BL_003";
 
  pinMode(wakePin, INPUT);
  RFduino_pinWake(wakePin, HIGH);

  pinMode(LEDpin,OUTPUT);
  pinMode(4,OUTPUT);
}


void loop() {
       
        RFduino_ULPDelay(INFINITE);

        if(RFduino_pinWoke(wakePin))
        {

          RFduino_resetPinWake(wakePin);         
          // Do your just woke up stuff here.
          // For example
          digitalWrite(LEDpin,HIGH);
          delay(200);
          digitalWrite(LEDpin,LOW);

          advertise("A", 3000);
          advertise("B", 3000);
          advertise("C", 3000);
          //delay(5000);
          // prepare wakePin for sleepy time
          RFduino_resetPinWake(wakePin);
        }
         
}

void advertise(const char *data, uint32_t ms)
{
  digitalWrite(4,HIGH);
  // Send advertisement data to RFDuino
  RFduinoBLE.advertisementData = data;
  RFduinoBLE.begin();
  delay(ms); // Reduce to ULP for duration -> ms (Not sure you can do ULP here
  RFduinoBLE.end();
  digitalWrite(4,LOW);
  delay(1000); //See if you LED turns off before next advertisement

}



ArG

  • RFduino Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: PinWake: Odd Behavior
« Reply #8 on: May 20, 2016, 08:40:11 AM »
Thank you so much....giving this a try now. 

I really like using the advertisement and the delay instead of ULP for broadcasting.  Super smart....

ArG

  • RFduino Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: PinWake: Odd Behavior
« Reply #9 on: May 20, 2016, 12:57:31 PM »
The suggested code updates worked incredibly well...

Thanks again for the quick responses.