I am trying to write this code which is interfaced with an app. I want the led and buzzer to turn on when a button is pressed and it should turn off when it goes above or below the angle specified in the code and turn on when its between the angles specified.
//Created by Rohan Dengle /************************************************************************************************************ *******************************************Start of Song Things********************************************* *************************************************************************************************************/ #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_C5 523 #define NOTE_CS5 554 #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 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978 #define REST 0 // Take Me Out to the Ball Game, first 8 notes // Score available at https://www.pianosongdownload.com/Take%20Me%20Out%20to%20the%20Ball%20Game%20Sheet%20Music.pdf // Song arrays contains arrays of size 2 (note, duration) const int ballGameIntro[][2] = { {NOTE_C6, 2},{NOTE_C7, 4},{NOTE_A7, 4},{NOTE_G6, 4},{NOTE_E6, 4},{NOTE_G7, -2},{NOTE_D6, -2} }; int introCount = sizeof(ballGameIntro) / sizeof(ballGameIntro[0]); const int ballGame[][2] = { {NOTE_C5, 2}, {NOTE_C6, 4}, //1 {NOTE_A6, 4}, {NOTE_G5, 4}, {NOTE_E5, 4}, {NOTE_G6, -2}, {NOTE_D5, -2}, {NOTE_C5, 2}, {NOTE_C6, 4}, {NOTE_A5, 4}, {NOTE_G5, 4}, {NOTE_E5, 4}, {NOTE_G5, -1}, {NOTE_A5, 4}, {NOTE_GS5, 4}, {NOTE_A5, 4}, {NOTE_E5, 4}, {NOTE_F5, 4}, {NOTE_G5, 4}, {NOTE_A5, 2}, {NOTE_F5, 4}, {NOTE_D5, -2}, {NOTE_A5, 2}, {NOTE_A5, 4}, {NOTE_A5, 4}, {NOTE_B5, 4}, {NOTE_C6, 4}, {NOTE_D6, 4}, {NOTE_B5, 4}, {NOTE_A5, 4}, {NOTE_G5, 4}, {NOTE_E5, 4}, {NOTE_D5, 4}, {NOTE_C5, 2}, {NOTE_C6, 4}, {NOTE_A5, 4}, {NOTE_G5, 4}, {NOTE_E5, 4}, {NOTE_G5, -2}, {NOTE_D5, 2}, {NOTE_D5, 4}, {NOTE_C5, 2}, {NOTE_D5, 4}, {NOTE_E5, 4}, {NOTE_F5, 4}, {NOTE_G5, 4}, {NOTE_A5, 1}, {NOTE_A5, 4}, {NOTE_B5, 4}, {NOTE_C6, -2}, {NOTE_C6, -2}, {NOTE_C6, 4}, {NOTE_B5, 4}, {NOTE_A5, 4}, {NOTE_G5, 4}, {NOTE_FS5, 4}, {NOTE_G5, 4}, {NOTE_A5, -2}, {NOTE_B5, -2}, {NOTE_C6, -1} }; int noteCount = sizeof(ballGame) / sizeof(ballGame[0]); const int sandstorm[][2] = { {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {REST, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {REST, 16}, {NOTE_A3, 16}, {NOTE_A3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {NOTE_E4, 16}, {REST, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {NOTE_D4, 16}, {REST, 16}, {NOTE_A3, 16}, {NOTE_A3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {NOTE_B3, 16}, {REST, 16} }; int sandstormCount = sizeof(sandstorm) / sizeof(sandstorm[0]); const int chargeIntro[][2] = { {NOTE_B4, 4}, {NOTE_F4, 4},{NOTE_G4, 4}, {NOTE_A4, 4}, }; int chargeIntroCount = sizeof(chargeIntro) / sizeof(chargeIntro[0]); const int chargeOutro[][2] = { {NOTE_C5, -2}, {NOTE_G4, 8}, {NOTE_C5, 8},{NOTE_E5, 8}, {NOTE_G5, -8}, {NOTE_E5, 16}, {NOTE_G5, 2} }; int chargeOutroCount = sizeof(chargeOutro) / sizeof(chargeOutro[0]); /************************************************************************************************************ *******************************************End of Song Things*********************************************** *************************************************************************************************************/ #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #include <Adafruit_LIS3DH.h> #include <NoDelay.h> BLECharacteristic *pCharacteristic; bool deviceConnected = false; float txValue = 0; const int readPin = 32; // Use GPIO number. See ESP32 board pinouts const int LED = 2; // Could be different depending on the dev board. I used the DOIT ESP32 dev board. // Bluetooth Variables int angle = 0; int angleSEL = 0; int blastSEL = 0; boolean soundSEL = false; int lightSEL = 0; boolean vibeSEL = false; boolean buntMode = true; boolean negFeed = false; char checkValue = 0; boolean received = false; unsigned long sleepTime = 0; //PIN LOCATIONS #define GREEN_LED 16 #define YELLOW_LED 17 #define RED_LED 21 #define VIBRATE 9 const int BUZZER = 26; #define SOUND_OFF 0 #define degree_of_freedom 5 // We'll just choose PWM channel 0 here const int TONE_PWM_CHANNEL = 0; // Non Blocking Delays noDelay sendAngle(300); noDelay blinkLED(400); boolean blinkState = true; noDelay trailLED(500); boolean greenState = true, yellowState = false, redState = false; noDelay noVelocity(125); // Speed Variables long refresh_time, sampleRate; long startTime,timeOfSwing; double total_accel; double max_accel; double velocity; uint16_t peakVelocity; int count; int velocityCount; uint16_t data; // Function Declarations void error(const __FlashStringHelper*err); void playSong(int pin, int song[][2], int totalNotes, int tempo); void(* resetFunc) (void) = 0; //declare reset function @ address 0 #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" // Create Accelerometer object Adafruit_LIS3DH lis = Adafruit_LIS3DH(); class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; ledcWriteNote(TONE_PWM_CHANNEL, NOTE_B, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_F, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_G, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_A, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_B, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_F, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_G, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_A, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_C, 5); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_G, 4); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_C, 5); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_E, 5); delay(500); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_G, 6); delay(1000); ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); delay(500); }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string rxValue = pCharacteristic->getValue(); if (rxValue.length() > 0) { Serial.println("*********"); Serial.print("Received Value: "); for (int i = 0; i < rxValue.length(); i++) { Serial.print(rxValue[i]); } Serial.println(); // Do stuff based on the command received from the app if (rxValue.find("a") != -1) { Serial.println("Turning ON!"); digitalWrite(LED, HIGH); } else if (rxValue.find("b") != -1) { Serial.println("Turning OFF!"); digitalWrite(LED, LOW); } // Bat Logic // Bunt Mode /*if (buntMode) { if (!negFeed) { */ // feedback when on target if (rxValue.find("1") != -1 && (angle > 0) && (angle < 5) ) { digitalWrite(GREEN_LED, HIGH); ledcWriteNote(TONE_PWM_CHANNEL, NOTE_C, 5); digitalWrite(VIBRATE, (boolean) vibeSEL); if (rxValue.find("A") != -1){ ledcWriteNote(TONE_PWM_CHANNEL, NOTE_C, 5); //delay(500); } else{ ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); } } else if (rxValue.find("2") != -1 && (angle > 15 - degree_of_freedom) && (angle < 15 + degree_of_freedom)) { digitalWrite(YELLOW_LED, HIGH); digitalWrite(VIBRATE, (boolean) vibeSEL); if (rxValue.find("A") != -1){ ledcWriteNote(TONE_PWM_CHANNEL, NOTE_C, 5); } else{ ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); } } else if (rxValue.find("3") != -1 && (angle > 45 - degree_of_freedom) && (angle < 45 + degree_of_freedom)) { digitalWrite(RED_LED, HIGH); digitalWrite(VIBRATE, (boolean) vibeSEL); if (rxValue.find("A") != -1){ ledcWriteNote(TONE_PWM_CHANNEL, NOTE_C, 5); } else{ ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); } } else { digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, LOW); digitalWrite(RED_LED, LOW); digitalWrite(VIBRATE, LOW); ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); } } if (negFeed) { // Feedback when off target if (rxValue.find("1") != -1 && ((angle < 5 ) || (angle > 0))) { digitalWrite(GREEN_LED, HIGH); digitalWrite(VIBRATE, (boolean) vibeSEL); if (rxValue.find("A") != -1){ ledcWriteNote(TONE_PWM_CHANNEL, NOTE_A, 5); } else{ ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); } } else if (rxValue.find("2") != -1 && ((angle < 15 - degree_of_freedom) || (angle > 15 + degree_of_freedom))) { digitalWrite(YELLOW_LED, HIGH); digitalWrite(VIBRATE, (boolean) vibeSEL); if (rxValue.find("A") != -1){ ledcWriteNote(TONE_PWM_CHANNEL, NOTE_A, 5); } else{ ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); } } else if (rxValue.find("3") != -1 && ((angle < 45 - degree_of_freedom) || (angle > 45 + degree_of_freedom))) { digitalWrite(RED_LED, HIGH); digitalWrite(VIBRATE, (boolean) vibeSEL); if (rxValue.find("A") != -1){ ledcWriteNote(TONE_PWM_CHANNEL, NOTE_A, 5); } else{ ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); } } else { digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, LOW); digitalWrite(RED_LED, LOW); digitalWrite(VIBRATE, LOW); ledcWrite(TONE_PWM_CHANNEL, SOUND_OFF); } } Serial.println(); Serial.println("*********"); } }; void setup() { Serial.begin(115200); ledcAttachPin(BUZZER, TONE_PWM_CHANNEL); pinMode(LED, OUTPUT); // PINS pinMode(GREEN_LED, OUTPUT); pinMode(YELLOW_LED, OUTPUT); pinMode(RED_LED, OUTPUT); pinMode(VIBRATE, OUTPUT); pinMode(BUZZER, OUTPUT); digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, LOW); digitalWrite(RED_LED, LOW); digitalWrite(VIBRATE, LOW); // Create the BLE Device BLEDevice::init("Softball"); // Give it a name // Create the BLE Server BLEServer *pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); // Create a BLE Characteristic pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic->addDescriptor(new BLE2902()); BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE ); pCharacteristic->setCallbacks(new MyCallbacks()); // Start the service pService->start(); // Start advertising pServer->getAdvertising()->start(); Serial.println("Waiting a client connection to notify..."); // Accelerometer Setup Serial.println("LIS3DH test!"); if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address Serial.println("Couldnt start"); while (1) yield(); } Serial.println("LIS3DH found!"); lis.setDataRate(LIS3DH_DATARATE_LOWPOWER_5KHZ); } void loop() { if (deviceConnected) { // Read Accelerometer lis.read(); // Raw Value sensors_event_t event; // In m/s^2 lis.getEvent(&event); // Magnitude of all axis acceleration total_accel = sq(event.acceleration.x) + sq(event.acceleration.y) + sq(event.acceleration.z); total_accel = sqrt(total_accel); // Subtract gravity total_accel = abs(total_accel - 9.8); // Calculate Swing Speed if (micros() > sampleRate){ if (startTime == 0){ timeOfSwing = 0; if (total_accel > 9){ startTime = millis(); } } else{ timeOfSwing = millis() - startTime; if (total_accel > max_accel){ max_accel = total_accel; } if (total_accel < 9){ startTime = 0; max_accel = 0; } } sampleRate = micros() + 200; } velocity = max_accel*(timeOfSwing/1000.0)*2.237; // Scale velocity up since accelerometer is not at tip of the bat velocity*=1.2; // Hold Peak Velocity if ((velocity > peakVelocity) && (velocity < 100)){ peakVelocity = velocity; } // LED feedback from swing speed if (peakVelocity > 10 && peakVelocity <= 40){ digitalWrite(GREEN_LED, HIGH); } else if (peakVelocity > 40 && peakVelocity <= 60){ digitalWrite(GREEN_LED, HIGH); digitalWrite(YELLOW_LED, HIGH); } else if (peakVelocity > 60){ digitalWrite(GREEN_LED, HIGH); digitalWrite(YELLOW_LED, HIGH); digitalWrite(RED_LED, HIGH); } // Detects no movement for 5 seconds if (noVelocity.update()){ if (velocity < 10){ velocityCount++; } else{ velocityCount = 0; } } // Reset Peak Velocity to 0 and turn off LED if no movement if (velocityCount >= 5){ velocityCount = 0; peakVelocity = 0; digitalWrite(GREEN_LED, LOW); digitalWrite(YELLOW_LED, LOW); digitalWrite(RED_LED, LOW); } // Calculate angle if (sendAngle.update()){ angle = abs(57 * atan2 ((double) lis.x, (double) lis.z)); // this is the angle if (checkValue != 0){ Serial.print(checkValue); checkValue = 0; } else{ // Combine angle and speed into 1 integer to send via bluetooh data = angle << 8; data = data | peakVelocity; Serial.print(data); } } // Let's convert the value to a char array: char txString[8]; // make sure this is big enuffz dtostrf(data, 1, 0, txString); // float_val, min_width, digits_after_decimal, char_buffer pCharacteristic->setValue(txString); pCharacteristic->notify(data); // Send the value to the app! Serial.print("*** Sent Value: "); Serial.print(txString); Serial.println(" ***"); } delay(1000); } i am not able to get the output