SUPERMECHANICAL.BLOG

Objects that connect us.

• Twine + Cloud Shield + Arduino + Bike = A More Healthy Nerd

Hey everybody, I’m the Other David at Supermechanical — I do a little bit of everything that relates to engineering and development here at Supermechanical. Most recently, I built Cloud Shield. As a nerd, I spend a lot of time sitting in front of my computer. I try to exercise regularly, but sometimes I get busy and forget. Twine + Cloud Shield + Arduino + Bike = A More Healthy Nerd.

I have a bicycle and a trainer at home that I use to work out — it’s set up so I can use my road bike as a stationary bike. Bonus: I don’t have to worry about getting hit by cars, and I can easily tear down the setup when not in use.

I use my Arduino to measure RPMs when I ride my bike and the Cloud Shield to trigger my Twine when I haven’t been on my bike in 24 hours. You can too!

Materials:

1. Attach the reed switch to the bike frame. Make sure it’s secure so it doesn’t move up/down/around the frame. Then attach the magnet to your spokes. Like this:

2. Strip some wire and attach it to the terminals on the reed sensor like this:

3. Test to make sure it’s working properly. I ran the standard “DigitalReadSerial” sketch to test to make sure that everything was connected correctly.  (Make sure that when the magnet is close to the reed switch it prints out 1.)

```/*
SimpleExerciseMonitor
http://supermechanical.tumblr.com/
*/

#include <Twine.h>
#include <Time.h>

//** Twine variable
Twine cloud;

//** RPM tracking variables
// The number of times the Arduino senses the magnet come around.
// It won't be exactly 1 detect:1 revolution unless you add debouncing code.
volatile int num_detects = 0;

//** Exercise detecting variables
// The number of seconds in a day
time_t day_of_sec = 24*60*60;

// The time we need to spend on the bike to make it count as exercise.
time_t exer_target_time = 0;
// If you exercise over a minute, then you have exercised (low standards).
time_t exer_elapsed_time = 60;
// This variable holds the time that is 24 hours after the last exercise.
time_t exer_trigger_time = 0;

//** RPM tracking functions
void wheel_detect()
{
num_detects++;
}

void clear_wheel_detect()
{
num_detects = 0;
}

//** Exercise detecting functions
void check_immediate_exercise()
{
if (num_detects > 0 )
{
if(exer_target_time == 0)
{
// This is the first exercise activity that we have detected in a while.
// Mark the time that'll be our threshold for saying that we've exercised.
time_t exer_start_time = now();
exer_target_time = exer_start_time + exer_elapsed_time;
}
else if( now() > exer_target_time)
{
// Exercise detected!
// We will hold off the Twine message for a day.
exer_trigger_time = now() + day_of_sec;
}
} else {
if(exer_target_time != 0)
{
// We need to clear out the target time because exercise has stopped.
exer_target_time = 0;
}
}
}

void verify_daily_exercise()
{
if(exer_trigger_time < now())
{
cloud.trigger(); // Trigger the rule on Twine that sends a message.
exer_trigger_time = now() + day_of_sec;
}
}

//** Main functions
void setup() {
// Interrupt 0 means different pins on different Arduinos. (For mine, pin 2.)
attachInterrupt(0, wheel_detect, FALLING);
}

void loop() {
verify_daily_exercise();
check_immediate_exercise();
clear_wheel_detect();
// wait a second
delay(1000);
}

```

5. Verify and upload the code to your Arduino. This code will trigger the Cloud Shield every 24 hours after you last finished riding your bike.

6. Plug the cloud shield into your Twine. Make sure both ends of the cable are firmly connected.

7. Configure your Twine to notify you when you’re not keeping up with your exercise. Like this:

I set mine up to send me an email but I may add texts in the future if I am ignoring my emails too much. Now I can try to be a bit more consistent with my workouts. Hurray!

1. dexterpeng reblogged this from supermechanical