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.

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.

LIGHT SENSOR

• 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:
155 IF A=0 CLS: ?”NO LIGHT DETECTED”:DELAY 2000

Or, you can test for too much light:
155 IF A>=600 CLS:?”THE LIGHT IS TOO BRIGHT.”:DELAY 2000

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() {
Serial.begin(9600);

}

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); 
  delay(100);       
}

TOUCH SENSOR

• 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() {
  Serial.begin(9600);
  // 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:
  Serial.begin(9600);
}

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.