Personal Exercise Trainer (Beginners Microcontroller Project)

Published

Introduction: Personal Exercise Trainer (Beginners Microcontroller Project)

Summary:

The goal of this instructable is to create a device that will help to deliver a consistent exercise routine for the user of an exercise bike.

The device will:

-Allow the user to maintain effort by flashing an LED and sounding a beep at a constant rate.

-Increase and decrease the rate at regular intervals to simulate the occasional hill.

-Signal that the workout is over.

This is my first Instructable. I wanted to make it a teaching tool for someone new to microcontrollers. I had four goals:

-Make a useful device

-Make the programming as easy as possible for those just beginning to learn how to use microcontrollers

-Keep the circuit simple with no exotic parts

-Allow the device to be built in several formats to accommodate various levels of skill and interest.

The device is merely a timer that maintains a cadence for exercise. At variable intervals it increases and then decreases the rate of effort to promote cardiovascular health. I have been using it for several months with an exercise bicycle, and I found that it gives me a very satisfactory workout. If I am not sweating enough by the end of the workout, I can speed it up for the next time by changing the program.

Parts The basic parts list is:

1 -Attiny13A microcontroller(You could use any 8 pin Attiny)

3 -LEDs of different colors

1 -0.1 uf capacitor (exact value is not too important)

1 -buzzer ( Here is one example.) https://www.ebay.com/itm/10pcs-2-Pins-Black-14-7-... )

1 -breadboard

1 -8 pin socket (If you go beyond just using a breadboard)

1 -3 volt battery supply.(2 aa batteries, 2 aaa batteries, 1 3 volt lithium ion coin battery, etc.)

Step 1: The Circuit

Step One: Build the Circuit

You have several choices for building the circuit. You can build the circuit on a breadboard (the easiest and fastest),

or a perfboard

or a stripboard (to practice soldering),

or even on your own pcb (printed circuit board).

1. Attach the positive end of the buzzer to leg 5 of the Attiny13a, and the other end to ground (leg 4).

2. Attach the positive end of one of the l.e.d.s to the same leg, leg 5, and the other end to ground (leg 4).

3. Attach the positive end of one of the l.e.d.s to leg 6 and the other end to ground (leg 4).

4. the positive end of one of the l.e.d.so leg 7 and the other end to ground (leg 4).

5. Attach the positive end of the 3 volt power supply to leg 8 of the Attiny.

6. Attach the negative end of the power supply to leg 4 of the Attiny.

7. Attach a capacitor between leg 8 and leg 4.

Step 2: Write the Code

You may use my code (but I am not a programmer so it is pretty sloppy.) Or you may modify my code or write your own.

The code merely blinks a light and sounds a buzzer at a rate of about 100 beats per minute. After about 8 minutes it starts blinking the second light and increases the rate for about 30 beats and then starts blinking the third light and increases again for about 65 beats. Then it slows a bit for 30 beats and finally falls back to the 100 beat per minute rate. The blinking of the second and third light helps the user to know when he is in “sprint” mode. After half an hour of exercise, 5 long buzzes let you know that the ride is over for the day.

One thing to keep in mind if you are new to programming Attiny chips or (any chips) - the leg that is used for input or output is called a ‘pin’ in the program, and it does NOT have the same number as the leg number in the pinout diagram in step one..

The Attiny chip has 8 legs arranged in a counter-clockwise order

_1| |8_

_2| |7_

_3| |6_

_4| |5_

It has 6 input/output pins,

Pin 0 is leg 5 on the pinout diagram in step one.

Pin 1 is leg 6 on the pinout diagram.

Pin 2 is leg 7 on the pinout diagram.

Pin 3 is leg 2 on the pinout diagram.

Pin 4 is leg 3 on the pinout diagram.

Pin 5 is leg 1 on the pinout diagram (leg 1 is also use for ‘reset’.)

Here is my code. I included a lot of comments to help those new to programming in c.

--------------------------------------------------------------

// PET.c

//Personal Exercise Trainer

//Exercise Bike Cadence with Sprints

//Commented Version

//Starts at 100 beats per minute

//At 8 minutes increases to 115 bpm

//At 8.5 minutes increases to 130 bpm

//666 = 10 beats in 6.00 secs -- .600 sec interval

// IMPORTANT!!!

// I was able to cut and paste this code into a text editor

//and it worked fine with one major exception--

//the Instructables editor does not like carrots (those little symbols

// above the period and comma on your keyboard),

// and it erases them and everything inside of them.

//In the first three lines of the actual program, replace "leftCarrot"space with <

//And replace space"rightCarrot" with >

//The actual program starts on the next line

#include leftCarrot avr/io.h rightCarrot

#includeleftCarrotutil/delay.h rightCarrot

#include leftCarrot avr/power.h rightCarrot

int main(void) {

DDRB=0b11111111; //sets all pins for output

int flash; //number of flashes

int TotalCount = 0; // TotalCount controls the length of the workout. This line begins it at 0

//The next three program lines just run once to signal start.

PORTB = 0b00000111;

// Turns on buzzer and three leds

//Note that the buzzer and 1 led are on the same leg (leg 5, pin 0)

// That is the far right position in 0b00000000

// FYI pin 1 is the next position to the left - 0b00000000

// And pin 2 is the next position to the left - 0b00000000 etc.

_delay_ms(3000); // Three second delay

PORTB = 0b00000000; // Turns off all pins

while (1)

{

_delay_ms(33); //This line is optional

for ( flash = 0; flash < 800; flash++ ) //800 is the number of flashes until sprint speed

//Decrease it to give less time between sprints.

{

if (TotalCount >= 3155) { //3155 gives a 30 minute workout . Change 3155 to vary the length of the workout

for ( flash = 0; flash < 5; flash++ ) // Repeats 3 second buzzer 5 times at end of workout.

{

PORTB = 0b00000011;

_delay_ms(3000);

PORTB = 0b00000000;

_delay_ms(2000);

}

TotalCount = 0; // Restarts the workout

}

//The lines below define the standard cadence of about 100 beats per minute and speed up the workout every eight minutes

PORTB = 0b00000001; //Turns on beeper and led on leg 5 (pin 0)

_delay_ms(33);

PORTB = 0b00000000; //Turns off after short flash and beep

_delay_ms(666);

TotalCount++;

}

_delay_ms(33); //This line is optional

for ( flash = 0; flash < 30; flash++ ) //30 beat transition to sprint speed

{

PORTB = 0b00000011; //Turns on additional second led on leg 6 (pin 1)

_delay_ms(33); //This line is optional

PORTB = 0b00000000;

_delay_ms(583); //half difference between normal and sprint speed (666 * .875)

TotalCount++; }

_delay_ms(33); //This line is optional

for ( flash = 0; flash < 65; flash++ ) //Sprint for 65 beats. Increase the 65 to lengthen the sprint time

{

PORTB = 0b00000111; //Turns on additional third led on leg 2 (pin 2)

_delay_ms(33);

PORTB = 0b00000000;

_delay_ms(500); // sprint speed (666 * .75). Decrease the 500 for faster sprint

TotalCount++; }

_delay_ms(33); //This line is optional

for ( flash = 0; flash < 30; flash++ ) //30 beat transition back to normal speed

{

PORTB = 0b00000011;

_delay_ms(33);

PORTB = 0b00000000;

_delay_ms(583); //half difference between normal and sprint speed (666 * .875)

TotalCount++; }

_delay_ms(33); //This line is optional

}

return (0);

}

Step 3: Flash the Code to the Chip

This is the amazing part and there are several ways to do it. I am not going to try to explain each of the methods of programming the chip. Instead I will refer you to other instructables and tutorials which do a much better job of explanation than I could.

Personally, I first used a Raspberry Pi and this Instructable.

https://www.instructables.com/id/Programming-the-AT...

This is the instructable that I used to upload my first program to a chip. It is clear and well written. There is really no difference in programming the ATtiny13A and the ATtiny85. You just have to change the name of the chip in AVRDude (You will know what that is once you read his Instructable.)

But it is just as easy to use an Arduino or a USB programmer attached to your computer.

Here is one Instructable which uses an Arduino:

https://www.instructables.com/id/Program-an-ATtiny...

And here is one (not an Instructable, but a very good tutorial) which uses a usb programmer:

https://learn.sparkfun.com/tutorials/tiny-avr-pro...

Step 4: Make It Better

Once you have wired the breadboard, and programed the chip and plugged it in, all you have to do is plug in the battery and start riding. I first used my PET (Personal Exercise Trainer) this way for weeks, and it worked just fine.

After a while, I started a making a few enhancements. The pictures below show some of the improvements that I made to my device. Please excuse their being upside down; they were right side up when I uploaded them.

First I added a switch so I didn’t have to plug in a wire each time.

Next, I built the circuit on a stripboard (see picture at the beginning of instructable) and soldered in the parts so I could use my breadboard for other things. If you go to this step, you will want to add a socket for the chip so that you can remove it to change the program.
Then I made a printed circuit board just for the heck of it (see picture at the beginning of instructable). Here is a very good instructable on that. https://www.instructables.com/id/Stop-using-Ferric... (Besides giving great advice on etching pcbs, Elliot Andrews is a real expert on using AVR chips. I highly recommend his book: Make AVR Programming: Learning to Write Software for Hardware)

Finally, I designed and printed a case to make it look nice.

Good luck, and have fun.

Microcontroller Contest

This is an entry in the
Microcontroller Contest

Share

    Recommendations

    • Woodworking Contest

      Woodworking Contest
    • Casting Contest

      Casting Contest
    • Make it Move Contest

      Make it Move Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments

    Good job on your first Instructable!