Tiny Basic Programming Lesson: talking to the real world

12419231_974683622601393_320731550894605287_oUsing HalfByte Tiny Basic to interface with the external world is pretty easy to do. There are already built in interfaces for temperature and humidity, but, using AREAD, AWRITE, DREAD and DWRITE, we can access a variety of sensors and modules that do not need specialized libraries in order to talk to them.  Sensors such as the touch sensor, IR reader and the light sensor I talked about in previous posts can easily be used by HB Tiny Basic.  The key to using them is to know the range of values they return.  Since documentation for them can be difficult to obtain, the best way to figure it out is to connect them and experiment.  Once you get a feel for the range of values and what they mean, you can easily incorporate them into a project.

Lets consider the following code:

100 CLS
110 A=AREAD(0)
120 CURSOR 0,2
130 PRINT "Light level: ";
140 IF A=0 PRINT"Pitch Dark":GOTO 500
150 IF A>0 IF A<100 PRINT "Dark    ":GOTO 500
160 IF A>100 IF A<300 PRINT "Low     ":GOTO 500
170 IF A>300 IF A<600 PRINT "Moderate":GOTO 500
180 IF A>500 PRINT "Bright":GOTO 500
500 GOTO 110

All this code does is read the sensor, determine a range for light level and output the results.  300 and above is about what you have in a living room in the even with one or two lamps turned on.  Over 500 and you are talking some bright light.  The higher the value, the brighter the light.  Zero, of course, is a total lack of light.

Looking at the code, you will notice that we are reading the A0 analog pin.  That’s the pin the light sensor is connected to. In HB Tiny Basic, A0 to A7 is represented by the integer portion-0,1,2 and so on.  To designate that you want to use the Analog pin, use AREAD.  The same rules apply for the Digital pins…use DREAD and 0 to 13 for the pins.  Line 110 above, A=AREAD( 0 ) to read A0 into the variable ‘A’.  That value, in variable A, becomes static until the pin is read again. This allows us to use the value, as captured at that moment, to do what ever we want.  In this case, we use it to determine if the light level is pitch dark to bright. 

Because HB Tiny Basic lacks such operators as ‘and’ and ‘or’, we have to use multiple IF statements. So, for line 150, instead of something like:

150 IF A>0 AND A<100 PRINT "Dark    ":GOTO 500

we have to use:

150 IF A>0 IF A<100 PRINT "Dark    ":GOTO 500

All it is saying is ‘if the value in A is greater than zero AND less than 100, then print the Dark to the output device and skip to line 500’.

Once we are done  evaluating the light level, we just go back and do it again.  A real world use of this could be like ‘if the light level goes below 300, set pin 10 to high so it turns on a light; if the level goes above 600, set the pin to low to turn off the light’.

There are maximum and minimum values and they are determined by both Tiny Basic, which allows for –32767 to 32767, and the sensor itself.  Some will return a non zero value to indicate an ‘on’ value and a zero for ‘off’.  Or, as in the case of the touch sensor, the values switch between 22 and 1023.  Your mileage may vary.

I wrote a short little game, loosely based on Flappy Bird, that uses the light sensor as the controller. The code is posted here and shows a real world use for using AREAD to retrieve data from an external source.  You could take the code and fancy and add code to trigger a buzzer when the bit hits a pipe (using either AWRITE or DWRITE.) 

There all kinds of things you can do with these four simple functions and statements.  A future modification to the language might be the ability to auto run code from eeprom, which would allow HB Tiny Basic to then be used as a control language.  You can do a lot with few resources.


Flappy Bit: writing a Tiny Basic game that uses a light sensor and you as the game controller

FlappyBitPhotoHere's a very rough, very crude game, in HalfByte Tiny Basic, of Flappy Bird. I call it Flappy Bit. You use the Light Sensor to control your bit. Cover the sensor and your bit moves up, shine light on it and you go down. As I have not built much logic into it, weird things happen, like sometimes the pipes blend together and you will hit it, sometimes, you go through the pipe, etc. It only uses about 400 or bytes, so there is plenty of room to play with.

Game Features:

  • Use your hand as the controller!
  • Ultra realistic blocky graphics!
  • Stunning sound!
  • Full Color Black and White!
  • True to physics!

100 CLS
110 X=1:Y=20
120 P=50:Q=0
130 L=RND(20):M=RND(20)
135 W=-1
140 A=0:Z=0
150 S=0
160 D=1
170 C=0
200 LINE P,0,P,L,1
210 LINE P,44,P,44-M,1
220 SET X,Y
225 DELAY 250
230 Z=AREAD(A)
240 IF Z<300 K=-1
250 IF Z>299 K=1
260 IF GET(X,Y+K)=1 GOTO 500
280 X=X+D
290 IF X>75 X=1:D=1
300 LINE P,44,P,44-M,0
310 LINE P,0,P,L,0
320 P=P+W
330 IF P<1 P=RND(75):M=RND(20):L=RND(20)
340 IF M<15 M=M+10
345 IF L<15 L=L+10
350 Y=Y+K
360 IF Y>40 Y=40
370 IF Y<1 Y=1
380 DELAY 100
385 IF X=P+1 C=C+1:TONE 2000,200
386 CURSOR 0,6:?C;
390 GOTO 200
500 CLS
510 ?"You hit the pipe!"
520 DELAY 5000
590 GOTO 100


Giving your Arduino projects 'sight' and 'touch': using IR, Photocells and Touch Sensors to your project

12419231_974683622601393_320731550894605287_oOK, so I am taking some liberty here with the terms sight and touch, but it got your attention, yes?

In this post, I am going to briefly share how to use three sensors: a 'light detector' or photocell, a touch sensitive 'button' and an IR receiver.

These three sensors all came from a company called 'OSEPP', but similar sensors can be had from other vendors as well.  I am going to write about these three specific sensors, but you should be able to adapt the information to what ever you have.  I will present code in both Tiny Basic and Arduino.


• 3 pin outs: GRD (-) VCC (+) S ( Signal)

• Suitable supply voltage: +3 to 5Vdc
• Analog voltage output: 0 to 5 Vdc
• Detects ambient light density
• Works with CdsPhotoresistor
• Interface with microcontrollers and logic circuits • Analog sensors
• Uses PH 2.0 socket
• Special sensor with Arduino expansion boards

The light sensor is a cool photocell that is mounted on little breakout board, making it easier to use in a project. There are three pins: VCC, GND and Signal. Signal would connect to any of the Analog pins. When in use, it not only will let you know if it detects light, but also returns the intensity. The higher the value, the more light it detects. Reading it easy and you do not need any libraries. See the HalfByte Tiny Basic example below.

100 CLS
110 L=0: # PIN A0
120 P=13:# PIN 13 LED
130 A=AREAD(L)
140 IF A>299 DWRITE P, 0
150 IF A<300 DWRITE P, 1
160 CURSOR 0,0
170 ?"Intensity: ", A," "
180 DELAY 250
190 GOTO 130

What this little piece of code will do is turn on the LED if the light level drops below 300 and turns it off if it goes above 299. It also writes the level to the screen.

if you add a line, say 155, you can test for no light:

Or, you can test for too much light:

There are many things you can do, for example, control a servo that turns an armature to open a food door to distribute dog food to a dish when the sun comes up.

You can use it to log when the sun comes, goes down. Use it control lighting, etc.

Here's the Arduino sample:

/* OSEPP example of measured ambient light intensity from photocell .
depending on brightness, an LED brightness is changed.
more light = brighter LED. */

int photocellPin = A0;    // photocell sensor input
int ledPin = 11;      // select the pin for the LED
int photocellValue = 0;  // variable to store the value coming from the photocell val

void setup() {


void loop() { 
// read the value from the sensor: 
photocellValue = analogRead(photocellPin); 
photocellValue = constrain(photocellValue, 200, 800); //adjust depending on environment.   
  // change brightness of LED depending on light intensity of photocell
  int ledbrightness = map(photocellValue, 200, 800, 0, 255);
  Serial.print("incoming value from photocell sensor =");
  Serial.println( photocellValue);
  analogWrite(ledPin, ledbrightness); 


• 3 pin outs: G (GRD) V (VCC) S ( Signal)Basic wiring scheme for all three sensors.
• 3-5 V operating range
• 5 mA minimum current requirement.
• Capacitive touch detection

The touch sensor detects when you are touching the plate on the breakout board.  As long as you are touching the sensor, it returns a value.  You can test the value and determine if there is someone touching the sensor.

Arduino Code:


OSEPP Touch Sensor Module Example

Serial monitor prints values output from touch sensor
when body conductivity applied. 

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  Serial.println("Touch Sensor Value = " + sensorValue);


Tiny Basic Code:

100 CLS
110 L=0: # PIN A0
120 P=13:# PIN 13 LED
130 A=AREAD(L)
140 IF A>100 DWRITE P, 0
150 IF A<100 DWRITE P, 1
160 CURSOR 0,0
170 ?"Value: ", A," "
180 DELAY 250
190 GOTO 130

The Tiny Basic, very similar to the light sensor code, will turn the LED on and off each time you press the sensor.  On my setup, the values switch between 22 and 1023.  Your mileage may vary.

IR Sensor12792206_974716205931468_7006422127672690003_o

• 100% Arduino Compatible
• 3 pin outs: G –Ground V – 5V S – Signal
• Operates at a frequency of 38khz

The IR Sensor allows control of a circuit via an Infra Red remote or other IR source.  It works very much like the other two sensors here: has a ground, voltage and signal pins.  It returns values based on the IR signal received.  Admittedly, I have not yet done much with the sensor, so I have limited experience with it.  You can use the same Tiny Basic example from the Touch Sensor above.

Arduino Code:

infrared sensor reciver. connect signal of infrared to analog pin 0. as the distance
from an object to sensor increases/decreases, you will increase/decrease
speed of led blinks from HIGH to LOW


int IR_Pin = A0;    // select the input pin for the potentiometer
int IR_Value = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:

void loop() {
  // read the value from the sensor:
  IR_Value = analogRead(IR_Pin);   
Serial.println("delay value for LED = "+ IR_Value);  //what value are we reading once an IR led is detected?

// IR_VALUE  = constrain(IR_VALUE, 0, 100); // optional to add a strict range

In each of the examples above, the sensors are connected to the HalfByte Console (or your Arduino compatible) via pin A0.  You can use what ever analog pins you want, just change the reference in the code.  You can use them together as well.



iPad Mini has broken glass? No problem...

10633983_968983429838079_2216291504368981093_oA few months back, my lovely wife's iPad Mini was cracked, right around the button, by my then two year old son.  Now, these things are tough and Xander had thrown it a few times prior to that fateful day.  It hit the floor and then the corner of a cabinet and cracked the glass right by the button.  Over the next month or so, we continued to use the device was dropped once more and, this time, more damage ocurred.

Even in a protective case, the damage got worse and glass shards started coming off, so we shelved it.  Being cheap, we did not want to spend the hundred or so dollars to have it repaired and, on 'Black Friday', we took advantage of the $35 (US) Kindle. The iPad Mini just sat, battery drained, on a box under my desk.

Until a few days ago (Feb 13, 2016) when I found a cheap replacement kit for the glass.  This kit came with the glass, home button and the digitizer plus tools to remove the broken glass and install the new glass.  The best part? It was UNDER twenty five US dollars.

I ordered it and it arrived a couple of days later, thanks to Amazon Prime.10357769_968983373171418_8161123942176373028_o

I set about to remove the old glass, which had the white bezel, and install the new glass with the black bezel. I must say, I like the black bezel more.

The process took about three hours as the glass was tough to remove. In hindsight, I did it wrong.

All of the processes I read about said to use a heat gun to soften the adhesive and make it easier to remove. I did NOT do that.  I wish I had.  I ended up destroying the old glass and, in the process, getting little shards all over my hands.

The process is not difficult, but it is a pain in the ass and very tedious.  The sides came up easy, it was the bloody  corners that caused the problem. There's not much to this, but you need to know a few things...

So, here's a short, step by step list of things I did (except #1):

  1. Use a heat gun or hair dryer on high to soften the adhesive. Be careful not to overheat the device.
    1. If you do not, you must be very careful with prying up the old glass, it is very brittlle and will break. Easily. Very Easily.
  2. Using a small screwdriver or similar device, gently pry the glass up. Pick a corner and slowly work around the perimeter of the device. 
  3. Once the glass is off, lay it over - if possible - so you can rest the LCD on top. If you cannot, find a soft cloth to lay the LCD on when ready.
  4. There are four screws in each corner of the LCD. Unscrew them and set them aside.  The two small screws by the home button you can ignore.
  5. Be careful, the WiFi antennae are right at the edge of the metal casing, I darn near destroyed them.  It is also easy to unplug them. If you do, no problem, you can plug them back in. At this point, you should not be able to unplug them as there is a cover over them.
  6. Once you have unscrewed the LCD, you will need to remove the black tape holding in place, be careful not to cut or destroy the tape, you will need it later.
  7. Gently lay the LCD over the area you set aside earlier.
  8. Next, there is a metal cover under the LCD that is screwed down by 16 very, very tiny screws.
  9. Remove the screws and gently remove the panel.
  10. Next, on lower right, where the cables are, there a small metal cover with four or so screws. Remove it.
  11. Be careful now, this is where those Wifi antennae live.
  12. The two larger flat cables need to be unplugged. 
  13. Unplug the LCD cable and then the digitizer/home button cable.
  14. Remove any excess adhesive from around the edge of the case. It needs to be clean.
  15. Gently plug in the new home button/digitizer cable
  16. Gently plug in the LCD Panel. It takes very little force and you can only plug them in one way.
  17. Replace the small metal cover and then the larger one.
  18. Replace the LCD panel.
  19. Remove the sticky back tape and any other protective plastic from the underside of the new glass.
  20. Carefully place it on the device and firmly press down around the sides and the top and bottom.
  21. Turn on the device, if charged, make sure it works.
  22. If all went well, congratulations, you save yourself a lot of money.

Those screws are very, very small.  There is no real reason why there are sixteen on the big metal plate.  I think they could have gotten by with half that number or even a quarter of them. At any rate, someone felt they were necessary. 

So, the take away here is that if you are going to do this, get a kit that includes the home button and the digitizer. Some only have one or none of them.

12698567_968983143171441_5796826882905185056_oAmazon is a great source for things like this, but so is eBay.  If you take your time and are patient, you can do this with ease.  Patience is the big thing here.  I nearly lost mine with the small screws. Did I mention that they are small? Oh, yeah, sorry. But, they are DAMNED SMALL!

Good luck if you undertake this endevor. 


Windows Mobile 10: It's tough being a fan

About seven years ago, I got genuinely excited about a new consumer product.  That product had WP_20141110_009all kinds of promise: price, performance, looks, ease of use and the ability to be a small computer I could carry around with me to surf the web, read my mail, do some short writing stints and more. Oh, I could make and receive phone calls.  That device was the Palm Pre smartphone.

I laughed at people who waited in line, before a store even opened, for Apple products.  I thought it was absurd. Yet, there I was, with about 15 other good people, waiting in front of a Sprint store to get my Palm Pre.  I don't recall how much I paid, but, at that point, I didn't much care as it was not the seven hundred or so that the iPhone was then.

Oh boy, I got my phone and a haircut and rushed home to play with my new toy.  This thing was just spectacular.  Touch screen AND a keyboard (it was a slider phone that looked like big pebble) and was super easy to use.  And, its browser really more quasi browser like my Windows Mobile 5 powered Motorola Q.  The Pre was just great.  For about a year. That's when the cheapness of the poorly designed plastic body came into play.  See, the hardware, while attractive, was a disaster.  The body had lots strain from the sliding and would eventually crack.  The battery did last long either. It would die and not even be turned on.  And so ended my love for the Pre, but not WebOS.  There has not been anything close until last year.CIMG0118

Lets roll back time a bit.  The aforementioned Moto Q ran Windows Mobile 5, a cramped and poor copy of desktop Windows. Frankly, it was an abomination.  The interface was pretty bad.  The included applications were awful and the support from third parties was not great. In short, Windows Mobile 5, and all versions before it, was just awful.  It's only plus was the Windows Live application that, in many aspects, was similar to Google Now or Cortana. You could talk to it and it would, verbally as well as visually, answer you. I used it a lot in the last few months I used the Q.

myWin10PhoneFast forward a few years and Micorosoft is, once again, hawking a product called Windows Mobile for Phones.  This time, the operating system is just gorgeous. And functional.  It's everything that a mobile operating system should be and more.  It is as good today as webOS was then.  And, it can be used as a desktop operating system as well, via a feature called Continuum. Indeed, on its own, installed in desktop hardware, it could work wonderfully with a keyboard and a mouse. 

Yet, it gets little love and Microsoft is to blame, mostly,  They do not promote it or its capabilities. They port its great features to iOS and Android and even save it for last when developing apps for mobile devices.  It is treated like a distant cousin.  But, that doesn't mean it does not have fans or support. True, it has few of each, but they are there.  And, now, there are more and more hardware manufacturers jumping on board.  In addition to Microsoft, there are companies like Acer, HP, Asus, the former Sony brand turmyWin10Phone3ned company, VAIO, and a host of others.  The phones range from just OK to stellar.  Most are able to support Continuum (it requires a certain class of processor and 2gb of ram, minimum) and all run Windows 10. 

As a user and a fan, I often find it difficult to stay both when I see stories about its demise, when I see Microsoft putting it on the back burner for some lame iOS project or hear the CEO say in one sentence just how important it is while the next sentence he would, essentially, say it was not that important after all.  Indeed, it is difficult being a fan and staying enthusiastic about the platform. 

Windows Mobile 10, Windows 10 Mobile, or what ever they call it, has so much potential--more than its desktop companion, I think.  There's no reason why Microsoft could not port Visual Studion to WM10.  With a mouse and keyboard attached (in Continuum) a developer could very easily develop on the phone.  This powerful OS has the potential to, provided the hardware supports it, completely replace your desktop.  Hell, the iPhone could if Apple pulled its head out of its ass.

WP_20150107_003So, yeah, I went from one doomed smartphone to another seemingly doomed platform.  Oh, in between I had an Android and an iPhone 4. Didn't like them, hated the Android and, early on, liked the iPhone until i 'upgraded' to iOS 7...ugh, what a piece of crap that was.  But, I digress.

Even though I hated my Android phone, if I do get away from Windows Mobile, I think I would have to get a Samsung Galaxy whatever. They are beautiful phones and Android is, finally, getting useable. Some of the siliness is gone, but the fracturing is still there.  So, why not iPhone? Well, it's Apple and that means there are lots of costs.  The hardware is very nice and iOS 9 looks great. My son has a 6 and loves it.  I've played with it and it is far and away better than the version I had.  But, it is still Apple and that means you are married to them and that is one marriage I do not care to join.WP_20141110_018

So, how long do I have with my favorite mobile operating system? Well, until Microsoft says they won't support it or they dump the business they paid billions for, a few short years ago.  I am pining for the Lumia 950, but that Acer Jade Primo looks awfully nice too.  The VAIO looks good as well.  So, there are a few non-Microsoft phones that I would buy, but...I'd rather get a 950. Unfortunately, I have, yet again, picked the wrong carrier (Verizon, which hates Microsoft) and still have about a year left on my bloody contract.  I'm hoping there is a more drool worthy phone out by then. Until then, I'll continue along with my beat up ICON. It is still a great phone and works like a champ, running Windows Mobile 10. 

Did I mention I love Windows Mobile 10? Oh, right.