├── README.txt ├── dev └── path_send_test │ └── path_send_test.ino ├── examples ├── ArduinoBlue_basic │ └── ArduinoBlue_basic.ino ├── ArduinoBlue_functions_example │ └── ArduinoBlue_functions_example.ino ├── HM10_console │ └── HM10_console.ino ├── HM10_interface │ └── HM10_interface.ino ├── HM10_rename │ └── HM10_rename.ino ├── builtin_serial │ └── builtin_serial.ino ├── drive_robot │ └── drive_robot.ino ├── multiple_LED_brightness │ └── multiple_LED_brightness.ino ├── multiple_LED_switch │ └── multiple_LED_switch.ino └── single_LED_brightness │ └── single_LED_brightness.ino ├── library.properties └── src ├── ArduinoBlue.cpp ├── ArduinoBlue.h └── FunctionType.h /README.txt: -------------------------------------------------------------------------------- 1 | THIS IS THE UNSTABLE DEVELOPMENT VERSION OF THE LIBRARY. 2 | If you're looking for the latest stable version, please go to the releases page: 3 | https://github.com/purwar2016/ArduinoBlue-library/releases 4 | 5 | Tutorial (Beginner Friendly): 6 | https://sites.google.com/stonybrook.edu/arduinoble/ 7 | 8 | Documentation: 9 | https://github.com/purwar2016/ArduinoBlue-library/wiki/Documentation 10 | -------------------------------------------------------------------------------- /dev/path_send_test/path_send_test.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Tests the path sending and receiving code. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | 9 | // The bluetooth tx and rx pins must be supported by software serial. 10 | // Visit https://www.arduino.cc/en/Reference/SoftwareSerial for unsupported pins. 11 | // Bluetooth TX -> Arduino D8 12 | const int BLUETOOTH_TX = 8; 13 | // Bluetooth RX -> Arduino D7 14 | const int BLUETOOTH_RX = 7; 15 | 16 | int button; 17 | 18 | bool hasPathPrinted = false; 19 | 20 | SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX); 21 | ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor 22 | 23 | // Setup code runs once after program starts. 24 | void setup() { 25 | // Start serial monitor at 115200 bps. 26 | Serial.begin(115200); 27 | 28 | // Start bluetooth serial at 9600 bps. 29 | bluetooth.begin(9600); 30 | 31 | // delay just in case bluetooth module needs time to "get ready". 32 | delay(100); 33 | 34 | Serial.println("\n\n______________________________________________________________________________________________________________________________________"); 35 | Serial.println("SETUP COMPLETE"); 36 | Serial.println("PATH SEND TEST\n\n"); 37 | } 38 | 39 | void printCoordinate(float x, float y) { 40 | Serial.print(x); Serial.print("\t"); 41 | Serial.println(y); 42 | } 43 | 44 | // Put your main code here, to run repeatedly: 45 | void loop() { 46 | button = phone.getButton(); 47 | if (button != -1) { 48 | // Test if bluetooth is working properly by pressing any button. 49 | Serial.print("Button: "); Serial.println(button); 50 | } 51 | 52 | if (phone.isPathAvailable() && !hasPathPrinted) { 53 | // PRINT INTERPOLATED PATH 54 | // Get the path data 55 | int length = phone.getPathLength(); 56 | float * pathX = phone.getPathArrayX(); 57 | float * pathY = phone.getPathArrayY(); 58 | 59 | // Print the path data 60 | Serial.println("______________________________________________________________________________________________________________________________________"); 61 | Serial.println("ACTUAL PATH\n"); 62 | for (int i = 0; i < length; i++) { 63 | printCoordinate(pathX[i], pathY[i]); 64 | } 65 | 66 | // PRINT INTERPOLATED PATH 67 | double step = 3; 68 | double distance, xCurr, yCurr, x0, y0, x1, y1; 69 | int numPtsIter; 70 | 71 | Serial.println("______________________________________________________________________________________________________________________________________"); 72 | Serial.println("INTERPOLATED PATH\n"); 73 | 74 | for (int i = 1; i < length - 2; i++) { 75 | // 1. Get the ith and (i+1)st coordinate 76 | x0 = pathX[i]; 77 | y0 = pathY[i]; 78 | x1 = pathX[i + 1]; 79 | y1 = pathY[i + 1]; 80 | 81 | // 2. Get distance between them 82 | distance = sqrt((x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0)); 83 | 84 | // 3. Get number of points needed 85 | numPtsIter = (int) distance / step; 86 | 87 | /*Serial.print("x0: "); Serial.print(x0); 88 | Serial.print("\ty0: "); Serial.print(y0); 89 | Serial.print("\tx1: "); Serial.print(x1); 90 | Serial.print("\ty1: "); Serial.print(y1); 91 | Serial.print("\td: "); Serial.print(distance); 92 | Serial.print("\tnum: "); Serial.println(numPtsIter);*/ 93 | 94 | // 4. Interpolate and print 95 | xCurr = x0; 96 | yCurr = y0; 97 | printCoordinate(x0, y0); // Print the ith coordinate 98 | for (int j = 0; j < numPtsIter; j++) { 99 | xCurr += step; 100 | yCurr = phone.getPathY(xCurr); 101 | printCoordinate(xCurr, yCurr); 102 | } 103 | printCoordinate(x1, y1); // Print the (i+1)st coordinate 104 | } 105 | 106 | hasPathPrinted = true; // So it won't keep on printing 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /examples/ArduinoBlue_basic/ArduinoBlue_basic.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ArduinoBlue example code to demonstrate the features of the app. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | const unsigned long BAUD_RATE = 9600; 9 | 10 | // The bluetooth tx and rx pins must be supported by software serial. 11 | // Visit https://www.arduino.cc/en/Reference/SoftwareSerial for unsupported pins. 12 | // Bluetooth TX -> Arduino D8 13 | const int BLUETOOTH_TX = 8; 14 | // Bluetooth RX -> Arduino D7 15 | const int BLUETOOTH_RX = 7; 16 | 17 | int prevThrottle = 49; 18 | int prevSteering = 49; 19 | int throttle, steering, sliderVal, button, sliderId; 20 | 21 | SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX); 22 | ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor 23 | 24 | // Setup code runs once after program starts. 25 | void setup() { 26 | // Start serial communications. 27 | // The baud rate must be the same for both the serial and the bluetooth. 28 | Serial.begin(BAUD_RATE); 29 | bluetooth.begin(BAUD_RATE); 30 | delay(100); 31 | 32 | Serial.println("setup complete"); 33 | } 34 | 35 | // Put your main code here, to run repeatedly: 36 | void loop() { 37 | // ID of the button pressed pressed. 38 | button = phone.getButton(); 39 | 40 | // Returns the text data sent from the phone. 41 | // After it returns the latest data, empty string "" is sent in subsequent. 42 | // calls until text data is sent again. 43 | String str = phone.getText(); 44 | 45 | // Throttle and steering values go from 0 to 99. 46 | // When throttle and steering values are at 99/2 = 49, the joystick is at center. 47 | throttle = phone.getThrottle(); 48 | steering = phone.getSteering(); 49 | 50 | // ID of the slider moved. 51 | sliderId = phone.getSliderId(); 52 | 53 | // Slider value goes from 0 to 200. 54 | sliderVal = phone.getSliderVal(); 55 | 56 | // Display button data whenever its pressed. 57 | if (button != -1) { 58 | Serial.print("Button: "); 59 | Serial.println(button); 60 | } 61 | 62 | // Display slider data when slider moves 63 | if (sliderId != -1) { 64 | Serial.print("Slider ID: "); 65 | Serial.print(sliderId); 66 | Serial.print("\tValue: "); 67 | Serial.println(sliderVal); 68 | } 69 | 70 | // Display throttle and steering data if steering or throttle value is changed 71 | if (prevThrottle != throttle || prevSteering != steering) { 72 | Serial.print("Throttle: "); Serial.print(throttle); Serial.print("\tSteering: "); Serial.println(steering); 73 | prevThrottle = throttle; 74 | prevSteering = steering; 75 | } 76 | 77 | // If a text from the phone was sent print it to the serial monitor 78 | if (str != "") { 79 | Serial.println(str); 80 | } 81 | 82 | // Send string from serial command line to the phone. This will alert the user. 83 | if (Serial.available()) { 84 | Serial.write("send: "); 85 | String str = Serial.readString(); 86 | phone.sendMessage(str); // phone.sendMessage(str) sends the text to the phone. 87 | Serial.print(str); 88 | Serial.write('\n'); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /examples/ArduinoBlue_functions_example/ArduinoBlue_functions_example.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ArduinoBlue example code to demonstrate the features of the app. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | const unsigned long BAUD_RATE = 9600; 9 | 10 | // The bluetooth tx and rx pins must be supported by software serial. 11 | // Visit https://www.arduino.cc/en/Reference/SoftwareSerial for unsupported pins. 12 | // Bluetooth TX -> Arduino D8 13 | const int BLUETOOTH_TX = 7; 14 | // Bluetooth RX -> Arduino D7 15 | const int BLUETOOTH_RX = 8; 16 | 17 | int prevThrottle = 49; 18 | int prevSteering = 49; 19 | int throttle, steering, sliderVal, button, sliderId; 20 | 21 | float val = 0; 22 | 23 | SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX); 24 | ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor 25 | 26 | // Setup code runs once after program starts. 27 | void setup() { 28 | // Start serial communications. 29 | // The baud rate must be the same for both the serial and the bluetooth. 30 | Serial.begin(BAUD_RATE); 31 | bluetooth.begin(BAUD_RATE); 32 | delay(100); 33 | 34 | Serial.println("setup complete"); 35 | } 36 | 37 | // Put your main code here, to run repeatedly: 38 | void loop() { 39 | // ID of the button pressed pressed. 40 | button = phone.getButton(); 41 | 42 | // Returns the text data sent from the phone. 43 | // After it returns the latest data, empty string "" is sent in subsequent. 44 | // calls until text data is sent again. 45 | String str = phone.getText(); 46 | 47 | // Throttle and steering values go from 0 to 99. 48 | // When throttle and steering values are at 99/2 = 49, the joystick is at center. 49 | throttle = phone.getThrottle(); 50 | steering = phone.getSteering(); 51 | 52 | // ID of the slider moved. 53 | sliderId = phone.getSliderId(); 54 | 55 | // Slider value goes from 0 to 255. 56 | sliderVal = phone.getSliderVal(); 57 | 58 | // Display button data whenever its pressed. 59 | if (button != -1) { 60 | Serial.print("Button: "); 61 | Serial.println(button); 62 | } 63 | 64 | // Display slider data when slider moves 65 | if (sliderId != -1) { 66 | Serial.print("Slider ID: "); 67 | Serial.print(sliderId); 68 | Serial.print("\tValue: "); 69 | Serial.println(sliderVal); 70 | } 71 | 72 | // Display throttle and steering data if steering or throttle value is changed 73 | if (prevThrottle != throttle || prevSteering != steering) { 74 | Serial.print("Throttle: "); Serial.print(throttle); Serial.print("\tSteering: "); Serial.println(steering); 75 | prevThrottle = throttle; 76 | prevSteering = steering; 77 | } 78 | 79 | // If a text from the phone was sent print it to the serial monitor and send it back to the terminal on the phone 80 | if (str != "") { 81 | Serial.println(str); 82 | phone.sendMessage(str); // phone.sendMessage(str) sends the text to the phone. 83 | unsigned int in = 2; 84 | phone.sendDisplayData(in, str); 85 | } 86 | 87 | // Send string from serial command line to the phone. This will show up in the terminal. 88 | if (Serial.available()) { 89 | Serial.write("send: "); 90 | String str = Serial.readString(); 91 | phone.sendMessage(str); 92 | phone.sendDisplayData(2, str); 93 | Serial.print(str); 94 | Serial.write('\n'); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /examples/HM10_console/HM10_console.ino: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | const unsigned long BAUD_RATE = 9600; 5 | 6 | // The bluetooth tx and rx pins must be supported by software serial. 7 | // Visit https://www.arduino.cc/en/Reference/SoftwareSerial for unsupported pins. 8 | // Bluetooth TX -> Arduino D8 9 | const int BLUETOOTH_TX = 7; 10 | // Bluetooth RX -> Arduino D7 11 | const int BLUETOOTH_RX = 8; 12 | 13 | const int READ_TIME = 500; 14 | 15 | SoftwareSerial bt(BLUETOOTH_TX, BLUETOOTH_RX); 16 | 17 | void setup() { 18 | // Baud rate must be same for bluetooth and serial. 19 | // It helps to prevent interference. 20 | bt.begin(BAUD_RATE); 21 | Serial.begin(BAUD_RATE); 22 | Serial.println("Setup Complete"); 23 | } 24 | 25 | void loop() { 26 | // while incoming signal is available. 27 | if (bt.available()) { 28 | unsigned long readStart = millis(); 29 | while (bt.available() || millis() - readStart < READ_TIME) { 30 | if (bt.available()) { 31 | Serial.print((char)bt.read()); 32 | } 33 | } 34 | Serial.println(); 35 | } 36 | 37 | while (Serial.available()) { 38 | String input = Serial.readStringUntil('\r'); 39 | String output = ""; 40 | for (int i = 0; i < input.length(); i++) { 41 | char c = input.charAt(i); 42 | if (c != 10 && c != 13) { // Don't send line end characters to HM 10 43 | output += c; 44 | } 45 | } 46 | Serial.print("Entered: "); Serial.println(output); 47 | bt.print(output); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /examples/HM10_interface/HM10_interface.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned long BAUD_RATE = 9600; 4 | 5 | const int BLUETOOTH_TX = 7; 6 | const int BLUETOOTH_RX = 8; 7 | 8 | const int READ_TIME = 500; //ms 9 | 10 | unsigned long prevMillis; 11 | 12 | SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX); 13 | 14 | void setup() { 15 | // Start serial communications. 16 | // The baud rate must be the same for both the serial and the bluetooth. 17 | Serial.begin(BAUD_RATE); 18 | bluetooth.begin(BAUD_RATE); 19 | Serial.println("Setup Complete"); 20 | } 21 | 22 | void loop() { 23 | // put your main code here, to run repeatedly: 24 | if (Serial.available()) { 25 | String str = ""; 26 | Serial.print("Input: "); 27 | 28 | prevMillis = millis(); 29 | while (millis() - prevMillis < READ_TIME) { 30 | if (Serial.available()) { 31 | char c = Serial.read(); 32 | if (c != 10 && c != 13) { // Don't send line end characters to HM10. 33 | str += c; 34 | } 35 | } 36 | } 37 | 38 | bluetooth.print(str); 39 | Serial.println(str); 40 | } 41 | 42 | if (bluetooth.available()) { 43 | String str = ""; 44 | Serial.print("HM10: "); 45 | 46 | prevMillis = millis(); 47 | while (millis() - prevMillis < READ_TIME) { 48 | if (bluetooth.available()) { 49 | str += (char) bluetooth.read(); 50 | } 51 | } 52 | Serial.println(str); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /examples/HM10_rename/HM10_rename.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Renames the HM 10 bluetooth module 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | const unsigned long BAUD_RATE = 9600; 9 | 10 | // PINS 11 | const int BLUETOOTH_TX = 7; 12 | const int BLUETOOTH_RX = 8; 13 | 14 | SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX); 15 | ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor. 16 | 17 | void setup() { 18 | 19 | // Start bluetooth serial 20 | bluetooth.begin(BAUD_RATE); 21 | 22 | delay(1000); 23 | 24 | // rename device to xxxxxx 25 | bluetooth.write("AT+NAMExxxxxx"); 26 | 27 | } 28 | 29 | void loop() { 30 | 31 | } 32 | -------------------------------------------------------------------------------- /examples/builtin_serial/builtin_serial.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Uses built-in hardware serial instead of software serial. 3 | Recommended for Arduino boards with more than one serial ports. 4 | https://www.arduino.cc/reference/en/language/functions/communication/serial/ 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | const unsigned long BAUD_RATE = 9600; 11 | 12 | int prevThrottle = 49; 13 | int prevSteering = 49; 14 | int throttle, steering, sliderVal, button, sliderId; 15 | 16 | // See: https://www.arduino.cc/reference/en/language/functions/communication/serial/ 17 | ArduinoBlue phone(Serial2); 18 | 19 | // Setup code runs once after program starts. 20 | void setup() { 21 | // Start serial communications. 22 | // The baud rate must be the same for both serials. 23 | Serial.begin(BAUD_RATE); 24 | Serial2.begin(BAUD_RATE); 25 | 26 | delay(100); 27 | 28 | Serial.println("setup complete"); 29 | } 30 | 31 | // Put your main code here, to run repeatedly: 32 | void loop() { 33 | // ID of the button pressed pressed. 34 | button = phone.getButton(); 35 | 36 | // Returns the text data sent from the phone. 37 | // After it returns the latest data, empty string "" is sent in subsequent. 38 | // calls until text data is sent again. 39 | String str = phone.getText(); 40 | 41 | // Throttle and steering values go from 0 to 99. 42 | // When throttle and steering values are at 99/2 = 49, the joystick is at center. 43 | throttle = phone.getThrottle(); 44 | steering = phone.getSteering(); 45 | 46 | // ID of the slider moved. 47 | sliderId = phone.getSliderId(); 48 | 49 | // Slider value goes from 0 to 200. 50 | sliderVal = phone.getSliderVal(); 51 | 52 | // Display button data whenever its pressed. 53 | if (button != -1) { 54 | Serial.print("Button: "); 55 | Serial.println(button); 56 | } 57 | 58 | // Display slider data when slider moves 59 | if (sliderId != -1) { 60 | Serial.print("Slider ID: "); 61 | Serial.print(sliderId); 62 | Serial.print("\tValue: "); 63 | Serial.println(sliderVal); 64 | } 65 | 66 | // Display throttle and steering data if steering or throttle value is changed 67 | if (prevThrottle != throttle || prevSteering != steering) { 68 | Serial.print("Throttle: "); Serial.print(throttle); Serial.print("\tSteering: "); Serial.println(steering); 69 | prevThrottle = throttle; 70 | prevSteering = steering; 71 | } 72 | 73 | // If a text from the phone was sent print it to the serial monitor 74 | if (str != "") { 75 | Serial.println(str); 76 | } 77 | 78 | // Send string from serial command line to the phone. This will alert the user. 79 | if (Serial.available()) { 80 | Serial.write("send: "); 81 | String str = Serial.readString(); 82 | phone.sendMessage(str); // phone.sendMessage(str) sends the text to the phone. 83 | Serial.print(str); 84 | Serial.write('\n'); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /examples/drive_robot/drive_robot.ino: -------------------------------------------------------------------------------- 1 | /* 2 | ArduinoBlue-library demo for driving differential drive robot. 3 | Instructions: 4 | 1. Set the pin constants. 5 | 2. Edit the motorSetForward, motorSetBackward, and motorBrake functions as necessary (see comments). 6 | 3. Test the functions from step 2 by calling them and setting the motor speed to max (see doTesting function) 7 | CAUTION: The motor set functions must not set the motor speed. This function simply configures the motor controller. 8 | 4. Uncomment controlDrive function and comment doTesting function in the loop 9 | 5. Connect to app and drive. 10 | */ 11 | 12 | 13 | #include // SoftwareSerial for serial communication with HM10 bluetooth module. 14 | #include // ArduinoBlue bluetooth library 15 | 16 | const unsigned long BAUD_RATE = 9600; 17 | 18 | // MOTOR LEFT PINS 19 | const int ENA = 6; 20 | const int IN1 = 4; 21 | const int IN2 = 5; 22 | // MOTOR RIGHT PINS 23 | const int ENB = 11; 24 | const int IN3 = 12; 25 | const int IN4 = 13; 26 | 27 | // MOTOR PARAMETERS 28 | // Minimum PWM value of analogWrite to spin motor when robot is on the ground. 29 | const int MINIMUM_MOTOR_SPEED = 65; 30 | 31 | // HM10 BLUETOOTH MODULE PINS 32 | // NOTE: Not all pins on your Arduino may support SoftwareSerial. 33 | // Please check: https://www.arduino.cc/en/Reference/softwareSerial 34 | const int BLUETOOTH_TX = 7; 35 | const int BLUETOOTH_RX = 8; 36 | 37 | SoftwareSerial softSerial(BLUETOOTH_TX, BLUETOOTH_RX); // Object for serial communication to HM 10 bluetooth module using ditigal pins. 38 | ArduinoBlue phone(softSerial); // Object for smartphone robot control. 39 | 40 | // Configures the motor controller to stop the motors. 41 | void motorBrake() { 42 | digitalWrite(ENA, LOW); 43 | digitalWrite(ENB, LOW); 44 | digitalWrite(IN1, LOW); 45 | digitalWrite(IN2, LOW); 46 | digitalWrite(IN3, LOW); 47 | digitalWrite(IN4, LOW); 48 | digitalWrite(ENA, HIGH); 49 | digitalWrite(ENB, HIGH); 50 | // Do not write the motor speeds on this function. It simply configures the motor controller. 51 | } 52 | 53 | // Configures the motor controller to have the robot move forward. 54 | void motorSetForward() { 55 | digitalWrite(IN1, LOW); 56 | digitalWrite(IN2, HIGH); 57 | digitalWrite(IN3, LOW); 58 | digitalWrite(IN4, HIGH); 59 | // Do not write the motor speeds on this function. It simply configures the motor controller. 60 | } 61 | 62 | // Configures the motor controller to have the robot move backwards. 63 | void motorSetBackward() { 64 | digitalWrite(IN1, HIGH); 65 | digitalWrite(IN2, LOW); 66 | digitalWrite(IN3, HIGH); 67 | digitalWrite(IN4, LOW); 68 | // Do not write the motor speeds on this function. It simply configures the motor controller. 69 | } 70 | 71 | void setPins() { 72 | // Set pins as input or output 73 | pinMode(ENA, OUTPUT); 74 | pinMode(IN1, OUTPUT); 75 | pinMode(IN2, OUTPUT); 76 | pinMode(ENB, OUTPUT); 77 | pinMode(IN3, OUTPUT); 78 | pinMode(IN4, OUTPUT); 79 | } 80 | 81 | void controlDrive() { 82 | // THROTTLE AND STEERING CONTROL 83 | // throttle values after subtracting 49: 84 | // 50 = max forward throttle 85 | // 0 = no throttole 86 | // -49 = max reverse throttle 87 | // steering values after subtracting 49: 88 | // 50 = max right 89 | // 0 = straight 90 | // -49 = max left 91 | int throttle = phone.getThrottle() - 49; 92 | int steering = phone.getSteering() - 49; 93 | 94 | if (throttle == 0) { 95 | // If throttle is zero, don't move. 96 | motorBrake(); 97 | return; 98 | } 99 | 100 | // Determine forwards or backwards. 101 | if (throttle > 0) { 102 | // Forward 103 | motorSetForward(); 104 | } 105 | else { 106 | // Backward 107 | motorSetBackward(); 108 | } 109 | 110 | // Map throttle to PWM range. 111 | int mappedSpeed = map(abs(throttle), 0, 50, MINIMUM_MOTOR_SPEED, 255); 112 | // Map steering to PWM range. 113 | int reducedSpeed = map(abs(steering), 0, 50, mappedSpeed, MINIMUM_MOTOR_SPEED); 114 | 115 | int leftMotorSpeed, rightMotorSpeed; 116 | if (steering > 0) { 117 | // Turn Right: reduce right motor speed 118 | leftMotorSpeed = mappedSpeed; 119 | rightMotorSpeed = reducedSpeed; 120 | } 121 | else { 122 | // Turn Left: reduce left motor speed 123 | leftMotorSpeed = reducedSpeed; 124 | rightMotorSpeed = mappedSpeed; 125 | } 126 | 127 | // Set motor speeds 128 | analogWrite(ENA, leftMotorSpeed); 129 | analogWrite(ENB, rightMotorSpeed); 130 | 131 | // Print Debug Info 132 | Serial.print("throttle: "); Serial.print(throttle); 133 | Serial.print("\tsteering: "); Serial.print(steering); 134 | Serial.print("\tmappedSpeed: "); Serial.print(mappedSpeed); 135 | Serial.print("\treducedSpeed: "); Serial.print(reducedSpeed); 136 | Serial.print("\tleftMotorSpeed: "); Serial.print(leftMotorSpeed); 137 | Serial.print("\trightMotorSpeed: "); Serial.println(rightMotorSpeed); 138 | } 139 | 140 | void doTesting() { 141 | // Uncomment to test. 142 | // motorSetForward(); 143 | // motorSetBackward(); 144 | // motorBrake(); 145 | 146 | // Set motor speeds to max 147 | analogWrite(ENA, 255); 148 | analogWrite(ENB, 255); 149 | } 150 | 151 | void setup() { 152 | delay(500); 153 | // Start serial communications. 154 | // The baud rate must be the same for both serials. 155 | // Start communication with HM10 bluetooth module. 156 | softSerial.begin(BAUD_RATE); 157 | 158 | // Begin serial communication with computer. 159 | Serial.begin(BAUD_RATE); 160 | 161 | // Set pin modes 162 | setPins(); 163 | 164 | Serial.println("SETUP COMPLETE"); 165 | } 166 | 167 | void loop() { 168 | // Refer to comment on top for step by step instructions. 169 | doTesting(); // Comment this after testing. 170 | // controlDrive(); // Uncomment this after testing. 171 | } 172 | -------------------------------------------------------------------------------- /examples/multiple_LED_brightness/multiple_LED_brightness.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * ArduinoBlue example code for controling red, green, and blue LEDs. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | const unsigned long BAUD_RATE = 9600; 9 | 10 | const int RED_PIN = 6; 11 | const int GREEN_PIN = 5; 12 | const int BLUE_PIN = 3; 13 | 14 | // The bluetooth tx and rx pins must be supported by software serial. 15 | // Visit https://www.arduino.cc/en/Reference/SoftwareSerial for unsupported pins. 16 | // Bluetooth TX -> Arduino D8 17 | const int BLUETOOTH_TX_PIN = 7; 18 | const int BLUETOOTH_RX_PIN = 8; 19 | 20 | SoftwareSerial bluetooth(BLUETOOTH_TX_PIN, BLUETOOTH_RX_PIN); 21 | ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoCommander. 22 | 23 | int button, sliderId, sliderVal; 24 | int redVal = 255; 25 | int greenVal = 255; 26 | int blueVal = 255; 27 | bool redOn, greenOn, blueOn; 28 | 29 | void setLED(int pin, bool isOn, int value) { 30 | if (isOn) { 31 | analogWrite(pin, value); 32 | } 33 | else { 34 | analogWrite(pin, 0); 35 | } 36 | } 37 | 38 | void setup() { 39 | // Start serial communciations. 40 | // The baud rate must be the same for both serials. 41 | Serial.begin(BAUD_RATE); 42 | bluetooth.begin(BAUD_RATE); 43 | 44 | // Declare LED pins as output pins 45 | pinMode(RED_PIN, OUTPUT); 46 | pinMode(GREEN_PIN, OUTPUT); 47 | pinMode(BLUE_PIN, OUTPUT); 48 | 49 | Serial.println("setup complete"); 50 | } 51 | 52 | void loop() { 53 | button = phone.getButton(); 54 | sliderId = phone.getSliderId(); 55 | sliderVal = phone.getSliderVal(); 56 | 57 | // Change the brightness value of LED based on slider 58 | // Slider values go from 0 to 200 59 | if (sliderId == 0) { 60 | redVal = map(sliderVal, 0, 200, 0, 255); 61 | } 62 | else if (sliderId == 1) { 63 | greenVal = map(sliderVal, 0, 200, 0, 255); 64 | } 65 | else if (sliderId == 2) { 66 | blueVal = map(sliderVal, 0, 200, 0, 255); 67 | } 68 | 69 | // Set the LEDs on/off based on button press 70 | if (button == 0) { 71 | redOn = !redOn; 72 | } 73 | else if (button == 1) { 74 | greenOn = !greenOn; 75 | } 76 | else if (button == 2) { 77 | blueOn = !blueOn; 78 | } 79 | 80 | setLED(RED_PIN, redOn, redVal); 81 | setLED(GREEN_PIN, greenOn, greenVal); 82 | setLED(BLUE_PIN, blueOn, blueVal); 83 | } 84 | -------------------------------------------------------------------------------- /examples/multiple_LED_switch/multiple_LED_switch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Demo program for controling red, green, and blue LED. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | const unsigned long BAUD_RATE = 9600; 9 | 10 | const int RED_PIN = 6; 11 | const int GREEN_PIN = 5; 12 | const int BLUE_PIN = 3; 13 | 14 | // The bluetooth tx and rx pins must be supported by software serial. 15 | // Visit https://www.arduino.cc/en/Reference/SoftwareSerial for unsupported pins. 16 | // Bluetooth TX -> Arduino D8 17 | // Bluetooth RX -> Arduino D7 18 | const int BLUETOOTH_TX_PIN = 7; 19 | const int BLUETOOTH_RX_PIN = 8; 20 | 21 | SoftwareSerial bluetooth(BLUETOOTH_TX_PIN, BLUETOOTH_RX_PIN); 22 | ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor. 23 | 24 | int button; 25 | bool redOn; 26 | bool greenOn; 27 | bool blueOn; 28 | 29 | void setup() { 30 | // Start serial communications. 31 | // The baud rate must be the same for both serials. 32 | Serial.begin(BAUD_RATE); 33 | bluetooth.begin(BAUD_RATE); 34 | 35 | // delay just in case bluetooth module needs time to "get ready". 36 | delay(100); 37 | 38 | // Declare LED pins as output pins 39 | pinMode(RED_PIN, OUTPUT); 40 | pinMode(GREEN_PIN, OUTPUT); 41 | pinMode(BLUE_PIN, OUTPUT); 42 | 43 | Serial.println("setup complete"); 44 | } 45 | 46 | void loop() { 47 | button = phone.getButton(); 48 | 49 | if (button == 0) { 50 | if (redOn) { 51 | digitalWrite(RED_PIN, LOW); 52 | redOn = false; 53 | } 54 | else { 55 | digitalWrite(RED_PIN, HIGH); 56 | redOn = true; 57 | } 58 | } 59 | else if (button == 1) { 60 | if (greenOn) { 61 | digitalWrite(GREEN_PIN, LOW); 62 | greenOn = false; 63 | } 64 | else { 65 | digitalWrite(GREEN_PIN, HIGH); 66 | greenOn = true; 67 | } 68 | } 69 | else if (button == 2) { 70 | if (blueOn) { 71 | digitalWrite(BLUE_PIN, LOW); 72 | blueOn = false; 73 | } 74 | else { 75 | digitalWrite(BLUE_PIN, HIGH); 76 | blueOn = true; 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /examples/single_LED_brightness/single_LED_brightness.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * ArduinoBlue-library example code to control the brightness of an LED using a slider. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | const unsigned long BAUD_RATE = 9600; 9 | 10 | // The bluetooth tx and rx pins must be supported by software serial. 11 | // Visit https://www.arduino.cc/en/Reference/SoftwareSerial for unsupported pins. 12 | // Bluetooth TX -> Arduino D8 13 | const int BLUETOOTH_TX = 7; 14 | // Bluetooth RX -> Arduino D7 15 | const int BLUETOOTH_RX = 8; 16 | 17 | // LED (+) -> Arduino D9 18 | const int LED_PIN = 9; 19 | 20 | SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX); 21 | ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor. 22 | 23 | bool isLedOn = false; 24 | 25 | void setup() { 26 | // Start serial communications. 27 | // The baud rate must be the same for both serials. 28 | Serial.begin(BAUD_RATE); 29 | bluetooth.begin(BAUD_RATE); 30 | 31 | // delay just in case bluetooth module needs time to "get ready". 32 | delay(100); 33 | 34 | Serial.println("setup complete"); 35 | 36 | // Declare LED pin as an output pin. 37 | pinMode(LED_PIN, OUTPUT); 38 | } 39 | 40 | void loop() { 41 | // put your main code here, to run repeatedly: 42 | 43 | // Get slider ID and it's value 44 | int sliderId = phone.getSliderId(); 45 | int sliderVal = phone.getSliderVal(); 46 | 47 | // Get the button ID 48 | int buttionId = phone.getButton(); 49 | 50 | // The sliderId will not be -1 if a slider has been touched. 51 | if (sliderId != -1) { 52 | // Adjust LED brightness. 53 | int writeVal = map(sliderVal, 0, 200, 0, 255); 54 | analogWrite(LED_PIN, writeVal); 55 | Serial.print("ID: "); Serial.print(sliderId); 56 | Serial.print("\tWriteVal: "); Serial.print(writeVal); 57 | Serial.print("\tVal: "); Serial.println(sliderVal); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ArduinoBlue 2 | version=3.1.2 3 | author=Jae An 4 | maintainer=Rowan Nadon 5 | sentence=Interface your Arduino using my mobile app using bluetooth low energy. 6 | paragraph=This library works with the ArduinoBlue app available for Android and iOS. This library works with the HM 10 bluetooth module. 7 | category=Device Control 8 | url=https://sites.google.com/stonybrook.edu/arduinoble/ 9 | includes=ArduinoBlue.h 10 | -------------------------------------------------------------------------------- /src/ArduinoBlue.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author: Jae An and Rowan Nadon 3 | */ 4 | 5 | #include "ArduinoBlue.h" 6 | #include 7 | 8 | 9 | typedef union { 10 | float number; 11 | uint8_t bytes[4]; 12 | } FLOATUNION_t; 13 | 14 | ArduinoBlue::ArduinoBlue(Stream &output) : 15 | _bluetooth(output) 16 | { 17 | } 18 | 19 | // Checks for incoming bluetooth signal 20 | bool ArduinoBlue::checkBluetooth() { 21 | 22 | bool isDataRead = _bluetooth.available() > 0; 23 | 24 | while (_bluetooth.available() > 0) { 25 | uint8_t intRead = _bluetooth.read(); 26 | 27 | // Check for transmission starting 28 | // If a new transmission starts process the transmission 29 | if (intRead == DRIVE_TRANSMISSION) { 30 | processDriveTransmission(); 31 | } 32 | else if (intRead == BUTTON_TRANSMISSION) { 33 | processButtonTransmission(); 34 | } 35 | else if (intRead == SLIDER_TRANSMISSION) { 36 | processSliderTransmission(); 37 | } 38 | else if (intRead == TEXT_TRANSMISSION) { 39 | processTextTransmission(); 40 | } 41 | else if (intRead == PATH_TRANSMISSION) { 42 | processPathTransmission(); 43 | } 44 | else if (intRead == CONNECTION_CHECK) { 45 | _bluetooth.print(CONNECTION_CHECK); 46 | } 47 | } 48 | 49 | return isDataRead; 50 | } 51 | 52 | // Process incoming throttle and steering data. 53 | void ArduinoBlue::processDriveTransmission() { 54 | // Store the incoming throttle and steering values in the signal array. 55 | storeShortTransmission(); 56 | 57 | // Set the values from the signal array. 58 | _throttle = _signal[0]; 59 | _steering = _signal[1]; 60 | 61 | clearSignalArray(); 62 | } 63 | 64 | // Process incoming button data. 65 | void ArduinoBlue::processButtonTransmission() { 66 | // Store the button transmission in the signal array. 67 | storeShortTransmission(); 68 | 69 | // Set the values from the signal array. 70 | _button = _signal[0]; 71 | clearSignalArray(); 72 | } 73 | 74 | // Process incoming slider data. 75 | void ArduinoBlue::processSliderTransmission() { 76 | // Store the slider transmission in the signal array. 77 | storeShortTransmission(); 78 | 79 | // Set the values from the signal array. 80 | _sliderId = _signal[0]; 81 | _sliderVal = _signal[1]; 82 | 83 | clearSignalArray(); 84 | } 85 | 86 | // Process incoming text data. 87 | void ArduinoBlue::processTextTransmission() { 88 | // Get the text transmission. 89 | _text = readString(); 90 | clearSignalArray(); 91 | } 92 | 93 | // TODO: Implement this. 94 | void ArduinoBlue::processPathTransmission() { 95 | // 96 | detachInterrupts(); 97 | _pathAvailable = storePathTransmission(); 98 | _bluetooth.print((char)PATH_TRANSMISSION_CONFIRMATION); 99 | _bluetooth.flush(); 100 | 101 | clearSignalArray(); 102 | attachInterrupts(); 103 | } 104 | 105 | // Send location data back to ArduinoBlue 106 | void ArduinoBlue::sendLocation(float xPos, float yPos, float headingAngle, float xGoal, float yGoal) { 107 | _bluetooth.print((char)LOCATION_TRANSMISSION_START); 108 | sendFloatAsBytes((float)xPos); 109 | sendFloatAsBytes((float)yPos); 110 | sendFloatAsBytes((float)headingAngle); 111 | sendFloatAsBytes((float)xGoal); 112 | sendFloatAsBytes((float)yGoal); 113 | } 114 | 115 | // TODO: May not be needed. 116 | void ArduinoBlue::sendFloatAsBytes(float num) { 117 | FLOATUNION_t floatUnion; 118 | floatUnion.number = num; 119 | _bluetooth.print((char)floatUnion.bytes[0]); 120 | _bluetooth.print((char)floatUnion.bytes[1]); 121 | _bluetooth.print((char)floatUnion.bytes[2]); 122 | _bluetooth.print((char)floatUnion.bytes[3]); 123 | } 124 | 125 | // Convert four bytes to float value. 126 | float ArduinoBlue::bytesToFloat(uint8_t u1, uint8_t u2, uint8_t u3, uint8_t u4) { 127 | FLOATUNION_t floatUntion; 128 | floatUntion.bytes[0] = u1; 129 | floatUntion.bytes[1] = u2; 130 | floatUntion.bytes[2] = u3; 131 | floatUntion.bytes[3] = u4; 132 | return floatUntion.number; 133 | } 134 | 135 | // Read and store the path transmission. 136 | bool ArduinoBlue::storePathTransmission() { 137 | const int BYTES_PER_FLOAT = 4; 138 | 139 | // Delete the previously stored path if available. 140 | delete[] _pathX; 141 | delete[] _pathY; 142 | 143 | unsigned long prevMillis = millis(); 144 | uint8_t intRead; 145 | int bytesReadIteration = 0; 146 | int numbersRead = 0; 147 | uint8_t ary[BYTES_PER_FLOAT]; 148 | _pathLength = 9999; // Get in the loop 149 | 150 | // Read the float values corresponding to the path data. 151 | while (numbersRead <= _pathLength * 2) { 152 | if (_bluetooth.available()) { 153 | 154 | intRead = _bluetooth.read(); 155 | 156 | ary[bytesReadIteration] = intRead; 157 | bytesReadIteration++; 158 | 159 | if (bytesReadIteration == BYTES_PER_FLOAT) { 160 | // Convert the byte array to float array. 161 | float number = bytesToFloat(ary[0], ary[1], ary[2], ary[3]); 162 | numbersRead++; 163 | 164 | // The first path data transmitted is the length. 165 | if (numbersRead == 1) { 166 | _pathLength = (int)number; 167 | _pathX = new float[_pathLength]; 168 | _pathY = new float[_pathLength]; 169 | // Serial.print("Path length: "); Serial.println(_pathLength); 170 | } 171 | // Subsequent path data transmission are the coordinates. 172 | else { 173 | int index = numbersRead / 2 - 1; 174 | if (fabs(number) > PATH_OVERFLOW_VALUE) { 175 | // If any value has overflowed and is invalid use the previous coordinate. 176 | // Overflowed means that it's junk data. Throw it away. 177 | // TODO: Consider the case when the index is 0. 178 | _pathX[index] = _pathX[index - 1]; 179 | _pathY[index] = _pathY[index - 1]; 180 | } 181 | else { 182 | if (numbersRead % 2 == 0) { 183 | _pathX[index] = number; 184 | // Serial.print(number); 185 | } 186 | else { 187 | _pathY[index] = number; 188 | // Serial.print("\t"); Serial.println(number); 189 | } 190 | } 191 | //Serial.print("Number Read: "); Serial.println(numberReadCount); 192 | } 193 | bytesReadIteration = 0; 194 | } 195 | } 196 | if (millis() - prevMillis > PATH_TRANSMISSION_TIMEOUT) { 197 | //Serial.println("Error: path transmission took too long."); 198 | //return false; 199 | } 200 | } 201 | return true; 202 | } 203 | 204 | // Stores short transmission into the signal array 205 | void ArduinoBlue::storeShortTransmission() { 206 | unsigned long prevMillis = millis(); 207 | uint8_t intRead; 208 | while (millis() - prevMillis < SHORT_TRANSMISSION_TIMEOUT) { 209 | if (_bluetooth.available()) { 210 | intRead = _bluetooth.read(); 211 | if (intRead == TRANSMISSION_END) break; 212 | pushToSignalArray(intRead); 213 | } 214 | } 215 | } 216 | 217 | // Read and store incoming characters until TRANMISSION_END character is read or until TEXT_TRANMISSION_TIMEOUT. 218 | // Return the characters read. 219 | String ArduinoBlue::readString() { 220 | String s; 221 | uint8_t intRead; 222 | unsigned long prevTime = millis(); 223 | 224 | // Read until newline character or timeout is reached 225 | prevTime = millis(); 226 | while (millis() - prevTime < TEXT_TRANSMISSION_TIMEOUT) { 227 | if (_bluetooth.available()) { 228 | intRead = _bluetooth.read(); 229 | // break the loop if end of transmission 230 | if (intRead == TRANSMISSION_END) break; 231 | s.concat((char)intRead); 232 | } 233 | } 234 | return s; 235 | } 236 | 237 | void ArduinoBlue::pushToSignalArray(uint8_t elem) { 238 | if (elem < 0) { 239 | // Serial.print("neg"); 240 | } 241 | if ( !(_signalLength + 1 == MAX_SHORT_SIGNAL_LENGTH) ) { 242 | _signal[_signalLength] = elem; 243 | _signalLength++; 244 | } 245 | else { 246 | // Serial.println("ArduinoBlue: Transmission error..."); 247 | // Serial.print("elem: "); Serial.println(elem); 248 | } 249 | } 250 | 251 | void ArduinoBlue::clearSignalArray() { 252 | for (uint8_t i = 0; i < _signalLength; i++) { 253 | _signal[i] = DEFAULT_VALUE; 254 | } 255 | _signalLength = 0; 256 | } 257 | 258 | // Returns true if path data is available for use. 259 | bool ArduinoBlue::isPathAvailable() { 260 | bool toReturn = _pathAvailable; 261 | _pathAvailable = false; 262 | return toReturn; 263 | } 264 | 265 | // Returns the ID of the button pressed. 266 | // Returns -1 if a button has not been pressed. 267 | int ArduinoBlue::getButton() { 268 | checkBluetooth(); 269 | uint8_t btn = _button; 270 | _button = DEFAULT_VALUE; 271 | if (btn == DEFAULT_VALUE) return -1; 272 | return btn; 273 | } 274 | 275 | // Returns the slider ID of the slider touched. 276 | // Returns -1 if no slider was touched. 277 | int ArduinoBlue::getSliderId() { 278 | checkBluetooth(); 279 | uint8_t id = _sliderId; 280 | _sliderId = DEFAULT_VALUE; 281 | if (id == DEFAULT_VALUE) return -1; 282 | return id; 283 | } 284 | 285 | // Returns the value of the latest slider touched. 286 | int ArduinoBlue::getSliderVal() { 287 | uint8_t val = _sliderVal; 288 | _sliderVal = DEFAULT_VALUE; 289 | if (val == DEFAULT_VALUE) return -1; 290 | return val; 291 | } 292 | 293 | // Returns the throttle value of the joystick or tilt. 294 | int ArduinoBlue::getThrottle() { 295 | checkBluetooth(); 296 | return _throttle; 297 | } 298 | 299 | // Returns the steering value of the joystick or tilt. 300 | int ArduinoBlue::getSteering() { 301 | checkBluetooth(); 302 | return _steering; 303 | } 304 | 305 | // Returns the pointer to the x coordinates of path data. 306 | float * ArduinoBlue::getPathArrayX() { 307 | return _pathX; 308 | } 309 | 310 | // Returns the pointer to the y coordinates of path data. 311 | float * ArduinoBlue::getPathArrayY() { 312 | return _pathY; 313 | } 314 | 315 | // Returns the length of the path data. 316 | int ArduinoBlue::getPathLength() { 317 | return _pathLength; 318 | //return tempPathLength; 319 | } 320 | 321 | // Send text to ArduinoBlue to show in the terminal component 322 | void ArduinoBlue::sendText(String msg) { 323 | _bluetooth.print(((char)TEXT_SEND_TRANSMISSION) + msg + ((char)TRANSMISSION_END)); 324 | _bluetooth.flush(); 325 | } 326 | 327 | // Send text to ArduinoBlue to show in a display component 328 | void ArduinoBlue::sendDisplayData(uint8_t id, String msg) { 329 | _bluetooth.print(((char)DISPLAY_SEND_TRANSMISSION) + (((char)id) + msg) + ((char)TRANSMISSION_END)); 330 | _bluetooth.flush(); 331 | } 332 | 333 | // for backwards compatibility 334 | void ArduinoBlue::sendMessage(String msg) { 335 | sendText(msg); 336 | } 337 | 338 | 339 | bool ArduinoBlue::isConnected() { 340 | _bluetooth.print(CONNECTION_CHECK); 341 | // wait for 500 ms 342 | delay(500); 343 | if (_bluetooth.available()) { 344 | return _bluetooth.read() == CONNECTION_CHECK; 345 | } 346 | return false; 347 | } 348 | 349 | // Returns the text that the user sent through the app. 350 | String ArduinoBlue::getText() { 351 | checkBluetooth(); 352 | String ret = _text; 353 | _text = ""; 354 | return ret; 355 | } 356 | 357 | // Sets pointer to the functions to detach and attach interrupts. 358 | // This is done so that interrupts will be disabled when path data is being read. 359 | // See storePathTransmission(). 360 | void ArduinoBlue::setInterruptToggle(functiontype attach, functiontype detach) 361 | { 362 | _attachInterrupts = attach; 363 | _detachInterrupts = detach; 364 | } 365 | 366 | // Attach interrupts 367 | // See setInterruptToggle() 368 | void ArduinoBlue::attachInterrupts() 369 | { 370 | if (_attachInterrupts != nullptr) 371 | { 372 | _attachInterrupts(); 373 | } 374 | } 375 | 376 | // Detach interrupts 377 | // See setInterruptToggle() 378 | void ArduinoBlue::detachInterrupts() 379 | { 380 | if (_detachInterrupts != nullptr) 381 | { 382 | _detachInterrupts(); 383 | } 384 | } 385 | 386 | // bool ArduinoBlue::isConnected() { 387 | // _bluetooth.print(CONNECTION_CHECK); 388 | // // wait for 500 ms 389 | // delay(500); 390 | // if (_bluetooth.available()) { 391 | // return _bluetooth.read() == CONNECTION_CHECK; 392 | // } 393 | // return false; 394 | // } 395 | -------------------------------------------------------------------------------- /src/ArduinoBlue.h: -------------------------------------------------------------------------------- 1 | /* 2 | Author: Jae An 3 | */ 4 | 5 | #ifndef ArduinoBlue_h 6 | #define ArduinoBlue_h 7 | 8 | #include "FunctionType.h" 9 | #include 10 | 11 | // Denotes start of path transmission from mobile app. 12 | #define PATH_TRANSMISSION 244 13 | // Denotes that the path transmission was received successfully by the Arduino. (Sent from Arduino). 14 | #define PATH_TRANSMISSION_CONFIRMATION 245 15 | 16 | // Denotes start of location transmission from Arduino 17 | #define LOCATION_TRANSMISSION_START 246 18 | #define DELIMETER 247 19 | #define TEXT_SEND_TRANSMISSION 248 20 | #define CONNECTION_CHECK 249 21 | #define DRIVE_TRANSMISSION 251 22 | 23 | // Denotes start of button transmission from mobile app. 24 | #define BUTTON_TRANSMISSION 252 25 | 26 | // Denotes start of slider transmission from mobile app. 27 | #define SLIDER_TRANSMISSION 253 28 | 29 | // Denotes start of text transmission from mobile app. 30 | #define TEXT_TRANSMISSION 254 31 | #define DISPLAY_SEND_TRANSMISSION 243 32 | 33 | // Denotes that the text, button, or slider transmission has been received successfully by the Arduino. (Sent from Arduino). 34 | #define TRANSMISSION_END 250 35 | 36 | // Default value for signal array elements. 37 | #define DEFAULT_VALUE 255 38 | 39 | #define TEXT_TRANSMISSION_TIMEOUT 5000 // ms 40 | #define SHORT_TRANSMISSION_TIMEOUT 500 41 | #define PATH_TRANSMISSION_TIMEOUT 10000 42 | 43 | #define PATH_OVERFLOW_VALUE 1000000 44 | 45 | const uint8_t DEFAULT_STEERING = 50; 46 | const uint8_t DEFAULT_THROTTLE = 50; 47 | const uint8_t MAX_SHORT_SIGNAL_LENGTH = 3; 48 | 49 | 50 | class ArduinoBlue 51 | { 52 | public: 53 | ArduinoBlue(Stream &output); 54 | int getButton(); 55 | int getSliderId(); 56 | int getSliderVal(); 57 | int getThrottle(); 58 | int getSteering(); 59 | float * getPathArrayX(); 60 | float * getPathArrayY(); 61 | float getPathY(float); 62 | int getPathLength(); 63 | bool checkBluetooth(); 64 | bool isConnected(); 65 | bool isPathAvailable(); 66 | void sendText(String msg); 67 | void sendMessage(String msg); 68 | void sendDisplayData(uint8_t id, String data); 69 | void sendLocation(float, float, float, float, float); 70 | static float bytesToFloat(uint8_t u1, uint8_t u2, uint8_t u3, uint8_t u4); 71 | String getText(); 72 | void setInterruptToggle(functiontype attach, functiontype detach); 73 | private: 74 | Stream & _bluetooth; 75 | uint8_t _signal[MAX_SHORT_SIGNAL_LENGTH]; 76 | uint8_t _signalLength = 0; 77 | uint8_t _throttle = DEFAULT_STEERING; 78 | uint8_t _steering = DEFAULT_THROTTLE; 79 | uint8_t _sliderVal = DEFAULT_VALUE; 80 | uint8_t _sliderId = DEFAULT_VALUE; 81 | uint8_t _button = DEFAULT_VALUE; 82 | bool _pathAvailable = false; 83 | String _text; 84 | float * _pathX; 85 | float * _pathY; 86 | float _prevReturnXx; 87 | int _pathLength; 88 | void clearSignalArray(); 89 | void pushToSignalArray(uint8_t elem); 90 | void storeShortTransmission(); 91 | bool storePathTransmission(); 92 | void processDriveTransmission(); 93 | void processButtonTransmission(); 94 | void processSliderTransmission(); 95 | void processTextTransmission(); 96 | void processPathTransmission(); 97 | void sendFloatAsBytes(float); 98 | void attachInterrupts(); 99 | void detachInterrupts(); 100 | String readString(); 101 | functiontype _attachInterrupts = nullptr; 102 | functiontype _detachInterrupts = nullptr; 103 | }; 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /src/FunctionType.h: -------------------------------------------------------------------------------- 1 | #ifndef FunctionType_h 2 | #define FunctionType_h 3 | 4 | typedef void(*functiontype)(); 5 | typedef void(*functiontypeint)(int); 6 | 7 | 8 | #endif 9 | --------------------------------------------------------------------------------