All posts by edwardm

What day is today? Finding the “Day of the Week” in App Inventor

A reader recently asked how to determine the day of the week – Monday, Tuesday, Wednesday and so on, using MIT App Inventor code. Obtaining the day of the week is easy, although not obvious.

Using TimePicker and DatePicker

I previously posted a tutorial titled “Using TimePicker and DatePicker for entering time and data information“. Refer to that tutorial for basic information on the handling of time and data information in App Inventor.

Converting Time Instants into Different Formats

App Inventor has built-in functions to convert the date into different formats. However, the function to convert a calendar date into the day of the week is not in the DatePicker component, but in the Clock component.

To demonstrate, we use the original app from the TimePicker and DatePicker tutorial (you may download the sample code from the original tutorial page) and then we add two small bits of code to do identify the day of the week.

In the original sample code, the screen’s Initialize event handler displays the current date and time on screen. To that code, we add the block shown within the yellow rectangle, below:

Voila_Capture 2016-08-01_03-02-47_PM

call Clock1.Now” returns a value called a time instant. The time instant contains both date and time in a special format: the format is not important to us as App Inventor provides routines to convert the time instant into common values like year or hours and minutes.

The Clock component has a method “call Clock1.WeekdayName” that extracts the date information and calculates the day of the week, returning a text string with values like “Sunday” or “Monday”.

In the original tutorial, the DatePicker is used to select a new calendar date (different than what ever the current date is). Once the data is selected with that user interface, the event “.AfterDateSet” is thrown. Within the AfterDateSet event handler, we extract the Year, Month, Day and also the “WeekdayName” name. To convert the date instant to a day of the week format, we use the WeekdayName method of the Clock component – easy but not obvious!

Voila_Capture 2016-08-01_03-03-18_PM

Summary

Converting a date or time instance into normal formats like Year, Month, Day or day of the week is easy when you use the appropriate conversion functions built in to the App Inventor components.

The weekday value conversion is a little odd in that its hidden in the Clock component. But once you find it, obtaining the day of the week is easy!

Introducing my “Internet of Things” Blog

The “Internet of Things” refers to connecting all types of products and sensors to the Internet.

Sensors, microcontrollers and communications are so inexpensive they can be built in to many objects and devices.

For example, a light bulb (or light switch) can detect the presence or absence of people in a room – and automatically turn a light on or off. A light bulb that can detect people in a room could also become part of a security system by transmitting the room status to another device (security alarm) or to a smart phone app or to an Internet application running in “the cloud”.

In this way, a “dumb” device (light bulb) becomes enhanced to perform new functions and become more valuable than “just a light”.

The Internet of Things (or IoT) refers to adding sensors, controllers and communications to all sorts of “things”. Devices become connected to the Internet – and even our lightbulbs might end up with IP addresses!

I just launched a new web site – “Coldstreams Internet of Things” – that will focus on the Internet of Things, and the related subjects of robotics and automation.

Regarding robotics and automation, the same inexpensive technology that makes IoT possible is creating new opportunities for robotics and automation. We will see the development and adoption of new kinds of automation, far sooner than most people realize. These technologies are not in the distant future but are available now, particularly in business and industry.

To visit the web site, go here: Coldstreams Internet of Things

Follow on Facebook here: https://www.facebook.com/coldstreams

While IoT is separate from App Inventor, there will be overlap. I am sure many of us are dreaming of MIT App Inventor apps that interface with external devices! I know I am!

Why the name Coldstreams? Long story but I’ve had that name and URL for a very long time. It was used in the past for a small business and then for a blog on technology and business. I have relaunched the web site to focus on Internet of Things. That said, about 2,000 original and now dated blog posts are still there – and on the Facebook page!

Sorry for the delays in getting more tutorials up!

I have been busy with other projects.

But I have a big list of ideas for more tutorials and App Inventor projects! As time becomes available (which should be soon!) I will be posting more items here.

I am also beginning to study the Internet of Things market and technologies. App Inventor may play a role in this exciting new technology where everything is potentially able to communicate with other objects and the Internet.

Ed

Rounding a number to a specific number of decimal places

I was working on a small app that needed to round some numeric values. For example, given a number such as 123.456789, I wanted to round this off to two decimal places such as 123.46. Why .46 instead of .45? Because the value is rounded at the n-th decimal place (.456 rounds up to .46).

MIT App Inventor does not have a function to do this sort of round off – well, not exactly. Actually, it has a formatting function that can accomplish the same thing but it is intended for converting numeric values to text strings.

I created a simple little program to demonstrate how this works, plus a couple of other methods to implement this feature.

Update: This post has been updated with thanks to Taifun for noting that App Inventor does have a “raise to a power function”. See below for more details and a link to his great web site!

User Interface View

The user interface prompts for a number, and the number of decimal places to which it should be rounded. Press the Round off number button to see the result.

RoundOffUIHere you can see that 123.4567 has been rounded to 123.457.

Continue reading Rounding a number to a specific number of decimal places

Appinventor.pevest.com usage

This web site is now receiving 25,000 unique visitors each month! I am very pleased that these tutorials are helpful to so many people all over the world!

Web site access is nearly the same every hour around the clock, implying a global readership (update: 145 countries!)

Operating System Used

  • 82% of the visitors use Windows
  • 7% use Linux
  • 4.4% use Mac OS X
  • 1% use iOS (iPhone/iPad)
  • A very tiny number have accessed the web site via Java Mobile, Blackberry and … the Nintendo Wii plus a few miscellaneous devices.

Bandwidth

The web server output an average of about 800 kbytes per minute delivered to those viewing the web site. That is nearly 1 megabyte per minute.

Browser Used

  • Internet Explorer 42%
  • Chrome 32%
  • Firefox 12%
  • Opera 5%
  • Safari 2%
  • Android browser on smart phone 1/2%

Countries

I do not yet have a count of countries. I am working on that.

UPDATE: 145 countries!

How Do People Find appinventor.pevest.com?

  • 67% come directly to the web site by typing the address or using a bookmark
  • About 1% come from a link on another web site
  • About 32% come from an online search, and almost all of those come from Google searches.

The most common search is for information about App Inventor (duh!), followed by information about Bluetooth! High in the search list are also searches for information on Android or App Inventor and Arduino and searches related to App Inventor sensors (such as orientation sensor) and TinyDB.

How Do We Know This?

When you visit any web site, your browser sends information about the operating system used, screen size, browser used, and so on, to the web site so the server can optionally deliver a web page customized to your configuration. When you search for something online, the search service (such as Google) passes the search text to the web server so web developers can optimize their content or learn what content is most desired by users.

The web server does not know who you are.

Using Location Information and GPS for finding your position

This tutorial introduces location services features available in App Inventor.

A future tutorial will demonstrate these features applied to specific applications.

About Android Device Location and GPS Features

Your Android device probably has built-in features to identify the device’s location. These features may include:

  • Global Positioning Satellite receiver (GPS)
  • Cellular network location information
  • Wi-Fi network location information

The GPS receiver interprets special (and very weak!) timing signals from satellites and uses those signals to compute latitude, longitude and altitude of your device. GPS has fairly good accuracy – correctly identifying the device’s position within as little as 5 meters (but which may at times be much wider such as within a 30 meter diameter circle accuracy). GPS accuracy is affected by the device’s ability to see the sky and may be blocked by buildings, trees and weather, and signals may suffer from ionospheric effects.

Cell phones can obtain information about the cellular network tower to which they are connected, including location information from the cellular tower.

Wi-Fi networks have a unique identifier associated with them (not the SSID public address). Google has built a map of wi-fi access point network locations using a combination of their Google mapping cars as well as everyone’s Android-powered cellular phone to build a huge database of wi-fi access points and their location. Your Android device can use a combination of location data and fetch a postal mailing address from Google for a given location.

Android phones have options as to which type of location information to use.

  • Cellular network information may be the least accurate, but it also uses the least amount of battery power.
  • Wi-fi network position information also uses little battery power, and can provide reasonably accurate location information when your phone is indoors and unable to hear signals from GPS satellites.
  • GPS provides the best accuracy but receiving and processing the signals uses the most battery power.

Your Android phone has options to select High accuracy, Battery saving and Device only.

  • High accuracy – Android will automatically select GPS, cellular or wi-fi to obtain a high accuracy position reading. This is likely the default setting on your phone.
  • Battery saving – Android will use cellular or Wi-fi networks to obtain position information.
  • Device only – Android will use the GPS receiver (greatest battery drain)

To select Location services, go to Settings | Location and set this feature to “On”.

Touch the item labeled “Mode” at the top of the screen to select “Location mode” options. This is where you may choose High accuracy, Battery saving or Device only.

Screenshot_20160516-175035To use any location features, you must turn Location to “On” in Android Settings!

MIT App Inventor has a component named LocationSensor providing access to latitude, longitude, altitude and even the postal mailing address associated with the location.

This tutorial introduces the LocationSensor and the basic properties and features. A future tutorial will go in to more depth.

Continue reading Using Location Information and GPS for finding your position