Created
June 17, 2016 06:15
-
-
Save monpetit/2dc1f7ae7fdcae7931907d6f0f0739a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Copyright (c) 2016 Ergo Inventus Inc. | |
| Copyright (c) 2012-2014 RedBearLab | |
| Permission is hereby granted, free of charge, | |
| to any person obtaining a copy of this software | |
| and associated documentation files (the "Software"), | |
| to deal in the Software without restriction, | |
| including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, | |
| and/or sell copies of the Software, | |
| and to permit persons to whom the Software is furnished | |
| to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice | |
| shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | |
| THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE | |
| AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS | |
| OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
| DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
| CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | |
| IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
| OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| /* | |
| * Chat | |
| * | |
| * Simple chat sketch, work with the Chat iOS/Android App. | |
| * Type something from the Arduino serial monitor to send | |
| * to the Chat App or vice verse. | |
| * | |
| */ | |
| //"RBL_nRF8001.h/spi.h/boards.h" is needed in every new project | |
| #include <SPI.h> | |
| #include <EEPROM.h> | |
| #include <boards.h> | |
| #include <BlendMicro.h> | |
| uint32_t tick, last_tick = 0; | |
| uint32_t count = 0; | |
| char txbuffer[20]; | |
| const int LED_B = 3; | |
| const int LED_G = 5; | |
| const int LED_R = 8; | |
| void ble_write_string(const char* buffer, size_t length) | |
| { | |
| while (length--) { | |
| if (*buffer) { | |
| ble_write(*buffer); | |
| buffer++; | |
| } | |
| else | |
| return; | |
| } | |
| } | |
| void ble_write_string(const char* buffer) | |
| { | |
| ble_write_string(buffer, strlen(buffer)); | |
| } | |
| void send_voltage(void) | |
| { | |
| int adc_result = analogRead(A0); | |
| float voltage = ((adc_result + 1) * 2) * 2.56 / 1024; | |
| String outbuffer = String("[VBAT] "); | |
| outbuffer += String(voltage, 2); | |
| outbuffer += " V"; | |
| ble_write_string(outbuffer.c_str(), outbuffer.length()); | |
| } | |
| void led_green_on(void) | |
| { | |
| digitalWrite(LED_G, LOW); | |
| digitalWrite(LED_B, HIGH); | |
| digitalWrite(LED_R, HIGH); | |
| } | |
| void led_blue_on(void) | |
| { | |
| digitalWrite(LED_G, HIGH); | |
| digitalWrite(LED_B, LOW); | |
| digitalWrite(LED_R, HIGH); | |
| } | |
| void led_red_on(void) | |
| { | |
| digitalWrite(LED_G, HIGH); | |
| digitalWrite(LED_B, HIGH); | |
| digitalWrite(LED_R, LOW); | |
| } | |
| void led_all_off(void) | |
| { | |
| digitalWrite(LED_G, HIGH); | |
| digitalWrite(LED_B, HIGH); | |
| digitalWrite(LED_R, HIGH); | |
| } | |
| void init_leds(void) | |
| { | |
| pinMode(LED_G, OUTPUT); | |
| pinMode(LED_B, OUTPUT); | |
| pinMode(LED_R, OUTPUT); | |
| led_all_off(); | |
| } | |
| void setup() | |
| { | |
| // Default pins set to 9 and 8 for REQN and RDYN | |
| // Set your REQN and RDYN here before ble_begin() if you need | |
| //ble_set_pins(3, 2); | |
| // Set your BLE Shield name here, max. length 10 | |
| //ble_set_name("My Name"); | |
| init_leds(); | |
| led_red_on(); | |
| analogReference(INTERNAL); | |
| // Init. and start BLE library. | |
| ble_begin(); | |
| // Enable serial debug | |
| Serial.begin(57600); | |
| Serial1.begin(9600); | |
| last_tick = millis(); | |
| } | |
| void loop() | |
| { | |
| if (ble_available()) { | |
| while (ble_available()) { | |
| char ch = ble_read(); | |
| Serial.write(ch); | |
| Serial1.write(ch); | |
| if (ch == 'R') { | |
| led_red_on(); | |
| } | |
| else if (ch == 'G') { | |
| led_green_on(); | |
| } | |
| else if (ch == 'B') { | |
| led_blue_on(); | |
| } | |
| else if (ch == 'O') { | |
| led_all_off(); | |
| } | |
| } | |
| Serial.println(); | |
| Serial1.println(); | |
| } | |
| if (Serial.available()) { | |
| delay(5); | |
| while (Serial.available()) | |
| ble_write(Serial.read()); | |
| } | |
| tick = millis(); | |
| if ((tick - last_tick) >= 2000) { | |
| last_tick = tick; | |
| send_voltage(); | |
| } | |
| ble_do_events(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment