You are on page 1of 9

Amplify

Unplug, connect.

Andrew Chan March 28th, 2013

Amplify
Unplug, connect.

Circuits and Code


LED

Arduino Code
#include <SoftwareSerial.h> int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2 int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3 int led = 5; // this is a PWM pin, uses analogWrite int ledTester = 11; // for testing purposes to see if PWM is working properly on led 5 oat in, out;

Sound Meter Code


import java.io.IOException; import android.media.MediaRecorder; import android.os.Environment; public class SoundMeter { static nal private double EMA_FILTER = 0.6; private MediaRecorder mRecorder = null; private double mEMA = 0.0;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); void setup() { Serial.begin(9600); // Begin the serial monitor at 9600bps pinMode(led, OUTPUT); pinMode(ledTester, OUTPUT); bluetooth.begin(9600); // The Bluetooth Mate defaults to 115200bps while( bluetooth.available() == 0 ){ Serial.println("Trying to connect..."); // if bluetooth isn't available, blink on and o to indicate it isn't working { for (in = 0; in < 6.283; in = in + 0.001) out = sin(in) * 127.5 + 127.5; analogWrite(led,out); analogWrite(ledTester,out);

LED Circuit Board

public void start() { if (mRecorder == null) { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setOutputFile("/dev/null/"); try { mRecorder.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

Resistor 9v battery
}

} }

mRecorder.start(); mEMA = 0.0;

void loop() { if(bluetooth.available() > 0) // If the bluetooth sent any characters { // Send any characters the bluetooth prints to the serial monitor char incoming = (char)bluetooth.read(); // convert char to integer int volume = incoming - '0';

public void stop() { if (mRecorder != null) { mRecorder.stop(); mRecorder.release(); mRecorder = null; } } public double getTheAmplitude(){ if(mRecorder != null) return (mRecorder.getMaxAmplitude()); else return 1; } public double getAmplitude() { if (mRecorder != null) return (mRecorder.getMaxAmplitude()/2700.0); else return 0; } public double getAmplitudeEMA() { double amp = getAmplitude(); mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA; return mEMA; }

MOSFET

if( volume < 0 || volume > 9 ){ // messed up values volume = 0; } if ( volume == 0 ){ // keep minimum brightness volume = 15; } else{ // search for peak values volume = volume*28; // convert to 0-255 } Serial.println(volume); analogWrite(led, volume); // volume determines brightness of LED, from 0-9 analogWrite(ledTester, volume); // write volume to tester PIN to make sure PWM is working }

Bluetooth Arduino

Continued Next Page


Because of the previous problems that were encountered with the microphone, bluetooth connection and voltage overload, the circuit has been redone into a more simplier version. Now the circuit diagram on the left is built for the LED only. The charger base has been re-equiped with a simple cellphone, used as both the bluetooth and microphone receiver. This solution was thought up because while we were having problems with getting the PRE-amp microphone to work and talking on skype on my iPad, we noticed that an already built device (cellphone) has a sensitive and powerful installed microphone. Hence, we added an application into the cellphone to trigger the LED dectection.

Using the LG P500 as a receiver

Amplify
Unplug, connect.

Circuits and Code

Main Activity Code


mport java.io.IOException; import java.io.OutputStream; import java.lang.re ect.Method; import java.util.UUID; import import import import import import import import import import import import import import import import import com.garytse89.Amplify.R; android.app.Activity; android.bluetooth.BluetoothAdapter; android.bluetooth.BluetoothDevice; android.bluetooth.BluetoothSocket; android.content.Context; android.content.Intent; android.os.Build; android.os.Bundle; android.os.Handler; android.os.PowerManager; android.util.Log; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.TextView; android.widget.Toast;

} @Override public void onResume() { super.onResume(); wl.acquire(); updateTextView(R.id.status, "On resume, need to initiate sound sensor."); // Sound based code try { mSensor.start(); Toast.makeText(getBaseContext(), "Sound sensor initiated.", Toast.LENGTH_SHORT).show(); } catch (IllegalStateException e) { // TODO Auto-generated catch block Toast.makeText(getBaseContext(), "On resume, sound sensor messed up...", Toast.LENGTH_LONG).show(); e.printStackTrace(); } Log.d(TAG, "...onResume - try connect..."); // Set up a pointer to the remote node using it's address. BluetoothDevice device = btAdapter.getRemoteDevice(address); // Two things are needed to make a connection: // A MAC address, which we got above. // A Service ID or UUID. In this case we are using the // UUID for SPP . try { btSocket = createBluetoothSocket(device); } catch (IOException e1) { //errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.getMessage() + "."); } // Discovery is resource intensive. Make sure it isn't going on // when you attempt to connect and pass your message. btAdapter.cancelDiscovery(); // Establish the connection. This will block until it connects. Log.d(TAG, "...Connecting..."); updateTextView(R.id.status, "Bluetooth Status: Connecting..."); try { btSocket.connect(); Log.d(TAG, "...Connection ok..."); updateTextView(R.id.status, "Bluetooth Status: Connected"); } catch (IOException e) { try { btSocket.close(); } catch (IOException e2) { //errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); } } // Create a data stream so we can talk to server. Log.d(TAG, "...Create Socket..."); updateTextView(R.id.status, "Bluetooth Status: Trying / Creating socket..."); try { outStream = btSocket.getOutputStream(); } catch (IOException e) { //errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + "."); } }

public void updateTextView(int text_id, String toThis) { TextView val = (TextView) val.setText(toThis); } return; ndViewById(text_id);

private void start() throws IllegalStateException, IOException { mSensor.start(); } private void stop() { mSensor.stop(); private void sleep() { mSensor.stop();

public class MainActivity extends Activity { private static String volumeVisual = ""; private static nal String TAG = "bluetooth1"; private PowerManager.WakeLock wl; private Handler handler; private SoundMeter mSensor; private TextView volumeLevel, status; private BluetoothAdapter btAdapter = null; private BluetoothSocket btSocket = null; private OutputStream outStream = null; // SPP UUID service private static nal UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // MAC-address of Bluetooth module (you must edit this line) private static String address = "00:06:66:49:54:31"; /** Called when the activity is rst created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(getBaseContext(), "Loading...", Toast.LENGTH_LONG).show(); setContentView(R.layout.activity_main); // Wakelock PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNjfdhotDimScreen"); TextView volumeLevel = (TextView) ndViewById(R.id.volumeLevel); TextView status = (TextView) ndViewById(R.id.status); TextView volumeBars = (TextView) ndViewById(R.id.volumeBars); btAdapter = BluetoothAdapter.getDefaultAdapter(); checkBTState(); // Sound-based code mSensor = new SoundMeter(); try { mSensor.start(); Toast.makeText(getBaseContext(), "Sound sensor initiated.", Toast.LENGTH_SHORT).show(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

@Override public void onPause() { updateTextView(R.id.status, "Paused."); super.onPause(); wl.release(); // Wakelock Log.d(TAG, "...In onPause()..."); if (outStream != null) { try { outStream. ush(); } catch (IOException e) { //errorExit("Fatal Error", "In onPause() and failed to } }

handler = new Handler(); nal Runnable r = new Runnable() { public void run() { //mSensor.start(); Log.d("Amplify","HERE"); Toast.makeText(getBaseContext(), "Working!", Toast.LENGTH_LONG).show(); runOnUiThread(new Runnable() { }

ush output stream: " + e.getMessage() + ".");

@Override public void run() { // Get the volume from 0 to 255 in 'int' double volume = 10 * mSensor.getTheAmplitude() / 32768; int volumeToSend = (int) volume; updateTextView(R.id.volumeLevel, "Volume: " + String.valueOf(volumeToSend)); volumeVisual = ""; for( int i=0; i<volumeToSend; i++){ volumeVisual += "|"; }

try { btSocket.close(); } catch (IOException e2) { //errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + "."); }

});

updateTextView(R.id.volumeBars, "Volume: " + String.valueOf(volumeVisual)); //Log.d("Amplify",String.valueOf(volumeToSend)); sendData(Integer.toString(volumeToSend)); updateTextView(R.id.status, "Bluetooth Status: Sending values..."); handler.postDelayed(this, 250); // amount of delay between every cycle of volume level detection + sending the data out } };

private void checkBTState() { // Check for Bluetooth support and then check to make sure it is turned on // Emulator doesn't support Bluetooth and will return null if(btAdapter==null) { //errorExit("Fatal Error", "Bluetooth not support"); } else { if (btAdapter.isEnabled()) { Log.d(TAG, "...Bluetooth ON..."); updateTextView(R.id.status, "Bluetooth Status: ON"); } else { //Prompt user to turn on Bluetooth Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 1); } } } private void errorExit(String title, String message){ Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show(); nish(); } private void sendData(String message) { byte[] msgBu er = message.getBytes(); //Log.d(TAG, "...Send data: " + message + "..."); try { outStream.write(msgBu er); } catch (IOException e) { String msg = "In onResume() and an exception occurred during write: " + e.getMessage(); if (address.equals("00:00:00:00:00:00")) msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 35 in the java code"; msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n"; } //errorExit("Fatal Error", msg);

// Is this line necessary? --- YES IT IS, or else the loop never runs // this tells Java to run "r" handler.postDelayed(r, 250); } private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException { if(Build.VERSION.SDK_INT >= 10){ try { nal Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class }); return (BluetoothSocket) m.invoke(device, MY_UUID); } catch (Exception e) { Log.e(TAG, "Could not create Insecure RFComm Connection",e); } } return device.createRfcommSocketToServiceRecord(MY_UUID);

The Arduino code is then uploaded to the Arduino. Whereas, the sound meter and main activity are uploaded to the cellphone for detection. The functions on the cellphone will then trigger its code to activate the microphone and bluetooth. Thus, the Arduino will detect those signals and emit the LED accordingly.

Amplify
Unplug, connect.

Functions

Simple T est Run on Cellphone

Application on the cellphone

Bluetooth turned on, application activated. Two grey buttons are displayed.

Pushing the ON buttons will trigger the LED to go on through bluetooth detection.

Pushing the OFF buttons will trigger the LED to go off through bluetooth detection.

Different color variations depending on the level of sound detected, it goes from white to green, yellow, red.

Amplify
Unplug, connect.

Visualization
Visual Inspiration

Because of the new solution of building this prototype, the charger no longer needs to be connected to an extra Arduino and beardboard. Therefore, the space is reduced, allowing more freedom to design the charger component. Looking through at some of the visual inspirations, we wanted the design to be more compacted and the USB ports to be facing upwards. After some quick renderings, we decided to go with a matte black and blue color. We wanted a more sleek design that would fit in anywhere and yet still not be too complicated.

Amplify
Unplug, connect.

Prototype

Measurement and sizes, cut and glued together based on Solidworks drawing.

Used matte black spray-paint on the shell of the charger.

Sprayed the plastic pieces with a medium gloss paint.

Assembled together with charger and other parts.

Amplify
Unplug, connect.

Prototype

Similar to the circuit diagram shown on the second page, the LED is connected to the beardboard. From there, there is a MOSFET and a resistor for electrical resistance. Followed by the connection to the Arduino to the bluetooth and powersupply, the circuit is properly connected for testing.

The finished look of the charger. It has 4 USB ports that make it compatible with any cellphone that has a USB plug.

Amplify
Unplug, connect.

Proof of Concept

First, plug the charger and the Arduino to an wall outlet. W ait a few seconds for the Arduino to calibrate its options. Also plug the phone into the charger as well to keep the phone from draining too much power. When the small lights on the bluetooth are on, it is a signal that it is ready for use.

In the above sequence, I had tapped the surface of the table, creating a rather loud thump. The sound was so loud that the phone was able to pick up the noise and reached to a volume of 9 (red) right off the start. The thump was a very quick sound and to avoid a complete shut-off of the LEDs, the volume level gradually lowers back down through the other colors (yellow, green then white). If there was anymore incoming sound, the cellphone would detect it and adjust according to the level of volume it heard and jump to the corresponding color.

END.

You might also like