IxLab Recitation 3: Workout

Short Video

Step 4

This is a bicep curl workout counter. It can count how many times the user curls the bicep.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Step 5

This is a step counter. It will count how many steps the user has ran. When it reach the goal the buzzer will play the song “Never Gonna Give You Up”.

 

 

 

 

 

 

 

 

 

Code

Here’s the code of the step counter.

int SENSOR_PIN = 2;int tiltVal;int prevTiltVal;
 int count = 0;
 int set = 0;
 int buzzerPin = 8;

void setup() {
  pinMode(SENSOR_PIN, INPUT); // Set sensor pin as an INPUT pin
  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);
  Serial.println("Start your workout!")
}

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) {
    if (tiltVal == 0){
      count += 1;
      set += 1;
      Serial.print(count);
      Serial.println(" Steps");
    }
    if (set == 10){
      Serial.println("Yay, you've done one set of steps");
      playMelody();
      set = 0;
    }
    prevTiltVal = tiltVal;
  }
  delay(10);
}

void playFreq(double freqHz, int durationMs) {
  //Calculate the period in microseconds
  int periodMicro = int((1 / freqHz) * 1000000);
  int halfPeriod = periodMicro / 2;

  //store start time
  long startTime = millis();

  //(millis() - startTime) is elapsed play time
  while ((millis() - startTime) < durationMs) {
    digitalWrite(buzzerPin, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(buzzerPin, LOW);
    delayMicroseconds(halfPeriod);
  }
}

void playMelody() {

  playFreq(262, 100);
  playFreq(294, 100);
  playFreq(349, 100);
  playFreq(294, 100);
  playFreq(440, 200);
  delay(100);
  playFreq(440, 400);
  playFreq(392, 400);
  delay(300);
  playFreq(262, 100);
  playFreq(294, 100);
  playFreq(349, 100);
  playFreq(294, 100);
  playFreq(392, 200);
  delay(100);
  playFreq(392, 400);
  playFreq(349, 400);
}

Sketch

The user need to wear the sensor on its hand. The sensor is connected to the arduino board. The arduino board can exchange data with the running machine wirelessly. In this case, the user can see how many steps were ran through the display on the running machine.

Reflection

  • At what angle of tilt does it transition between HIGH and LOW?

When the tilt sensor was parallel to the ground.

  • What else did you notice about its behavior?

When shaking the sensor, I can feel that there’s a movable part in the sensor which can make a sound.

  • What if you rotate the limb that has the sensor attached to it?

The output value won’t change.

  • What if you shake it?

The output value will shift between 1 & 0 rapidly.

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

The output value won’t change.

  • Do you think it can be used by any user?

It can be utilized by the amateur fitness enthusiasts since it can count for them. Also, we can set the goal and visualize the process of workout in order to encourage them to insist.

Leave a Reply

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