User Tools

Site Tools


nxt_sensor-in_digital-out_microphone_and_leds

This is an old revision of the document!


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.

{|style=“ border-collapse: separate; border-spacing: 15; border-width: 0px; border-style: solid; border-color: #FFFFFF; padding: 0px; text-align: center;”

!style=“width: 75px; background: #F0F0F0”| Part !style=“width: 100px; background: #F0F0F0”| Quantity

<br> 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:<br><br>

<source lang=“c” line start=“92”> … 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; … </source>

<br><br>

{| style=“text-align: center; margin: 1em auto 1em auto;”

{|

style=“border: 1px solid #CCCCCC;” valign=“center”
style=“width: 10px”
style=“border: 1px solid #CCCCCC;” valign=“center”
style=“border: 1px solid #CCCCCC; background: #F9F9F9”
style=“border: 1px solid #CCCCCC; background: #F9F9F9”
Video

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

NXC Code

<source lang=“c” line start=“1”>

#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

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();

} </source>

Download

nxt_sensor-in_digital-out_microphone_and_leds.1478040211.txt.gz · Last modified: 2016/11/01 15:43 by dwallace