Recitation3: Workout
1.I connected the tilt switch with the two long wires and soldered them.
2.I connected the circuit according to the sketch
.
3.I wrote the first program so that it will count a transition between LOW and HIGH instead of count every time it changed. The code is as follows.
int SENSOR_PIN = 2;
int tiltVal;
int prevTiltVal;
int a;
//a is used to show a trisition between LOW and HIGH.
int count;
void setup() {
pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin
Serial.begin(9600);
a=0;
count=0;
}
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);
a=a+1;
prevTiltVal = tiltVal;
if(a==2){
count=count+1;
Serial.println(count);
a=0;
}
}
delay(10);
}
And here’s my illustration video (sorry for awful recording, but it did work)
4. I adapted the code so that it fits all the requests, which is as follows
int SENSOR_PIN = 2;
int tiltVal;
int prevTiltVal;
int a;
//a is used to show a trisition between LOW and HIGH. It is the 'count in the previous program'
int count;
int b;
void setup() {
pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin
Serial.begin(9600);
a=0;
count=0;
b=0;
}
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);
a=a+1;
prevTiltVal = tiltVal;
if(a==2){
count=count+1;
b=b+1;
Serial.println(count);
a=0;
}
}
if(b==8){
Serial.println("Yay, you’ve done one set of curls");
b=0;
}
delay(10);
}
Reflection:
the process of connecting the wires and soldering the sensor is pretty good. However, I had difficulties adapting the code, which is because I mistaken “==” with”=”, with the help of my peers, I discovered it and corrected it.
I think the sensor changes when the angle is around 30 degrees.
It will only switch between “HIGH” and “LOW” when tilted to one side, if it’s tilted to another side, nothing will happen.
I don’t think it will be used by many users, the accuracy of the sensor is not guaranteed and the wires will disturb the exercising and will be hard to tackle with.
Leave a Reply