====== Analog-In/Digital-Out (Potentiometer and LEDs) ======
===== Intro =====
HiTechnic's Prototype Board allows analog input and digital input and output to be easily achieved with a breadboard for circuit prototyping and a LEGO NXT from the Mindstorms robotics kit. This board connects to the standard NXT sensor port and communicates over an I2C serial bus.
The files to accompany the HiTechnic Prototype Board (old version, not "SuperPro") can be found on [[http://www.hitechnic.com/products|HiTechnic'c Downloads page]] or right here:
{{HiTechnic_ExperimentersKitA.zip}}
The PDF handbook included in the file above can also be downloaded here:
{{HiTechnicExperimentersKitHandbookA.pdf}}
===== Wiring =====
* 220 Ohm Resistor (6)
* 10k Potentiometer (1)
* LED (6)
The six LEDs are connected to six pins that will act as a digital out. The potentiometer's variable resistance causes a variable voltage to be read at the analog input AO. This voltage is read in as a number between 0 and 1024. The LEDs are lit up linearly as the potentiometer is rotated.
For example, when the pin A0 reads a voltage corresponding to 200 (~20%), the first LED will light up. At 400, 2 LEDS will light. At 1000 or greater, all LEDs will be lit. This digital-out pin control can be realized in the code excerpt below:
...
if(inputdata>200) outputdata=b0|b1;
if(inputdata>400) outputdata=b0|b1|b2;
if(inputdata>600) outputdata=b0|b1|b2|b3;
if(inputdata>800) outputdata=b0|b1|b2|b3|b4;
if(inputdata>1000) outputdata=b0|b1|b2|b3|b4|b5;
...
{{PotLEDdiagram.jpg}}\\
Circuit Diagram from HiTechnic's Experimenter's Kit Handbook\\
{{PotCircuitPhoto.jpg}}\\
Photo of completed circuit\\
===== Video =====
{{youtube>pIjAGGYgIfI?large}}\\
**Note:** If the video doesn't work just reload the page.
===== NXC Code =====
/*
Filename: PotLED.nxc
Based on HiTechnic Experimenter's Kit Program
(c) HiTechnic 2009
(c) Alex Alspach 2012
*/
#include "NXCDefs.h"
#define PROTO_PORT IN_1
int inputdata;
int outputdata;
int count;
byte cmndbuf[]; // buffer for outbound I2C command
byte respbuf[]; // buffer for inbound I2C response
/* protoboard I/O map
42,43 - A0 input
44,45 - A1 input
46,47 - A2 input
48,49 - A3 input
4A,4B - A4 input
4C - B inputs
4D - B outputs
4E - B controls
*/
void readdata()
{
ArrayInit(cmndbuf, 0, 2); // set the buffer to hold 2 values
cmndbuf[0] = 0x02; // set write to channel
cmndbuf[1] = 0x42; // to set read address
count=2; // 2 bytes to read
I2CBytes(PROTO_PORT, cmndbuf, count, respbuf); // issue I2C write command and read the byte back
inputdata=respbuf[0]*4+respbuf[1]; // create input value by reading the high order byte,
// shift it left 3 bits and add the low order byt to
//create a full 10 bit value
}
void writedata()
{
ArrayInit(cmndbuf, 0, 3); // set the buffer to hold 3 values
cmndbuf[0] = 0x02; // set write to channel
cmndbuf[1] = 0x4D; // to set write address
cmndbuf[2] = outputdata; // to set write data
count=0; // no bytes to read
I2CBytes(PROTO_PORT, cmndbuf, count, respbuf); // issue I2C write command and read the byte back
}
task main()
{
byte b0 = 1;
byte b1 = 2;
byte b2 = 4;
byte b3 = 8;
byte b4 = 16;
byte b5 = 32;
SetSensorLowspeed(PROTO_PORT); // set sensor port 1 to low speed serial (I2C)
Wait(100);
ArrayInit(cmndbuf, 0, 3); // set the buffer to hold 3 values
cmndbuf[0] = 0x02; // set write to channel
cmndbuf[1] = 0x4E; // to set write address
cmndbuf[2] = 0x3F; // to write 111111
count=0; // no bytes to read
I2CBytes(PROTO_PORT, cmndbuf, count, respbuf); // issue I2C write command
Wait(100);
while (TRUE)
{
readdata(); // read the analog port 0
ClearScreen();
NumOut(20, LCD_LINE1, inputdata);
outputdata=b0; //set the output value to turn one LED on
if(inputdata>200) outputdata=b0|b1; // based on the input value
if(inputdata>400) outputdata=b0|b1|b2;
if(inputdata>600) outputdata=b0|b1|b2|b3;
if(inputdata>800) outputdata=b0|b1|b2|b3|b4;
if(inputdata>1000) outputdata=b0|b1|b2|b3|b4|b5;
writedata();
Wait(50);
}
}
===== Download =====
{{PotLED.zip}}