Screen Time Monitor

Mobile phones are hard to put down. It’s easy to become distracted when texting with your friends, browsing social media or playing games. You can use your Light Phidget to help keep you on task when you are studying or in class. In this project, you will create a Phone screen time monitor. It will keep track of the time spent with your phone, helping you spend less time on your phone!

Setup

Before getting started, make sure you have the following parts.

Light Phidget

Phidget cable

 

Cellphone

USB cable

Step 1

Attach Light Phidget to the VINT Hub and VINT Hub to your computer.

Step 2

Set your phone screen down on the Phidget.

Write code (Java)

Copy the code below into a new Java project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.

Not your programming language? Set your preferences so we can display relevant code examples

  
//Add Phidgets Library
import com.phidget22.*;

public class ScreenTime {
    public static void main(String[] args) throws Exception{
        
        //Create
        LightSensor lightSensor = new LightSensor();
        
        //Open
        lightSensor.open(1000);
        
        //Use your Phidgets
        while(true){
            System.out.println("Illuminance: " + lightSensor.getIlluminance() + " lx");
            Thread.sleep(150);
        }
    }
}
  
  
package screentime;

//Add Phidgets Library
import com.phidget22.*;

public class ScreenTime {
    public static void main(String[] args) throws Exception{
        
        //Create
        LightSensor lightSensor = new LightSensor();
        
        //Open
        lightSensor.open(1000);
        
        //Use your Phidgets
        while(true){
            System.out.println("Illuminance: " + lightSensor.getIlluminance() + " lx");
            Thread.sleep(150);
        }
    }
}
  

Write code (Python)

Copy the code below into a new Python project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.

Not your programming language? Set your preferences so we can display relevant code examples

  
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.LightSensor import *
#Required for sleep statement
import time

#Create
lightSensor = LightSensor()

#Open
lightSensor.openWaitForAttachment(1000)

#Use your Phidgets
while (True):
    print("Illuminance: " + str(lightSensor.getIlluminance()) + " lx")
    time.sleep(0.15)
  

Write code (C#)

Copy the code below into a new C# project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.

Not your programming language? Set your preferences so we can display relevant code examples

  
//Add Phidgets Library
using Phidget22;

namespace ScreenTime{
    class Program{
        static void Main(string[] args){

            //Create
            LightSensor lightSensor = new LightSensor();

            //Open
            lightSensor.Open(1000);

            //Use your Phidgets
            while (true){
                System.Console.WriteLine("Illuminance: " + lightSensor.Illuminance + " lx");
                System.Threading.Thread.Sleep(150);
            }
        }
    }
}
  

Write code (Swift)

Copy the code below into a new Swift project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.

Not your programming language? Set your preferences so we can display relevant code examples

  
import Cocoa
import Phidget22Swift

class ViewController: NSViewController {

    @IBOutlet weak var lightLabel: NSTextField!
    
    //Create
    let lightSensor = LightSensor()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        do{
            //Subscribe to event
            let _ = lightSensor.illuminanceChange.addHandler(light_change)
            
            //Open
            try lightSensor.open()
            
        } catch {
                 print(error)
        }
    }
    
    func light_change(sender:LightSensor, illuminance: Double) {
      DispatchQueue.main.async {
        // Use information from your Phidget to change label
        self.lightLabel.stringValue = String(illuminance) + " lx"
      }
    }
}
  

Try running your program. Place your phone over the light sensor and note the change in illuminance. You will use this change in the next step.

Track Screen Time

To track your screen time, you will need to introduce a timer into your code. The difference between the time the phone is removed and replaced is how long the phone was active. If you keep a running total of your active minutes, you can keep track of your overall screen time.

Add a timer to your program and alert your user to their total screen time in minutes every time they place their phone on the Light Phidget.

Record time (Python)

time.time() provides the time since your program started in seconds.

Record time (Java)

System.nanoTime() provides the time since your program started in nanoseconds. 1 nanosecond is 10^-9 seconds. To get the time in seconds use: (System.nanoTime * Math.pow(10,-9))

Record time (C#)
  1. add using System.diagnostics; at the top of your program
  2. Create a new stopwatch and start the timer just after your open call before you use your Phidgets:
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();
  3. stopwatch.ElapsedMilliseconds will provide you with the time since you started the timer in milliseconds. To convert to seconds use: (stopwatch.ElapsedMilliseconds/1000)
Record time (Swift)

To record the current time use: let time = DispatchTime.now(). To use or print the time recorded in seconds use: Double(time.uptimeNanoseconds)/1000000000

What are Phidgets?

Phidgets are programmable USB sensors. Simply plug in your sensor, write code in your favorite language and go!

Phidgets have been used by STEM professionals for over 20 years and are now available to students.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done