Skip to content

Instantly share code, notes, and snippets.

@don
Created December 13, 2013 16:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save don/7947381 to your computer and use it in GitHub Desktop.
/*
This RFduino sketch demonstrates a full bi-directional Bluetooth Low
Energy 4 connection between an iPhone application and an RFduino.
This sketch works with the rfduinoLedButton iPhone application.
The button on the iPhone can be used to turn the green led on or off.
The button state of button 1 is transmitted to the iPhone and shown in
the application.
*/
#include <RFduinoBLE.h>
// pin 3 on the RGB shield is the red led
// (can be turned on/off from the iPhone app)
int led = 3;
// pin 5 on the RGB shield is button 1
// (button press will be shown on the iPhone app)
int button = 5;
// debounce time (in ms)
int debounce_time = 10;
// maximum debounce timeout (in ms)
int debounce_timeout = 100;
void setup() {
// led turned on/off from the iPhone app
pinMode(led, OUTPUT);
// button press will be shown on the iPhone app)
pinMode(button, INPUT);
// this is the data we want to appear in the advertisement
// (the deviceName length plus the advertisement length must be <= 18 bytes
RFduinoBLE.advertisementData = "ledbtn";
// start the BLE stack
RFduinoBLE.begin();
}
int debounce(int state)
{
int start = millis();
int debounce_start = start;
while (millis() - start < debounce_timeout)
if (digitalRead(button) == state)
{
if (millis() - debounce_start >= debounce_time)
return 1;
}
else
debounce_start = millis();
return 0;
}
int delay_until_button(int state)
{
// set button edge to wake up on
if (state)
RFduino_pinWake(button, HIGH);
else
RFduino_pinWake(button, LOW);
do
// switch to lower power mode until a button edge wakes us up
RFduino_ULPDelay(INFINITE);
while (! debounce(state));
// if multiple buttons were configured, this is how you would determine what woke you up
if (RFduino_pinWoke(button))
{
// execute code here
RFduino_resetPinWake(button);
}
}
void loop() {
delay_until_button(HIGH);
RFduinoBLE.send(1);
delay_until_button(LOW);
RFduinoBLE.send(0);
}
void RFduinoBLE_onDisconnect()
{
// don't leave the led on if they disconnect
digitalWrite(led, LOW);
}
void RFduinoBLE_onReceive(char *data, int len)
{
// if the first byte is 0x01 / on / true
if (data[0])
digitalWrite(led, HIGH);
else
digitalWrite(led, LOW);
}
/*
The sketch demonstrates how to do accept a Bluetooth Low Energy 4
Advertisement connection with the RFduino, then send CPU temperature
updates once a second.
It is suppose to be used with the rfduinoTemperature iPhone application.
*/
#include <RFduinoBLE.h>
void setup() {
// this is the data we want to appear in the advertisement
// (the deviceName length plus the advertisement length must be <= 18 bytes)
RFduinoBLE.advertisementData = "temp";
// start the BLE stack
RFduinoBLE.begin();
}
void loop() {
// sample once per second
RFduino_ULPDelay( SECONDS(1) );
// get a cpu temperature sample
// degrees c (-198.00 to +260.00)
// degrees f (-128.00 to +127.00)
float temp = RFduino_temperature(CELSIUS);
// send the sample to the iPhone
RFduinoBLE.sendFloat(temp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment