How long do you use your computer a day? Device measuring the time in front of a computer

It is known that sitting long is a risk factor for short life expectancy.

So, I tried to make a device measuring the time when I sit in front of my computer.

For this project I decided to use ARDUINO UNO board. This is a quick prototyping microcontroller board.

<Figure 1. ARDUINO UNO>

To measure the distance, there are many kinds of sensors. I tried to find some cheap and easy to use ones with Arduino board in the Ebay.

<Figure 2. HC-SR04 Distance Sensor>  <Figure 3. Sharp GP2Y0A02YK0F Infrared Sensor>

Image

Image

The ultrasonic sensor has detection range of 2cm-450cm. The infrared sensor has detection range of 20-150cm. I purchased all of them. Each has their pros and cons. The ultrasonic sensor is cheap (< 3$) and accurate in measuring an object in proximity (even less than 10 cm). However, the sampling rate seems low. It’s natural because it uses ultrasound! It emits a sound, and measures the time until the reflected sound comes back. The infrared (IR) sensor is expensive (~$20), and cannot measure an object in proximity. However, it has high sampling rate.  After experiment, I decided to use the IR sensor.

<Figure 4, 5. The assembled views>

Image

Image

The assembly is very easy. Just connect the +/- electrodes and a signal node. The signal node was placed to Analog A0 pin of the Arduino board.

To run the Arduino board, we need a progrmming. For arduino, they call it “Sketch”.

<Figure 6. Programming tool for Arduino>

<The source Sketch>

// I/O Pin declarations
const int analogInPin = 0; // Analog input pin that the sharp distance sensor is attached to

// distance formula values
const int mprime = 16667;
const int bprime = 15;
const int k = 10;
const int maxDistance = 200; // maximum distance of the sensor (in cm)

// Alert constants
const int alertDistance = 75; // Distance to detect a proximity state in cm

// variables
int sensorValue = 0; // value read from the sensor
int distanceIR = 0; // calculated distance (in cm)

void setup(){
//Serial Setup
Serial.begin(9600); // init serial 9600
}

void loop(){
// Read the SHARP distance sensor
sensorValue = readSensor();

// Calculate distance based on formula
distanceIR = (mprime / (sensorValue + bprime)) – k;

if(distanceIR < alertDistance) {
Serial.println(distanceIR);
}
delay(100);

}

// Read Sensor Value ————————————-
int readSensor() {
int i;
int sensorValue = 0;
for(i = 0; i < 5; i ++) {
sensorValue += analogRead(analogInPin);
delay(10);
}
sensorValue = sensorValue/5;
return sensorValue;
}

The distance measuring algorithm comes from:
http://www.semi-blog.com/2010/03/parking-distance-sensor-alarm/ (This link seems broken. Thus I include the source code here: SHARP_DistanceSensor). The original source code contains the following headng:
@title: Distance Measurement and Alarm system
@author: Sebastien Eckersley-Maslin
@company: Blue Chilli Technology
@date: 2 March 2010

I set the Presence state as an object within 90 cm. You can adjust it.
>> const int alertDistance = 75; // Distance to detect a proximity state in cm

From the original source, I removed all most all other redundant codes, not necessary to this project. And I added a code for serial communication.
>> Serial.println(distanceIR);

<Figure 7. Serial communicaiton and the received data>

After compiling and uploading the code, the Arduino board sends  signals to computer through serial port.

Now, we need a PC-side application which calculates and accumulates the proximity time. I decided to use Processing,  a well-know platform for graphics.

<Firuge 8. The Processing>
To measure the time, we need to know the exact time and date. However, the Processing does not provide appropriate datetime functions. So we need some sophisticated programming. However, as I am not good at Processing (neither others :D), I also searched the web for help and found the source code for timer written in Processing.
http://www.openprocessing.org/sketch/60150

I modified the “timerExample.pde” as follows. For the program, “timer.pde” should be placed in the same folder. (You can find the original source codes from the above link.)

TimewithComputer.pde

/*—————————————
Filename: TimewithComputer.pde
title: TimewithComputer
author: Rae Woong Park
email: veritas@ajou.ac.kr
date: 16 August 16, 2012
—————————————–*/

import processing.serial.*;
import java.text.SimpleDateFormat;

timer timer1;
int LineLength, TodayValue, j1, k1=0, ms1=1000, pMs1=100, prevDuration=0;

Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
PrintWriter output;

Date d = new Date();
long timestampToday = d.getTime();// + (86400000 * offset);
long timestampYesterday = d.getTime() + (86400000 * -1);
String Today = new java.text.SimpleDateFormat(“yyyyMMdd”).format(timestampToday);
String Yesterday = new java.text.SimpleDateFormat(“yyyyMMdd”).format(timestampYesterday);
// int myArray[][] = new int[lines.length][2];

String myArray[][] = new String[10000][2];

void setup()
{
File f = new File(dataPath(“Data.txt”));

if (!f.exists()) {
output = createWriter(dataPath(“Data.txt”));
output.close(); // Finishes the file
}

String lines[] = loadStrings(dataPath(“Data.txt”));
LineLength = lines.length;

// println(LineLength);
size(300,160);
smooth();
timer1 = new timer(100);
String portName = Serial.list()[0];

//println(Serial.list());
myPort = new Serial(this, portName, 9600);

for(int i = 0; i < lines.length; i++){
String values[] = trim(lines[i].split(“,”));
myArray[i][0] = values[0];
myArray[i][1] = values[1];

if (int(Today) == int(values[0])) {
j1 = i;
k1 = 1;
TodayValue=int(values[1]);
}

if (int(values[0]) == int(Yesterday)) {
prevDuration=int(values[1]);
}
}
}

void draw()
{
background(255);

if ( myPort.available() > 0 && myPort.read() > 0) { // If data is available,
if (timer1.over()) {
TodayValue++;
timer1.reset();

//Update the value in array for saving.
if (k1 ==0) {
println(LineLength);
myArray[int(LineLength)][0] = Today;
myArray[int(LineLength)][1] = “”+TodayValue;
k1=1;
j1=LineLength;
LineLength++;
}
if (k1 ==1) {myArray[j1][1] = “”+TodayValue+””; }

// save data to file per 60 sec
if (TodayValue%10 == 1) {
output = createWriter(dataPath(“Data.txt”));
for(int p = 0; p < LineLength; p++){
output.println(myArray[p][0]+”,”+myArray[p][1]);
}
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
}
}
}

if (pMs1!=ms1)
{
timer1.setOver(ms1);
pMs1=ms1;
}

textAlign(CENTER);
fill(0);
textSize(40);
text(str(TodayValue/3600)+”h:”+ str((TodayValue%3600)/60)+”m:” + str(TodayValue%60)+”s”,150,80);

textSize(12);
text(“Time in front of computer”,150,20);
textSize(12);
text(“Yesterday ‘s total duration: “+ str(prevDuration/3600)+” h: “+ str((prevDuration%3600)/60)+” m: ” + str(prevDuration%60)+”s” ,150,120);
}

After compiling, the executable file will works as follows:

<Figure 9. The PC-side application>

<Figure 10, 11. The IR sensor attached to the monitor>

At this stage, the program displays today’s and yesterday’s time. The recorded data is saved into Data.txt file. Much things remained to be improved. Statistics  for daily use must be implemented.

Actually this device does not intended to measures the computer use time. But it rather measures sitting time in the chair. I prepared another mirrored computer station which should be used in standing like followings:

<Figure 12, 13. Standing computer station, mirrored to the PC in the desk>

The monitor is mirrored to my PC in desk. The wireless keyborad and mouse are also connected to the PC in desk. Thus, a PC is completely shared by two positions: in the desk and in the standing station. I will attache the distance monitoring device in the standing station, and will measure the time when I was in the standing position. I try to stand as long as possible.

I hope you enjoyed this post!

This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to How long do you use your computer a day? Device measuring the time in front of a computer

  1. Pingback: 페이스북에 글 올리는 JAVA프로그램, JAVA application posting to Facebook | freefromconfounder

  2. Pingback: 페이스북에 글 올리는 JAVA프로그램, JAVA application posting to Facebook | freefromconfounder

  3. I do not even know how I ended up here, but I thought this post
    was great. I do not know who you are but definitely you’re going to a famous
    blogger if you are not already 😉 Cheers!

    • rwpark9 says:

      Thank you very much for your encouragement! Hope you too have good luck! Actually I abandoned this site for the last 2 years while during my stay in USA. I came back to my homeland, I would like manage this site again.

Leave a comment