/* 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(); }