This is a very special recitation, off line, and also for me, off Arduino. However, thanks to Tracy and Robbin for taking me in, I got to work with them and overcame this tough time.
I built the circuit by myself according to this diagram. And later tested the code with Tracy.
From our observation, when the tilt is standing straight, the numbers shown on the serial monitor are 1, and when the tilt lays down, the serial monitor turns to 0.
For the questions part:
- At approximately what angle of tilt does it transition between HIGH and LOW?
- I think it’s about 45 degree.
- What else do you notice about its behavior?
- The switch of numbers on the monitor is really regular when the tilt is moved from 90 degrees and 0 degree, but less sensitive in sensing the middle angle ranges in between. So I think this tilt can be more efficient when it’s applied to something that moves in a big angle.
- What if you tilt the forearm?
- When I hold it and tilt my forearm, the numbers on the serial monitor change from 1 to 0 and to 1, and repeat.
- What if you hold the wires several centimeters away and tilt it?
- The speed of numbers changing is faster.
- What if you shake it?
- Depending on the range of angles I shake it, when the angle is small, the tilt sometimes can’t sense the change and the numbers remain 0 or 1, but if I shake it in a big angle, the numbers change accordingly.
Workout1:
// constants won't change. They're used here to set pin numbers: const int buttonPin = 6; // the number of the pushbutton pin int buttonState; // the current reading from the input pin int lastButtonState; // the previous reading from the input pin int counter = 0; // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. 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(buttonPin, INPUT_PULLUP); Serial.begin(9600); } void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin); // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); //Serial.println(buttonState); } if ( (reading != buttonState) && (millis() - lastDebounceTime) > debounceDelay) { if ( reading == 1) { counter = counter + 1; Serial.println(counter); } if (counter >= 8) { counter = 0; Serial.println("Yay, you’ve done a set of curls"); } buttonState = reading; } // save the reading. Next time through the loop, it'll be the lastButtonState: lastButtonState = reading; //if you want to use Serial Plotter, add these two lines: //Serial.println(buttonState); //delay(1); }
Workout2:
Questions:
Q1: Can you make changes to the speed of your “jumping jack” to detect it?
Yes, but as us speed up, the sensor might miss some comps.
Q2:What happens if you change the debounce lockout time? What do you need to change it to detect one “jumping” exercise?
It works when we changed it in a small range, however it missed more jumps as we changed it into a large number.
Q3:How reliable is the counting? How many extra, or missed, counts happen as a fraction of the number of real exercises done? What do you think would happen to these fractions if you adjust the debouncing time).
I think it is not very reliable, for the miss of jumps it have. Maybe it will be more precise if we adjust the debouncing time, however, it might turns out to be worse as well.
Workout3:
// constants won't change. They're used here to set pin numbers: const int buttonPin = 6; // the number of the pushbutton pin int buttonState; // the current reading from the input pin int lastButtonState; // the previous reading from the input pin int counter = 0; int duration = 0; // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled unsigned long debounceDelay = 70; // the debounce time; increase if the output flickers void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT_PULLUP); delay(1000); Serial.println("Start your Workout"); } void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin); duration = millis(); // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); //Serial.println(buttonState); } if ( (reading != buttonState) && (millis() - lastDebounceTime) > debounceDelay) { if (duration <= 21000) { if (reading == 1) { counter = counter + 1; Serial.println(counter); } } else if (duration > 21000 && counter != 0){ counter = 0; Serial.println("Stop, your time is up!"); } buttonState = reading; } // save the reading. Next time through the loop, it'll be the lastButtonState: lastButtonState = reading; //if you want to use Serial Plotter, add these two lines: //Serial.println(buttonState); //delay(1); }
Illustrating sketch: