We tried the tilt switch for the first time. And we used the serial plotter to observe some charateristics of it.
If tilting the switch nearly 90 degree, it goes from ON to OFF.
If tapping on the switch, the wave flickers.
The result of tilting fingers and tilting forearm is similar.
If I hold the wires and not the switch and tilt it, the signals become more unstable. There are lots of transitions in between tilts.
If I shake it with holding hands holding the wires, it misses lots of signals. If I grab it by the top and shake it, the waves are more accurate.
Workout STARTS
We debounced the swtich first, so the unintended transitions didn’t be affected the counts.
Biceps curls
When I finished the first version of the code, I found that the system counted on every switch of actions(e.g., 1 on up and 2 on down). But I wanted it to be counting on each up-down which means a full curl. Then Andy helped me with modifying the code. I added “if (reading == HIGH)” in the code, so it only counted when the tilted switch was on.
const int SENSOR_PIN = 6; int tiltVal; int prevTiltVal; int curls = 0; unsigned long lastDebounceTime = 0; // the last time the output pin was toggled unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin Serial.begin(9600); } void loop() { int reading = digitalRead(SENSOR_PIN); // if the tilt sensor value changed, print the new value if (reading != prevTiltVal) { lastDebounceTime = millis(); //Serial.println(tiltVal); } if ( (reading != tiltVal) && (millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state: tiltVal = reading; if (reading == HIGH) { curls = curls + 1; Serial.println(curls); } } if (curls == 8) { Serial.println("Yay, you've done a set of curls"); curls = 0; } prevTiltVal = reading; //for Serial Plotter use //Serial.println(tiltVal); //delay(1); }
Jumping jacks
The previous code correctly counted for jumping jacks. But if I speeded up, it didn’t counted accurately. We can see in the Serial Plotter that when the tilt switch is on, the wave is not stable. So I tried to decrease the number of the delaydebounce time, and the counts was able to keep up with the faster movement. However, it sometimes included extra counts. And if I increased the debounce lockout time, it missed counts. The best debounce time was 70.
Timing the workout
const int SENSOR_PIN = 6; int tiltVal; int prevTiltVal; int curls = 0; int duration = 0; unsigned long lastDebounceTime = 0; // the last time the output pin was toggled unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin Serial.begin(9600); delay(1000); Serial.print("Start your workout"); } void loop() { int reading = digitalRead(SENSOR_PIN); duration = millis(); // if the tilt sensor value changed, print the new value if (reading != prevTiltVal) { lastDebounceTime = millis(); //Serial.println(tiltVal); } if ( (reading != tiltVal) && (millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state: tiltVal = reading; if (duration <= 20000) { if (reading == HIGH) { curls = curls + 1; Serial.println(curls); } } else { Serial.println("STOP,time is up"); curls = 0; } //if (curls == 8) { //Serial.println("Yay, you've done a set of curls"); //curls = 0; } prevTiltVal = reading; //for Serial Plotter use //Serial.println(tiltVal); //delay(1); }