In this week’s recitation, our goal is to learn how to use the tilt switch.
At first, the result looked like this, which is quite difficult to observe.
After asking Vivian for help, I changed the delay code and finally got the correct interface.
- At approximately what angle of tilt does it transition between HIGH and LOW?
I think it’s transit at around 45 degrees and 135 degrees.
- What else do you notice about its behavior?
There were still some tiny times that the tilt switch was not that accurate.
- What if you tilt the forearm?
I think the result was quite similar to the last experiment.
- What if you hold the wires several centimeters away and tilt it?
The tilt switch became less accurate and sensitive.
- What if you shake it?
The serial monitor represented a very rapid change.
Workout 1: Biceps Curls
// 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 != buttonState) { // reset the debouncing timer lastDebounceTime = millis(); Serial.println(counter); } if ((reading != buttonState) && (millis() - lastDebounceTime > debounceDelay)) { if (counter < 8) { counter++; Serial.println(" curls, keep going!"); } if (counter >= 8) { counter = 0; Serial.println("Yay, you've done a set of curls"); } } lastButtonState = reading; //if you want to use Serial Plotter, add these two lines: //Serial.println(buttonState); //delay(1); }
Workout 2: Jumping Jacks
- Can you make changes to the speed of your “jumping jack” to detect it?
Of course. Just like I could did the Biceps Curls faster to change the speed.
- What happens if you change the debounce lockout time? What do you need to change it to detect one “jumping” exercise?
- 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).
-
-
Workout 3: Start and Stop Timing
// 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 = 50; // the debounce time; increase if the output flickers void setup() { pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); Serial.println("Start your Workout"); } 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 != buttonState) { // reset the debouncing timer lastDebounceTime = millis(); Serial.println(counter); } if ((reading != buttonState) && (millis() - lastDebounceTime > debounceDelay)) { if (duration < 20000) { if (reading == 1) { counter++; } else if (duration >= 20000 && counter != 0) { counter = 0; Serial.println("Stop, your time is up!"); } lastButtonState = reading; //if you want to use Serial Plotter, add these two lines: //Serial.println(buttonState); //delay(1); } } }
Leave a Reply