// // We connect the VCC pin of the HC-07 to pin P1.4 // So we can control when the chip gets power. // We connect P1.3 to the KEY pin on the HC-07, so // we can enter AT mode on the module to program it. // int counter = 0; char buf[64]; bool receive( char *buffer, int bsize ) { delay( 1000 ); // One command per second int x = 0; while( Serial.available() && x < bsize-1 ) { char c = Serial.read(); buffer[x++] = c; buffer[x] = 0; // delay( 10 ); } return buffer[0] == 'O' && buffer[1] == 'K'; } enum { POWER = P1_4, KEY = P1_3, STATE = P1_5 }; void AT_mode( void ) { // // This sequence puts the HC-07 into AT mode at 9600 baud // digitalWrite( POWER, LOW ); // power to HC-07 is off digitalWrite( KEY, LOW ); // KEY input is OFF delay( 1000 ); digitalWrite( POWER, HIGH ); // Power the HC-07 on delay( 1000 ); digitalWrite( KEY, HIGH ); // KEY input is ON delay( 1000 ); // AT commands need a 1 second wait after each one. } void comm_mode( void ) { // // The sequence to go into normal communication mode. // digitalWrite( POWER, LOW ); // power to HC-07 is off digitalWrite( KEY, LOW ); // KEY input is OFF digitalWrite( POWER, HIGH ); // Power the HC-07 on } enum { B1200 = '1', B2400 = '2', B4800 = '3', B9600 = '4', B19200 = '5', B38400 = '6', B57600 = '7', B115200 = '8', B230400 = '9', B460800 = 'A', B921600 = 'B', B1382400 = 'C' }; bool test( void ) { Serial.print( "AT" ); // NO CR LF delay( 1000 ); return receive( buf, sizeof buf ); // We expect "OK" } bool baud( const char rate_char, int rate ) { Serial.print( "AT+BAUD" ); Serial.print( rate_char ); // No CR LF delay( 1000 ); bool r = receive( buf, sizeof buf ); // We expect "OKrate" e.g. "OK9600" if( true || r ) { Serial.begin( rate ); } return r; } bool setpin( const char *pin ) { Serial.print( "AT+PIN" ); Serial.print( pin ); // No CR LF delay( 1000 ); return receive( buf, sizeof buf ); // We expect "OKsetPIN" } bool get_version( void ) { Serial.print( "AT+VERSION" ); // NO CR LF delay( 1000 ); return receive( buf, sizeof buf ); // We expect "OKlinvorV1.6" } bool rename_HC07( const char *name ) { // Tell the HC-07 to broadcast a more unique name than "HC-07" // when other computers scan for Bluetooth devices. // You can use any name up to 20 bytes long. // Serial.print( "AT+NAME" ); Serial.print( name ); delay( 1000 ); return receive( buf, sizeof buf ); // We expect "OKsetname" } void get_string( char *str, int len ) { --len; char c = 0; while( c != '\n' && c != '\r' && len > 0 ) { if( Serial.available() ) { if( c != '\r' && c != '\n' ) { c = Serial.read(); *str++ = c; len--; } } } *str = 0; } void setup() { Serial.begin( 9600 ); delay( 50 ); pinMode( KEY, OUTPUT ); pinMode( POWER, OUTPUT ); pinMode( STATE, INPUT ); pinMode( RED_LED, OUTPUT ); pinMode( GREEN_LED, OUTPUT ); digitalWrite( RED_LED, HIGH ); digitalWrite( GREEN_LED, LOW ); AT_mode(); // test(); // get_version(); // setpin( "1234" ); rename_HC07( "Tilt Tones" ); // baud( B9600, 9600 ); // baud_return = baud( B115200, 115200 ); // baud_return = baud( B19200, 19200 ); digitalWrite( RED_LED, LOW ); digitalWrite( GREEN_LED, HIGH ); Serial.println( "Ready" ); } // // Scan to the desired character, null out that character, and return the string past it. // char * skip_past( char *pos, char end_char ) { while( *pos && *pos != end_char ) pos++; if( *pos ) *pos++ = 0; return pos; } long int last_time = 0; void loop() { if( Serial.available() ) { get_string( buf, sizeof buf ); // // Strings coming in from the phone's accelerometers look like this: // Pos: [-981,981,123]\r\n // char *pos = skip_past( buf, '[' ); if( *pos ) { char *x_str = pos; pos = skip_past( pos, ',' ); int x_val = atoi( x_str ); if( x_val > 0 ) tone( P2_2, x_val ); else noTone( P2_2 ); char *y_str = pos; pos = skip_past( pos, ',' ); int y_val = atoi( y_str ); if( y_val > 0 ) tone( P2_3, y_val ); else noTone( P2_3 ); char *z_str = pos; pos = skip_past( pos, ']' ); int z_val = atoi( z_str ); if( z_val > 0 ) tone( P2_4, z_val ); else noTone( P2_4 ); last_time = millis(); } } if( millis() - last_time > 2000 ) { noTone( P2_2 ); noTone( P2_3 ); noTone( P2_4 ); } }