Cart
No products in the cart.
$32.95
Make sounds by giving this sensor a pitch, volume, and duration.
Out of stock
The Sound Generator can generate a sound based on volume, pitch, and duration. This sensor can also overwrite settings during a tone to change how it sounds, change the volume, or to extend the duration of the tone.
Sensor Type | Four Wire I2C Sensor |
Default I2C Address | 0x34 |
Sensor ID Code | 0x53 |
Dimensions | 32 mm x 32 mm x19 mm |
Mounting Holes | 24 mm x 24 mm |
Power | 5 V DC, 20 mA max |
Register | Function |
0x00 | Sensor Firmware Revision |
0x01 | Manufacturer Code |
0x02 | Sensor ID Code |
0x04 | Sound Level |
0x05/0x06 | Pitch (lsb/msb) |
0x07 | Duration |
The order if the 4 control bytes, Sound Level, Pitch low, Pitch high and Duration are arranged such that a signal 4-byte write can be used to initiate a tone.
Sound Level:
Controls the amplitude of the output signal from 0 to 3 where 0 is the quietest and 3 is the loudest.
Pitch:
Consisting of 2 bytes to make a word, the Pitch controls the frequency of the output in increments of 1Hz. The frequencies range from 1Hz to 65kHz, although operation over 5kHz is not recommended and may damage the device. The speaker resonates at about 2kHz, so the speaker will sound much louder at this frequency.
Duration:
This controls the duration of the tone in increments of 10mS. The duration of the tone can range from 10mS – 2.55S. The duration of the tone begins a countdown and will stop when the counter reaches 0. The duration may be updated at any time to extend the length of a tone past 2.55S.
You will need both of the below files in the same program folder.
/* DO NOT EDIT THIS PROGRAM Modern Robotics Sound Generator Driver Created 7/24/2017 by Colton Mehlhoff of Modern Robotics using FTC SDK 3.10 Reuse permitted with credit where credit is due This class provides functions to use the Sound Generator https://modernroboticsinc.com/sound-generator See MRI_Sound_Generator.java for example usage. Support is available by emailing support@modernroboticsinc.com */ package org.firstinspires.ftc.teamcode; import com.qualcomm.robotcore.hardware.HardwareMap; import com.qualcomm.robotcore.hardware.I2cAddr; import com.qualcomm.robotcore.hardware.I2cDevice; import com.qualcomm.robotcore.hardware.I2cDeviceSynch; import com.qualcomm.robotcore.hardware.I2cDeviceSynchImpl; public class MRISoundGenerator { private byte[] soundGcache; private I2cDevice soundG; private I2cDeviceSynch reader; HardwareMap hwMap = null; int letter[] = new int[]{0x42, 0x81, 0x85, 0x61, 0x20, 0x84, 0x63, 0x80, 0x40, 0x8E, 0x65, 0x82, 0x43, 0x41, 0x67, 0x86, 0x8B, 0x62, 0x60, 0x21, 0x64, 0x88, 0x66, 0x89, 0x8D, 0x83}; int number[] = new int[]{0xBF, 0xBE, 0xBC, 0xB8, 0xB0, 0xA0, 0xA1, 0xA3, 0xA7, 0xAF}; public MRISoundGenerator() { } public void init(HardwareMap ahwMap, String cfgName) { init(ahwMap, cfgName, 0x34); //Default I2C address for color beacon is 0x4c } public void init(HardwareMap ahwMap, String cfgName, int i2cAddr8) { hwMap = ahwMap; soundG = hwMap.i2cDevice.get(cfgName); reader = new I2cDeviceSynchImpl(soundG, I2cAddr.create8bit(i2cAddr8), false); reader.engage(); } //Sets sound and then continues with your program while the sound is playing. //Level is 0, 1, 2, or 3 //Frequency is 1-5000 //Length is 10 to 2550 in milliseconds void setSound(int level, int frequency, int length) { if (level < 0 || level > 3 || frequency < 1 || frequency > 5000 || length < 10 || length > 2550) return; else { int lsb = (frequency & 0x00FF); int msb = (frequency & 0xFF00) >> 8; length = length / 10; reader.write(0x04, new byte[]{(byte) level, (byte) lsb, (byte) msb, (byte) length}); } } //Play a sound and wait for the sound to finish before continuing with the program //Length can be more then 2550 ms in this function void setSoundBlocking(int level, int frequency, int length) throws InterruptedException { setSoundBlocking(level, frequency, length, 0); } //Play a sound and wait for the sound to finish before continuing with the program //Length can be more then 2550 ms in this function void setSoundBlocking(int level, int frequency, int length, int post_pause) throws InterruptedException { int temp; if (level < 0 || level > 3 || frequency < 1 || frequency > 5000 || length < 10) return; else { int lsb = frequency & 0x00FF; int msb = (frequency & 0xFF00) >> 8; if (length >= 2550) temp = 255; else temp = length / 10; reader.write(0x04, new byte[]{(byte) level, (byte) lsb, (byte) msb, (byte) temp}); length = length - 2550; while (length > 0) { while (getDuration() >= 20) ; if (length >= 2550) { length = length - 2550; temp = 255; } else { temp = length / 10; length = 0; } reader.write(0x07, new byte[]{(byte) temp}); } } while (getDuration() != 0) ; Thread.sleep(post_pause); } //Set volume of 0, 1, 2, or 3 void setVolume(int level) { if (level < 0 || level > 3) return; else { reader.write(0x04, new byte[]{(byte) level}); } } //Set the frequency void setPitch(int frequency) { if (frequency < 1 || frequency > 5000) return; else { int lsb = frequency & 0x00FF; int msb = (frequency & 0x00FF) >> 8; reader.write(0x05, new byte[]{(byte) lsb, (byte) msb}); } } //Set the duration of the sound. The level and pitch last sent to the sensor will be used. void setDuration(int length) { if (length < 10 || length > 2550) return; else { length = length / 10; reader.write(0x07, new byte[]{(byte) length}); } } //Read the reamining duration form the sensor in ms int getDuration() { byte receive[] = reader.read(0x07, 1); return (int) receive[0] * 10; } //Send a text string with level and frequency to be played in morse code void morseCode(String message, int level, int frequency) throws InterruptedException { if (level < 0 || level > 3 || frequency < 1 || frequency > 5000) return; else { for (int i = 0; i < message.length(); i++) { int length; int index, morse_letter, temp = 0; if (message.charAt(i) >= 0x41 && message.charAt(i) <= 0x5A) { index = message.charAt(i) - 0x41; temp = letter[index]; } else if (message.charAt(i) >= 0x61 && message.charAt(i) <= 0x7A) { index = message.charAt(i) - 0x61; temp = letter[index]; } else if (message.charAt(i) >= 0x30 && message.charAt(i) <= 0x39) { index = message.charAt(i) - 0x30; temp = number[index]; } else { return; } length = (temp & 0xE0) >> 5; morse_letter = temp & 0x1F; for (int n = 0; n < length; n++) { if ((morse_letter & 0x01) == 0x01) setSoundBlocking(level, frequency, 100, 50); else if ((morse_letter & 0x01) == 0x00) setSoundBlocking(level, frequency, 50, 50); morse_letter = morse_letter >> 1; } Thread.sleep(250); } } return; } }
/* Modern Robotics Color Generator Example Created 7/24/2017 by Colton Mehlhoff of Modern Robotics using FTC SDK 3.10 Reuse permitted with credit where credit is due Configuration: I2CDevice "sg" (MRI Sound Generator with default I2C address 0x34) MRISoundGenerator class must be in the same folder as this program. Download from https://modernroboticsinc.com/sound-generator To change I2C Addresses, go to http://modernroboticsedu.com/mod/lesson/view.php?id=96 Support is available by emailing support@modernroboticsinc.com */ package org.firstinspires.ftc.teamcode; import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; import com.qualcomm.robotcore.eventloop.opmode.TeleOp; @TeleOp(name = "Sound Generator", group = "MRI") //@Disabled public class MRI_Sound_Generator extends LinearOpMode { MRISoundGenerator sound = new MRISoundGenerator(); int frequency = 100; int direction = 10; boolean y = false; @Override public void runOpMode() throws InterruptedException { telemetry.addData("Status", "Initialized"); telemetry.update(); sound.init(hardwareMap, "sg"); //initializes the I2CDevice. Second parameter is the name of the sensor in the configuration file. waitForStart(); while (opModeIsActive()) { if (gamepad1.a) { sound.morseCode("hello world", 2, 1500); } if (gamepad1.b) { sound.setSound(1, 1350, 200); } if(gamepad1.y){ y = true; frequency = frequency + direction; if(frequency < 500) direction = 50; if (frequency > 1000) direction = -50; sound.setSound(3, frequency, 20); } if(gamepad1.right_trigger > 0.05){ sound.setSound(3, (int)(gamepad1.right_trigger * 5000), 20); } telemetry.addData("frequency", frequency); telemetry.addData("y", y); telemetry.update(); } } }
Weight | .0415 lbs |
---|---|
Dimensions | 4 × 3.75 × 1 in |
Out of stock
Out of stock
Out of stock
Out of stock