Step 4: Bicep Curls
Code for Step 4:
int SENSOR_PIN = 2; int tiltVal; int prevTiltVal; int count; void setup() { pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin Serial.begin(9600); } void loop() { // read the state of the sensor tiltVal = digitalRead(SENSOR_PIN); // if the tilt sensor value changed, print the new value if (tiltVal != prevTiltVal) { if (tiltVal == 1) { count = count +1 ; Serial.println(count); } prevTiltVal = tiltVal; } if (count == 8 ) { Serial.println("Yay, you’ve done one set of curls"); count=0; } delay(10); }
Step 5 : Challenge (Arm Raise Exercise)
I added a buzzer to the circuit, and wrote the code that will allows the buzzer to play a melody after a set of workout(8 counts) is completed.
Initially, I was trying to design a jumping jacks with the sensor. However, after doing jumping jacks with the sensor fixed on my arm, the numbers on the Serial Monitor tab are not showing up accurately according to my movements. It seems that the sensor doesn’t work well when jumping movements are involved. So, I changed my workout to arm raise exercise instead.
Video of me doing the arm raise exercise:
Code for Step 5:
int SENSOR_PIN = 2; int tiltVal; int prevTiltVal; int count; int buzzerPin = 8; void setup() { pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin Serial.begin(9600); pinMode(buzzerPin,OUTPUT); } void loop() { // read the state of the sensor tiltVal = digitalRead(SENSOR_PIN); // if the tilt sensor value changed, print the new value if (tiltVal != prevTiltVal) { if (tiltVal == 1) { count = count +1 ; Serial.println(count); } prevTiltVal = tiltVal; } if (count == 8 ) { Serial.println("Yay, you’ve done one set of arm raise!"); count=0; playMelody(); } delay(10); } void playFreq(double freqHz, int durationMs) { //Calculate the period in microseconds int periodMicro = int((1 / freqHz) * 1000000); int halfPeriod = periodMicro / 2; //store start time long startTime = millis(); //(millis() - startTime) is elapsed play time while ((millis() - startTime) < durationMs) { digitalWrite(buzzerPin, HIGH); delayMicroseconds(halfPeriod); digitalWrite(buzzerPin, LOW); delayMicroseconds(halfPeriod); } } void playMelody() { playFreq(262, 100); playFreq(294, 100); playFreq(349, 100); playFreq(294, 100); playFreq(440, 200); delay(100); playFreq(440, 400); playFreq(392, 400); delay(300); playFreq(262, 100); playFreq(294, 100); playFreq(349, 100); playFreq(294, 100); playFreq(392, 200); delay(100); playFreq(392, 400); playFreq(349, 400); }
Sketch: How a Person Would Use It
Reflections on My Experiments
Like the picture shown on the left, I tried exercises including jumping jacks and sit-ups, and both of them didn’t work well with the sensor. First, when I did sit-ups with the sensor fixed on my belly, I noticed that the sensor doesn’t show any value. I took off the sensor from my arm and observe the transition of value with different angles, and it turned out that it will only transition between HIGH and LOW when you tilt it at least 180 degree. Since sit-ups only involve 90degree angle, the sensor will not work.
Through experimenting jumping jacks, I figured out that the sensor will not show the right count when you have shaking or jumping movements. It works when you rotate the limb or perform an arm circle.
When I finalize my design to arm-raise exercise, I tried moving my arms really fast to finish the set in a shorter amount of time. Unfortunately, the sensor seems to only work when you move slowly, or else, it will not show the right numbers of count.