NXT Sensor-In / Digital-Out (Microphone and LEDs)

From DASL MediaWiki
Jump to: navigation, search

Contents

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.

Part Quantity
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:

  1. ...
  2.  if(inputdata>20)  outputdata=b0|b1;
  3.  if(inputdata>40)  outputdata=b0|b1|b2;
  4.  if(inputdata>60)  outputdata=b0|b1|b2|b3;
  5.  if(inputdata>80)  outputdata=b0|b1|b2|b3|b4;
  6.  if(inputdata>90)  outputdata=b0|b1|b2|b3|b4|b5;
  7. ...



Circuit Diagram from HiTechnic's Experimenter's Kit Handbook Photo of completed circuit
Circuit Diagram from HiTechnic's Experimenter's Kit Handbook Photo of completed circuit:

Video


Note: If the video doesn't work just reload the page.

NXC Code

  1. /*
  2. Filename: MicLED.nxc
  3. Based on HiTechnic Experimenter's Kit Program
  4.  
  5. (c) HiTechnic 2009
  6. (c) Alex Alspach 2012
  7.  
  8. */
  9.  
  10. #include "NXCDefs.h"
  11.  
  12. #define PROTO_PORT IN_1
  13.  
  14. int inputdata;
  15. int outputdata;
  16. int count;
  17. byte cmndbuf[];                 // buffer for outbound I2C command
  18. byte respbuf[];                 // buffer for inbound I2C response
  19.  
  20. /* protoboard I/O map
  21.    42,43 - A0 input
  22.    44,45 - A1 input
  23.    46,47 - A2 input
  24.    48,49 - A3 input
  25.    4A,4B - A4 input
  26.    4C    - B inputs
  27.    4D    - B outputs
  28.    4E    - B controls
  29. */
  30.  
  31. void readdata()
  32.   {
  33.   ArrayInit(cmndbuf, 0, 2);     // set the buffer to hold 2 values
  34.   cmndbuf[0] = 0x02;            // set write to channel
  35.   cmndbuf[1] = 0x42;            // to set read address
  36.   count=2;                      // 2 bytes to read
  37.   I2CBytes(PROTO_PORT, cmndbuf, count, respbuf);  // issue I2C write command and read the byte back
  38.   inputdata=respbuf[0]*4+respbuf[1];              // create input value by reading the high order byte,
  39.                                                   // shift it left 3 bits and add the low order byt to
  40.                                                   //create a full 10 bit value
  41.   }
  42.  
  43. void writedata()
  44.   {
  45.   ArrayInit(cmndbuf, 0, 3);     // set the buffer to hold 3 values
  46.   cmndbuf[0] = 0x02;            // set write to channel
  47.   cmndbuf[1] = 0x4D;            // to set write address
  48.   cmndbuf[2] = outputdata;      // to set write data
  49.   count=0;                      // no bytes to read
  50.   I2CBytes(PROTO_PORT, cmndbuf, count, respbuf);  // issue I2C write command and read the byte back
  51.   }
  52.  
  53. task main()
  54.   {
  55.  
  56.   byte b0 = 1;
  57.   byte b1 = 2;
  58.   byte b2 = 4;
  59.   byte b3 = 8;
  60.   byte b4 = 16;
  61.   byte b5 = 32;
  62.  
  63.  
  64.   int soundSensorValue;
  65.   string stgSoundSensorValue;
  66.   string stgMessageandValue;
  67.  
  68.   SetSensorSound(IN_2);
  69.  
  70.   SetSensorLowspeed(PROTO_PORT); // set sensor port 1 to low speed serial (I2C)
  71.   Wait(100);
  72.  
  73.  
  74.   ArrayInit(cmndbuf, 0, 3);     // set the buffer to hold 3 values
  75.   cmndbuf[0] = 0x02;            // set write to channel
  76.   cmndbuf[1] = 0x4E;            // to set write address
  77.   cmndbuf[2] = 0x3F;            // to write 111111
  78.   count=0;                      // no bytes to read
  79.   I2CBytes(PROTO_PORT, cmndbuf, count, respbuf);  // issue I2C write command
  80.   Wait(100);
  81.  
  82.   while (TRUE)
  83.     {
  84.  
  85.     soundSensorValue = Sensor(IN_2);
  86.     inputdata =  soundSensorValue;
  87.  
  88.     ClearScreen();
  89.  
  90.     NumOut(20, LCD_LINE1, inputdata);
  91.  
  92.     outputdata=b0;                      //set the output value to turn one LED on
  93.     if(inputdata>20) outputdata=b0|b1;     //       based on the input value
  94.     if(inputdata>40) outputdata=b0|b1|b2;
  95.     if(inputdata>60) outputdata=b0|b1|b2|b3;
  96.     if(inputdata>80) outputdata=b0|b1|b2|b3|b4;
  97.     if(inputdata>90) outputdata=b0|b1|b2|b3|b4|b5;
  98.     writedata();
  99.  
  100.     Wait(50);
  101.     }
  102.  
  103.     StopAllTasks();
  104. }

Download

File:MicLED.zip

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox