For this week’s recitation, we did some workout! I used the tilt switch from your kit to build a wearable workout sensor, do a workout exercise, and complete a challenge. I redefined my code and make the workout more interactive.
Here are the Materials I used:
From the kit:
- 1 * Arduino Uno
- 1 * USB A to B cable
- 1 * breadboard
- 1 * tilt switch
- 1 * 10k ohm resistor
- A handful of M/F, M/M, and F/F cables
From course supplies:
- 2 long wires (about 1.8 m)
- 1 capacitor 0.1 uF (confirm size)
Step 1: Prepare my tilt sensor
Task #1: Solder the sensor to two long wires.
I didn’t realize at first that the cardboard played a fixed role and wasted a lot of time. I tried to bind the wires together by winding them but found it difficult to do so, and then had to use soldering, but I didn’t use wire at first, so the wires themselves were particularly difficult to melt. With the help of the professor, I finally managed to finish the soldering job.
Task #2: Connect the tilt sensor cables to my Arduino using a capacitor.
Capacitor can make the current more stable and not affect the function of using appliances. When my capacitor was out of circuit, the current was very unstable and the subsequent operation failed.
Task #3: I programmed my Arduino with the sketch and it worked.
Step 2: Refine the code
Task #1:I uploaded the following sketch to only print 1 when the tilt sensor changes from 0 to 1, and 0 when from 1 to 0. For example, the code we used before might have printed 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 as you move the sensor between upright and upside-down, but now code should print 0 1 0 1.
Step 3: Wear your sensor
I used a lot of paper tape to fix the tilt switch to my body, making it a wearable sensor. The sensor was along my forearm, so that I can use it to detect when my forearm is facing up.
Step 4: Bicep Curl Workout!
Task #1: Add a conditional to my sketch so that it shows a message on the Serial Monitor ONLY when a full biceps curl has been completed. But I forgot to switch from the Serial Plotter to the Serial Monitor so it didn’t work at first.
1.Identify the proper section in your code where you can add code when transitions happen.
2.Add a conditional that has the criteria so that you only detect transitions LOW to HIGH, showing only when the user has raised their arm completely.
Task #2: Count the curls, adding one to its value every time that the user does a complete bicep curl.
1.Declare a global variable.
2.Add one to its total every time that a LOW to HIGH transition has happened.
3.Modify the Serial.print and Serial.println instructions so that the value of this new variable is shown on the Serial Monitor.
Task #3: Set a limit to the variable you are using to count the curls, so that the final code has the following behavior.
1.When the total reaches 8, Print “Yay, nail one set”.
2.Reset the count, so that the behavior repeats at 16 curls.
When I first tried it, it would count with the movement of the arm, but there was a situation where the movement counted several times at one time. I thought at first that the small ball in the tilt device was too sensitive and would count because of the vibration, but later found that I should paste it vertically instead of horizontally.But it still did not count as we predicted.
I asked the professor for help. We found that when we wrote the code, we did not put part of the code into the loop of the if condition, causing the motion count to be recalculated each time causing errors. I envisioned that each set of movement would complete eight arm movements, but the actual code I needed to write the data was 9. When a set of movement was completed, there would be a prompt, and I wanted to be able to stop the movement after completing 4 sets, but the code never worked. So I first disable part of the code, using 0 1 serial monitor to see the problem with the other parts of my writing, and found the if condition is wrong. I have a small set of loops in 8, and the actual number of lifts is a different variable, and the two loops are in different locations. I changed my code and eventually it worked.I also tried to change the position of LOW and HIGH, so that after completing a lift is above will count, or hands naturally down will count.
Step 5: Exercise Challenge
My partner and I then added a buzzer to the programming code, at which point the bug reappeared; it did not sound after a set of eight actions was completed and paused after it sounded. Instead it would keep buzzing and the count was wrong. We enlisted the help of our professor and he helped us rewrite the code. At this point, whenever a set was completed, the buzzer would continue to chirp for one second after being affected by the delay code and then stop and start counting again.
Here are some of the wrong codings.
And here are my final codings.
int sensorPin = 2; int tiltVal; int number = 1; int actualNumber = 0; void setup() { Serial.begin(9600); Serial.println("Let do this!"); pinMode(sensorPin, INPUT); } int pretiltVal; void loop() { int tiltVal = digitalRead(sensorPin); if (tiltVal != pretiltVal && tiltVal != HIGH) { Serial.println(number); number++; actualNumber++; } pretiltVal = tiltVal; if (number == 9) { Serial.println("yay,nail one set!"); tone(8, 300); delay(1000); noTone(8); number = 1; } if (actualNumber > 32) { Serial.println("Now u should rest!"); } delay(5); }
When I kept shaking the tilt sensor, the count goes haywire! When I broke the wire with my hand, the computer also succeeded in counting I guess because when the sensor ball vibrates once it will count once, and when we shake the sensor, the ball will continue to vibrate. So we have to move more each time, try to do it perpendicular to the ground, and the help of the capacitor to maintain a stable current.
This led me to the following design. The user needs to strap the sensor to his wrist, and when he holds the dumbbell, he needs to first relax naturally perpendicular to the ground, and when he raises his arm up, he will complete a half movement. When his arm is naturally perpendicular to the ground again, a count will be completed. Every eight times, a buzzer will sound to remind him that when he finishes four sets of exercises, he can take a short break!