Step 1: Prepare your tilt sensor
During the process, the most challenge part might be keeping two wires’ tips and the feet of my tilt sensor stable when weilding it. Because the only thing I can use to press them together was the thin solder tip with two hands holding it and the tin wire. To solve it, I used more paper tape than it shown on the instrocutive picture on the website, pasting the sensor’s body and two wires’ tips on the cardboard at the same time. Unfortunately, for this part, I forgot taking a picture, but it worked well 😛
In the As for the splicing part, everything went well.
Step 2: Refine the code
To realize the basic function of digitalRead()
of :
int SENSOR_PIN = 2;
int tiltVal;
int prevTiltVal;
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) {
Serial.println(tiltVal);
prevTiltVal = tiltVal;
}
delay(10);
}
I tried to use analogWrite() instead of digitalWrite() to make a conditional interaction with the sensor: only reaching certain angle can make the movement counted into the amount.
At that time, ‘0’ corresponded to High and 1023 corresponded to Low. But later I found even a quick but tiny movement at a low angle can make the analogWrite() printed as 0 or a low number.
It is something I can explore more about!
And for the recoding, take a look at this:
int SENSOR_PIN = A0; int tiltVal; int prevTiltVal; int count=0; int precount=0; 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 (prevTiltVal==1 and tiltVal==0){ count=count+1; } if (tiltVal != prevTiltVal) { // Serial.println(tiltVal); prevTiltVal = tiltVal; } if (count != precount) { // Serial.println(tiltVal); precount = count; if (count==8){ Serial.println(count); Serial.println("Yay, you've done one set of curls"); count=0; }else if(count>0){ Serial.println(count); } } delay(100); }
I added a reminder for a completed set of exercise and make a automatically refreshing function to start counting for a new set!
Step 3: Wear your sensor & Step 4: Bicep Curl Workout!
Instructions:
To be honest, I’m still not sure how to make it only count the motion with full amplitude. But as an instruction, it should ask the user to try their best reaching the proper position, which is not merely good for a better effect of this exercise but more importantly to protect the user from injuries.
Additionally,
I found it can be used in more way other than simply on my arm!
It can also be used to test the quality of fingers training!
Step 5: Exercise Challenge
For the use of buzzer, I refer to the melody example introduced in our previews class and add the playing part inside the condition of “count==8”–when the set is finished.
int SENSOR_PIN = 2; int buzzer_PIN = 7; int tiltVal; int prevTiltVal; int count = 0; int precount = 0; // code for the melody. Reference: https://blog.jmaker.com.tw/arduino-buzzer/ #include "pitches.h" int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; //melody code void setup() { pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin pinMode(buzzer_PIN, OUTPUT); //set buzzer_pin as an output pin pinMode(motor_PIN, OUTPUT); Serial.begin(9600); Serial.println("Let's start exercise!"); } void loop() { // read the state of the sensor tiltVal = digitalRead(SENSOR_PIN); // if the tilt sensor value changed, print the new value if (prevTiltVal == 1 and tiltVal == 0) { count = count + 1; } if (tiltVal != prevTiltVal) { // Serial.println(tiltVal); prevTiltVal = tiltVal; } if (count != precount) { // Serial.println(tiltVal); precount = count; if (count == 8) { Serial.println(count); Serial.println("Yay, you've done one set of curls"); //melody code for (int thisNote = 0; thisNote < 8; thisNote++) { //計算每個音的長度,4分音符: 1000 / 4,8分音符:1000/8 int noteDuration = 1000 / noteDurations[thisNote]; tone(7, melody[thisNote], noteDuration); //tone(PIN腳,音調,拍子) // 每個音之間要停一小段時間,範例是建議拍子的長度加30% int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(7); // 停止播放 } //melody code count = 0; Serial.println("let's start a new set!"); } else if (count > 0) { Serial.println(count); } } delay(100); }
- Record a short video of yourself doing step 4 (bicep curls) and 5 (challenge) while showing the Serial monitor/plotter in action.
- Embed your code into your blog as shown with the example codes in this recitation, NOT by taking a screenshot of it.
- Draw your own illustration sketches that showcase how a person would use this interactive training device.
- Reflect on your experiments, explaining the notes you took, and adding photos, and/or screen shots to document what tests you have tried and what you have learned from them. More questions I want to write down:
At what angle of tilt does it transition between HIGH and LOW?
Mostly it was senstive to transition; When I kept rotating it at a low speed, it will keep show changes with my big movement. But when I sped up, only a small angle can be reflected into transition.
int SENSOR_PIN = 2; // int motor_PIN = 4; /* Vibration Motor with Arduino For more details, visit: https://techzeero.com/arduino-tutorials/vibration-motor-with-arduino/ */ int buzzer_PIN = 7; int tiltVal; int prevTiltVal; int count = 0; int precount = 0; // code for the melody. Reference: https://blog.jmaker.com.tw/arduino-buzzer/ #include "pitches.h" int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; //melody code void setup() { pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin pinMode(buzzer_PIN, OUTPUT); //set buzzer_pin as an output pin pinMode(motor_PIN, OUTPUT); Serial.begin(9600); Serial.println("Let's start exercise!"); } void loop() { // read the state of the sensor tiltVal = digitalRead(SENSOR_PIN); // if the tilt sensor value changed, print the new value if (prevTiltVal == 1 and tiltVal == 0) { count = count + 1; } if (tiltVal != prevTiltVal) { // Serial.println(tiltVal); prevTiltVal = tiltVal; } // if (count == 0) { // Serial.println("Let's start a new set!"); // } if (count != precount) { // Serial.println(tiltVal); precount = count; if (count == 8) { Serial.println(count); Serial.println("Yay, you've done one set of curls"); //vibration // digitalWrite(motor_PIN, HIGH); // delay(800); // digitalWrite(motor_PIN, LOW); // delay(500); // digitalWrite(motor_PIN,HIGH); // delay(800); // digitalWrite(motor_PIN,LOW); // delay(500); //melody code for (int thisNote = 0; thisNote < 8; thisNote++) { //計算每個音的長度,4分音符: 1000 / 4,8分音符:1000/8 int noteDuration = 1000 / noteDurations[thisNote]; tone(7, melody[thisNote], noteDuration); //tone(PIN腳,音調,拍子) // 每個音之間要停一小段時間,範例是建議拍子的長度加30% int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(7); // 停止播放 } //melody code count = 0; Serial.println("let's start a new set!"); } else if (count > 0) { Serial.println(count); } } delay(100); }