#Blog 9 Recitation 5: Workout

Documentation:

Connect the tilt switch to the Arduino like this:

At first numbers shown in the serial monitor is constantly changing when testing with the code below. I thought it was because my sensor was not connecting well.

 

const int SENSOR_PIN = 6;  
  
int tiltVal;

void setup() {
  pinMode(SENSOR_PIN, INPUT);    // Set sensor pin as an INPUT pin
  Serial.begin(9600);
}

void loop() {
  // read the state of the sensor
  tiltVal = digitalRead(SENSOR_PIN);
  Serial.println(tiltVal);
  delay(10);
}

But Prof. Godoy looked at my circuit and found that I miss connect it. After I changed the circuit under Prof.’s instruction, the numbers shown became more stable. Then, after the adjustment, I tried the other code that only print 1 when the title sensor changes from 0 to 1, or from 1 to 0:

const int SENSOR_PIN = 6;

int tiltVal;
int prevTiltVal;

void setup() {
  pinMode(SENSOR_PIN, INPUT);    // Set sensor pin as an INPUT pin
  Serial.begin(9600);
}

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);
  }
  prevTiltVal = tiltVal;

  // for Serial Plotter use
  //Serial.println(tiltVal);

  delay(10);
}

This is the video where I used the serial plotter after uncommenting the line //Serial.println(tiltVal):

 

Questions:

  1. At approximately what angle of tilt does it transition between HIGH and LOW?

It will transition in a range about 45 degrees evenly split by the horizontal line.

 

  1. What else do you notice about its behavior?

I notice its transition between HIGH and LOW is also affected by how fast I turn it. If I turn it more quickly, the small fraction inside the sensor are more easily thrown to the other part of the sensor tube, thus accelerating the speed of change.

  1. What if you tilt the forearm?

It’s the same as turning in with fingers, only that the move will appear to be more significant.

 

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

I don’t know if I get this question correctly. If I were to answer according to my understanding, I am concerned about the connecting issue between the wire and the breadboard.

  1. What if you shake it?

Shanking it will create a rapid change in 1 and 0 in the monitor:

Debouncing the tilt switch:

I think 50 ms is a reasonable lockout. This is the code for debouncing:

// 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

// 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) {
    // 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;
  }
  
  // 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 1: Biceps Curls

The thing I need to add to the code here is another if logic to let Arduino count and print the number I moved my arm.

 

This is the final code I used:

// 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);
}


Workout 2: Jumping Jacks

The system did correctly count jumping jacks. In the Serial Plotter, it not only record the status of the sensor, it also count of the times I move, so there will be a rising peak each time the status becomes 1.

 

Questions:

  1. Can you make changes to the speed of your “jumping jack” to detect it?

Yes, I can. And even if I move relatively fast, it can still detect the count really accurately.

  1. What happens if you change the debounce lockout time? What do you need to change it to detect one “jumping” exercise?

When I try to change the lockout time to 25, it doesn’t have too much impact on the count since my sensor is stable right now. But if I change it to 70, then it will miss a few counts.

  1. How reliable is the counting? How many extra, or missed, counts happen as a fraction of the number of real exercises done?

If I use the 50 ms debounce lockout time, then it really won’t count it wrong

 

Workout 3: Start and Stop Timing

In this workout, the issue I met with the code is that I don’t know how to make the “Start your Workout” appear only once in the monitor and how I can stop the monitor from printing “Stop, your time is up!”. To solve the problem, I learnt from our amazing LA, Winny. We solve the problem with the following code:

// 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);
}

 

Draw you own illustration sketches that showcase how a person would use this interactive training device.

March 19, 2022, Jinqiao, Younian Liu

1 thought on “#Blog 9 Recitation 5: Workout

Leave a Reply

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