Recitation 5

This week’s recitation took place online and teachers showed up with interesting camera images. We are working on a sensor that has not only utility but also has a lot of fun. It pushed me to exercise a bit.

First, we examined the character of the stilt.

At approximately what angle of tilt does it transition between HIGH and LOW? What else do you notice about its behavior?

I think at approximately 45 degree, but it depends. As long as the gravity pulls the balanced bead to another side, the tilt transits.

What if you tilt the forearm?

The tilt changes between 1 and 0 and it will be reflected on the computer according to the ups and downs of my arm.

What if you hold the wires several centimeters away and tilt it? What if you shake it?

The tilt will become less accurate and has problems of miscounting.

Here’s the video of the process I went through discovering these points.

Workout 1: Biceps Curls

Here’s the code and I made some changes to the words the serial monitor prints.

const int buttonPin = 6;
int buttonState;             // the current reading from the input pin
int lastButtonState;
int haha=0;
// the previous reading from the input pin

// 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);
 Serial.begin(9600);
 Serial.println("Start your workout");
}

void loop() {

  debounceDelay=millis()-lastDebounceTime;
  
 // 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();
//  }
if (debounceDelay>=20000){
 if ( reading != buttonState) {
   // whatever the reading is at, it's been there for longer than the debounce
   // delay, so take it as the actual current state:
     buttonState = reading;
//      haha++;
    Serial.print("You've down curls for ");
     Serial.print(buttonState);
     Serial.println(" times.");
 }
}
 else {
   lastDebounceTime=millis();
   Serial.println("Stop, your time is up!");
 }
 // 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);
}

 

 

Workout 2: Jumping Jacks

The extended wires made the exercise difficult to do, so I got a small jump. I think the system can correctly count the jumpings most of the time. On rare occasions, I found it count twice when I jump once. That might be the problem with debouncing.

Can you make changes to the speed of your “jumping jack” to detect it? 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 can add more debounce delay time to the loop. I don’t meet many error, so I can’t tell exactly. But it will probably reduce the numbers of mistakes it make. Because this case adds more tolerance to the unstability of the stilt when exercising.

Workout 3: Start and Stop Timing

No recording, but here is the code.

const int buttonPin = 6; 
int buttonState;             // the current reading from the input pin
int lastButtonState;
int haha=0;
// the previous reading from the input pin

// 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);
  Serial.begin(9600);
  Serial.println("Start your workout");
}

void loop() {


   debounceDelay=millis()-lastDebounceTime;
   
  // 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();
//  }
if (debounceDelay>=20000){
  if ( reading != buttonState) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:
      buttonState = reading;
//      haha++;
     Serial.print("You've down curls for ");
      Serial.print(buttonState);
      Serial.println(" times.");
  }
}
  else {
    lastDebounceTime=millis();
    Serial.println("Stop, your time is up!");
  }
  // 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);
}

Reflections:

Before I worked on the midterm code, it never occurred to me that the millis can be used in multiply ways in one time. So I gave up to debounce in the third part’s code to make way for  time counting, which is completely unnecessary. As long as I created enough variables, the debounce and time counting can be done at the same time.

Challenge:

Detect when the user has stopped doing biceps curls (or jumping jacks) before the set is done. Prompt them on the serial monitor.

I’m not sure whether I am coding correclty,since I didn’t test it. I just wanted to practice a little for my midterm project.

const int buttonPin = 6;
int buttonState;             // the current reading from the input pin
int lastButtonState;
int haha=0;
// the previous reading from the input pin

// 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 = 0;    // the debounce time; increase if the output flickers
unsigned long lastStiltTime = 0;  // the last time the output pin was toggled
unsigned long stiltDelay = 0;    // the debounce time; increase if the output flickers

void setup() {
 pinMode(buttonPin, INPUT);
 Serial.begin(9600);
 Serial.println("Start your workout");
}

void loop() {

  debounceDelay=millis()-lastDebounceTime;
  stiltDelay=millis()-lastStiltTime;
  
 // read the state of the switch into a local variable:
 int reading = digitalRead(buttonPin);
 
//  // If the switch changed, due to noise or pressing:

if (debounceDelay<=20000){
 if ( reading != buttonState) {
   // whatever the reading is at, it's been there for longer than the debounce
   // delay, so take it as the actual current state:
     buttonState = reading;
     haha++;
     lastStiltTime = millis();
    Serial.print("You've down curls for ");
     Serial.print(haha);
     Serial.println(" times.");
 }
 else if (reading == lastButtonState&&stiltDelay>1500) {
   // reset the debouncing timer
   Serial.println("You have stopped the exercise.");
 }
}
 else {
   lastDebounceTime=millis();
   Serial.println("Stop, your time is up!");
 }
 // 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);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *