A little Python program to follow the 20-20-20 rule and prevent eye strain | Rainnic in the Clouds

A little Python program to follow the 20-20-20 rule and prevent eye strain

Submitted by Nicola Rainiero on 2018-04-15 (last updated on 2018-07-09)

I have recently started to complain some eye strain and headache, after remaining many hours in front of my laptop screen. Hence I discovered the 20-20-20 rule and wrote a script in Python to automatic show a black fullscreen every 20 minutes for 20 seconds in order to force me to look away at something that is 20 feet (about 6,10 meters) from me. Besides the program makes a sound when the screen turns on again.

When I am working on PC, it’s difficult for me to relax my eye and take a break above all if I have many things to do, so a simple recurrent timer or the sedentary tracker alert of my smart bracelet don’t work at all. I have the necessity to force me to make a break like the 20-20-20 rule says.

The 20-20-20 rule

Here there is a complete article on this matter: How Does the 20-20-20 Rule Prevent Eye Strain?, the rule consists every 20 minutes spent using a screen, you should try to look away at something that is 20 feet away from you for a total of 20 seconds.

A little Python program to follow the 20-20-20 rule and prevent eye strain

I don’t know if the researches reported there are true, but I found some symptoms that happen even to me (highlighted in bold):

  • dry eyes;
  • watery eyes;
  • blurred vision;
  • doubled vision;
  • headaches;
  • soreness in the neck, shoulders, or back;
  • sensitivity to light;
  • trouble concentrating;
  • difficulty keeping eyes open.

And using this method I have almost solved them!

The program

I want a program that every 20 minutes turn off the screen of my computer, then turns on it and makes a sound when 20 seconds have passed, with the ability to deactivate the rule if my audio card is busy (useful if I am watching videos).

First I tried with Bash and cron using:

  • xrand (too parameters to set up, little portability to other computers)
    xrandr --output HDMI2 --off
    sleep 20
    xrandr --output HDMI2 --on
    xrandr --output HDMI2 --mode 1920x1080
    
  • chvt (necessity to change the permission at the binary in order to execute it and little suited)
    sudo chmod u+s /bin/chvt #to allow the user to execute it
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    export DISPLAY=:0.0
    status=`/usr/bin/pacmd list-sink-inputs | grep -c "state: RUNNING"`
    /usr/bin/pacmd list-sink-inputs >> /home/nicola/prova.txt
    echo $status >> /home/nicola/prova.txt
    if [ "`/usr/bin/pacmd list-sink-inputs | grep -c 'state: RUNNING'`" -lt "1" ]; then chvt 3;  sleep 20;  chvt 7; fi
    

Second I tried with Python and I was very satisfied with the solution.

Requirements

I wrote it in Python 3.6 and the following packages1 are required:

  • pygame;
  • sys;
  • subprocess;
  • schedule;
  • time;
  • pacmd (i.e. Pulseaudio2 and strictly connected to the OS in use).

Settings:

At the beginning of the file there are three items to adapt the program at your needs:

# SETTINGS:
size = (1920, 1080) # size of your monitor
check_audio = ‘ON’ # with 'ON' the script doesn't turn off the screen if your audio card is working
duration = 1200 # the time interval in seconds

Download

You can find and download my script on GitHub at the following link:

How it works

Simple execute it from a terminal or create a launcher in this manner:

python eye_strain_reducer.py

Then it prints in the terminal:

  • the time when it starts;
  • the time and the correct action every the duration setted
    • --> OKAY!! (if turns off the screen)
    • --> NO WAY (if doesn't turn off the screen because check_audio option is ON and the number of the sink is greater than or equal to 1).

Here is an example:

$ python3.6 eye_strain_reducer.py
19:35:11–> Starting…

b’0\n’
19:55:11–> OKAY!!

b’1\n’
20:15:34–> NO WAY!!

...
  • 1. To install them I suggest 'pip' because it is very easy to use: "pip install some-package-name"
  • 2. PulseAudio is a network-capable sound server available in several linux distributions.


Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.