nxt_sensor-in_digital-out_microphone_and_leds
Table of Contents
NXT Sensor-In/Digital-Out (Microphone and LEDs)
Wiring
NOTE: This is the same setup as the last exercise “Potentiometer and LEDs”. The potentiometer is no longer necessary to the circuit but can remain in place without interference.
Plug the NXT sound sensor (microphone) into sensor port 2 0n the NXT.
- 220 Ohm Resistor (6)
- 10k Potentiometer (1)
- LED (6)
The six LEDs are connected to six digital-out pins. As the volume varies between 0% and 100% of the sensor's range, it outputs a value between 0 and 100. This value is read in the same algorithm as analog input A0 was read before and the amount of lit LEDs corresponds to the output of the sensor. This digital-out pin control can be realized in the code excerpt below:
... if(inputdata>20) outputdata=b0|b1; if(inputdata>40) outputdata=b0|b1|b2; if(inputdata>60) outputdata=b0|b1|b2|b3; if(inputdata>80) outputdata=b0|b1|b2|b3|b4; if(inputdata>90) outputdata=b0|b1|b2|b3|b4|b5; ...
Circuit Diagram from HiTechnic's Experimenter's Kit Handbook
Video
Note: If the video doesn't work just reload the page.
NXC Code
- MicLED.nxc
/* Filename: MicLED.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; int soundSensorValue; string stgSoundSensorValue; string stgMessageandValue; SetSensorSound(IN_2); 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) { soundSensorValue = Sensor(IN_2); inputdata = soundSensorValue; ClearScreen(); NumOut(20, LCD_LINE1, inputdata); outputdata=b0; //set the output value to turn one LED on if(inputdata>20) outputdata=b0|b1; // based on the input value if(inputdata>40) outputdata=b0|b1|b2; if(inputdata>60) outputdata=b0|b1|b2|b3; if(inputdata>80) outputdata=b0|b1|b2|b3|b4; if(inputdata>90) outputdata=b0|b1|b2|b3|b4|b5; writedata(); Wait(50); } StopAllTasks(); }
Download
nxt_sensor-in_digital-out_microphone_and_leds.txt · Last modified: 2016/11/09 13:43 by dwallace