In recitation 3, we build a tilt sensor and complete some workouts with the sensor on.
First, we prepare the tilt sensor, which sense the direction and provides feedback based on it. When you change the orientation from up to down, the tilt transition from HIGH to LOW.
Piling off the plastic outside, we wrap the wires around the sensor before soldering. This step helped us to solder the wires as stable as possible.
The middle green stuff is the tilt sensor, through these steps it can be connected to the circuit. The diagram and our finished circuit are shown below.
int SENSOR_PIN = 2; int tiltVal; void setup() { pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INP Serial.begin(9600); } void loop() { // read the state of the sensor tiltVal = digitalRead(SENSOR_PIN); Serial.println(tiltVal); delay(10); }
Implementing the code above allows the tilt sensor to give feedback in the serial monitor, 1 when the direction is up and 0 down.
In order to make the Arduino print 1 and 0 when the tilt sensor changes from 0 to 1 and 1 to 0 respectively, we then refined our code.
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); }
The if condition restricts when we should print 0 or 1. We then did some workouts wearing the tilt sensor. But first we added a conditional into the code so that it will print a message after a full biceps curl was done. Then we set a limit to the count, so that another message is displayed when we done 8 biceps curl. Finally, we use a buzzer to alarm at the same time we finished.
int SENSOR_PIN = 2; int tiltVal; int prevTiltVal; int count=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 (tiltVal != prevTiltVal) { prevTiltVal = tiltVal; count=count+1; if (count%2==0){ Serial.println("You have finished a biceps curl."); } if (count%16==0) { Serial.println("Yay, you have done one set of curls."); tone(8,440); } else{ noTone(8); } } delay(10); }
I also tied the sensor to my thigh and do a few squat, finding the sensor works in this circumstances, because the direction of my thigh changes.
In reflection, the tilt sensor is a useful and interesting gadget. Sometimes, in order to save time and tape, we hold the wires and tilt it, because the sensor only test the direction rather than how the direction is changed. In the coding part, I tried to set the count to 0 every time I finished a full biceps curl and the conditional is “if (count ==2)”, however, this method didn’t apply if I want to display another message when the total times reach 8, so I change my strategy and conditional statement.
The tilt sensor can be used in most workouts, but for those doesn’t include changing of direction, it won’t work.