Skip to content

Instantly share code, notes, and snippets.

@maloneyN
Created February 27, 2017 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maloneyN/16d8bef9e1056bd252eacd2095cac90f to your computer and use it in GitHub Desktop.
Save maloneyN/16d8bef9e1056bd252eacd2095cac90f to your computer and use it in GitHub Desktop.
int trigPin = 7;
int echoPin = 6;
int speakerPin = 8;
long duration, cm, inches;
String scale;
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2
#define ADAFRUITBLE_RST 9
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
Adafruit_BLE_UART uart = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
void aciCallback(aci_evt_opcode_t event)
{
switch(event)
{
case ACI_EVT_DEVICE_STARTED:
Serial.println(F("Advertising started"));
break;
case ACI_EVT_CONNECTED:
Serial.println(F("Connected!"));
break;
case ACI_EVT_DISCONNECTED:
Serial.println(F("Disconnected or advertising timed out"));
break;
default:
break;
}
}
void rxCallback(uint8_t *buffer, uint8_t len)
{
Serial.print(F("Received "));
Serial.print(len);
Serial.print(F(" bytes: "));
for(int i=0; i<len; i++){
Serial.print((char)buffer[i]);
scale += (char)buffer[i];
}
Serial.print(F(" ["));
for(int i=0; i<len; i++)
{
Serial.print(" 0x"); Serial.print((char)buffer[i], HEX);
}
Serial.println(F(" ]"));
/* Echo the same data back! */
uart.write(buffer, len);
}
void setup(void)
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
while(!Serial); // Leonardo/Micro should wait for serial init
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Callback Echo demo"));
uart.setRXcallback(rxCallback);
uart.setACIcallback(aciCallback);
// uart.setDeviceName("NEWNAME"); /* 7 characters max! */
uart.begin();
}
void loop()
{
uart.pollACI();
Serial.println(scale);
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = (duration/2) / 29.1;
inches = (duration/2) / 74;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
//Serial.println();
if(scale.equals("penta")){
if (inches <= 1){
tone(speakerPin, 1175);
}
else
if (inches == 2){
tone(speakerPin, 1047);
}
else
if (inches == 3){
tone(speakerPin, 880);
}
else
if (inches == 4){
tone(speakerPin, 784);
}
else
if (inches == 5){
tone(speakerPin, 698);
}
else
if (inches == 6){
tone(speakerPin, 587);
}
else
if (inches == 7){
tone(speakerPin, 523);
}
else
if (inches == 8){
tone(speakerPin, 440);
}
else
if (inches == 9){
tone(speakerPin, 392);
}
else
if (inches == 10){
tone(speakerPin, 349);
}
else
if (inches == 11){
tone(speakerPin, 294);
}
else
{
noTone(speakerPin);
} }
else
{
if (scale.equals("chrom")){
if (inches <= 1){
tone(speakerPin, 1175);
}
else
if (inches == 2){
tone(speakerPin, 1109);
}
else
if (inches == 3){
tone(speakerPin, 1047);
}
else
if (inches == 4){
tone(speakerPin, 988);
}
else
if (inches == 5){
tone(speakerPin, 932);
}
else
if (inches == 6){
tone(speakerPin, 880);
}
else
if (inches == 7){
tone(speakerPin, 831);
}
else
if (inches == 8){
tone(speakerPin, 784);
}
else
if (inches == 9){
tone(speakerPin, 740);
}
else
if (inches == 10){
tone(speakerPin, 698);
}
else
if (inches == 11){
tone(speakerPin, 659);
}
else
if (inches == 12){
tone(speakerPin, 622);
}
else
if (inches == 13){
tone(speakerPin, 587);
}}
else
{
noTone(speakerPin);
} }
delay(20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment