A short video of myself doing step 4 and 5
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: Exercise Challenge
As what is suggested on the website, I plugged in a buzzer to my circuit, so that it could beep whenever the person finishes a set of Bicep Curl.
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); }
Reflection on my program
I tried to turn the sensor with my hand, and I found that the tilt sensor could only detect the movement which is over 180 degrees. I tried simply turn the sensor in 90 degrees, and I found that nothing happened.
And then I tried to shake the sensor, the sensor did not show any value. I guess the sensor might not be sensitive enough for a small movement. Only if I raised or put down my arms slowly, the sensor could detect the movement I made.
As small movement could not be detected by the tilt sensor, I think it is not suitable for elderly or people who are disabled. Bending their limbs over 180 degrees seems to be difficult for them.