Pretend Oxygen Monitor

This article will show you how to make an Android app that roughly simulates the depletion of Oxygen reserves over time, and gives a warning when it’s low.  It’s only pretend, but we will go over some real information about making sure you’re still able to breathe while in Space.

 

How much air is needed?

The first thing that we need is an estimate of how much air people use in a certain amount of time.  Space isn’t the only hostile environment where people need to carry their air with them.  People regularly do it when they go diving.

Underwater diver (nat10100, CC0 Public Domain)

Underwater diver (nat10100, CC0 Public Domain)

When using the traditional SCUBA (Self-Contained Underwater Breathing Apparatus) diving equipment, the diver breathes the air once and then release it (hence the bubbles).

From tank to lungs to bubbles

Simple view of Scuba Diving system (open-loop).

To work out how much ‘air’ they’ll need, divers use a term called Surface Air Consumption (SAC).  This is a figure for how much air they breathe per minute.  It depends on the individual, but as an example if you breath 16 times a minute, and use 1.5 litres of air in each breath, then you’ll need at least (16 x 1.5) 24 litres of air for each minute underwater.  It gets a bit more complicated because as you go deeper you use more air.

This turns out to be a lot of air.  If you wanted to dive for an hour, then you would need a minimum of 1440 litres of air.  However, because the air is compressed a 10 litre tank can contain 3000 litres of breathable air (3000 / 16 = 125 minutes).

The values from the diving community are a good starting point because they come from people engaging in activity (swimming around) while in an environment with some similarities to weightlessness – you’ve probably seen astronauts training for spacewalks underwater.  If we were interested in the air consumed inside a spacecraft (such as the ISS), then we could look at Tidal Volume – which is the amount of air that passes through a person’s lungs when they’re resting.  It’s a figure that’s particularly important when people are on respirators in hospitals – and is estimated at around 8ml per kg of body weight, 12 times a minute.  So, a person weighing 90kg might only need 8.64 litres of air per minute when, for example, asleep.

 

Where are the Astronaut’s/Cosmonaut’s/… Air-Tanks?

Air tanks are very obvious to spot on a SCUBA diver, and they might only be carrying enough for half an hour.  If modern EVAs (Extra Vehicular Activity – where the astronaut goes outside of the spaceship) can regularly last longer than 6 hours, then why aren’t they carrying 3 times the amount of air?

The answer is that they use expensive and complicated equipment to recycle the air and keep breathing it.  Since each breath only takes part of the Oxygen out of the air, it’s a waste to simply expel it.  Instead the air is constantly being passed through a system where CO2 and water vapour are taken out, and then the current level of Oxygen is checked, with extra Oxygen being squirted in to top-up as needed.  This is much more efficient, and means that a much smaller, pure oxygen, tank can be used.

breathing loop with oxygen added as required

Simple view of Astronaut breathing system (closed-loop).

The ‘air’ itself is also very different.  The air that you’re breathing right now is probably only around 20% oxygen.  In the spacesuit, the air is pure oxygen, and only around 1/3rd of normal pressure.  The system is called the PLSS (Primary Life Support System), and you can view a detailed schematic in this PDF.  There are also more advanced versions of SCUBA equipment called Closed-Circuit Rebreathers, which use similar technology.

 

Oxygen Consumption Rate

Actual values for the rate at which oxygen is used up in spacesuits is hard to come by, but an estimate was given in a blog post from British astronaut Tim Peake.

“In the spacesuit we breathe pure oxygen and consume about 50 litres per hour…”

This works out at 0.83 litres per minute, and is obviously much better than the 24 litres per minute needed when breathing Earth air.  When you consider that the air we breathe in is around 21% oxygen, and the air that we breath out is still around 16% oxygen, you can see why recycling would be much more efficient.

In the real-world a pressure sensor attached to the oxygen tank would be the best way of monitoring the amount left.  Since we can’t do that easily, we’ll create an app where you can set a volume in litres (uncompressed), and it will create a timer that counts down.  The 50 litres per hour seems like a rounded figure, but it will do for us.

 

Setting up for Android Development

If you haven’t done so already, then follow the steps in this post to setup Android Studio.  Create a new project, name it OxyGauge, and choose a Blank Activity.  When you see a screen similar to that shown below, then you’re ready to move on.

oxygauge-1

Project setup and ready to go

 

Time Calculation

If it isn’t open already, then switch to the activity_main.xml file – where you can see the preview of the phone.

  • Select the Hello world label and delete it.
  • Add a Number (Decimal) widget as shown below.  If you can’t see Number (Decimal), then Plain Text will do.
Adding text entry field to interface

Adding text entry field to interface

  • Add a button underneath, and set the text property to be something meaningful.
Adding a button

Adding a button

  • Also, take a look at the button’s clickable property and make sure that it is ticked.  Otherwise you won’t be able to click on your button.
  • Now add a Medium Text widget, and set its text property to be 00:00.
Adding the label and setting its default text

Adding the text view and setting its default text

You now have the basic user interface components that you need, so it’s time to add some code.  We want to be able to enter a volume in litres, press the button, and then be told how many minutes that would be.  In the MainActivity.java file, add the code below just above the last closing bracket.

NOTE: Be sure to save the file whenever you switch to another one.

REMEMBER: If something gets highlighted in red, check for typos first, and then try clicking on it and pressing Alt + Enter to automatically fix.

  • Return to the activity_main.xml file, select the button, and set the onClick property to be startUsingOxygen (matching the code that you just entered).
Setting the on click property of the button

Setting the onClick property of the button

Try your app, either on an actual device or through the emulator.  Note at this point you won’t get minutes and seconds, you’ll get minutes and fractions of minutes, but we’ll come back to that.

 

Oxygen Gauge

Now we’re going to add a graphical representation of the oxygen being used up.

  • Add a progressBar (Horizontal) to the interface.  Set its layout:width to fill_parent so that it goes across the full screen.
Adding the progress bar, and setting its width

Adding the progress bar, and setting its width

We need to create a reference for the progress bar at a higher level (I’ll explain why later).  Scroll up to near the top of the MainActivity.java file to the line that starts with public class.  Add the bottom line of code shown below.

Add the code below inside the startUsingOxygen method that you previously added (directly underneath txtTime.setText(String.format(“0.2f”, time)); )

The CountDownTimer is what’s called an inner class.  It’s a chunk of code that sits slightly separately from the code around it – which is why you needed to create the reference for the progress bar at a higher level.

Try your app.  The progress bar should start full (100%), and then reduce over time.

 

Low Oxygen

Running out of oxygen is a situation that everyone wants to avoid.  Let’s add some alerts for low levels – as an example when there is 30 seconds left.

  •  Update the onTick section by adding the following code (taken from StackOverflow):

Try  your app and check that it works (with a low number like 0.5 for the number of litres).

That might not be obvious enough.  Let’s add a warning message as well.

  • Return to the activity_main.xml file, and add a Medium Text underneath the progress bar.  Note that this one will probably be named textView2 by default.
  • Delete the words in the text property – so that initially you won’t be able to see it.
  • Find the textColor property, and click on the button with three dots.
Setting the warning message's textColor

Setting the warning message’s textColor

  • In the window that appears, click on the Color tab, choose a red colour and click OK.
  • Return to the bit of code that you just added, and adjust it to match that shown below:

  •  Also update the onFinish section to notify when all Oxygen is gone:

 

Going Further

That’s the basics of the app completed.  However, there’s lots that could be added to make a better app.  Let’s start by addressing some fairly serious problems.  Firstly, if you just press the button without entering a number, then the app will crash.  A simple solution to handle errors is to set a default value if something unexpected is found.

  • Update the code at the top of the startUsingOxygen method to match that shown below:

The first new line handles nothing being entered, and the second prevents negative numbers.  However, it could still cause the “OXYGEN DEPLETED” message to appear, so add the following code to reset it whenever the button is pressed (it can go anywhere above the CountDownTimer):

 

Now we’ll return to the issue of displaying correctly formatted time in minutes and seconds.  We need to do it a lot, so it makes sense to create a function.

  • Add the code shown below directly above public void startUsingOxygen(View v){

  •  Replace the line

  •  With:

  •  You can also get the time to continuously update by modifiying the onTick section:

  •  However, now the time display will stick on 0:01 at the end, so just like with the progress bar we need to update the onFinish section to tidy things up:

 

Wrapping Up

We could keep adding to the app, but it pretty much does what we wanted it to do.  If you ran into any problems then you can check the full MainActivity.java listing; which also includes some other features.

The end result

The end result

 



Comments are closed.