hi,
i have code below , more need. i'm new c/c++, arduino , electronics
anyway, these problems:
- code length; can remove?
- setup runs open serial monitor. want code start when battery powers device.
i have code below , more need. i'm new c/c++, arduino , electronics
anyway, these problems:
- code length; can remove?
- setup runs open serial monitor. want code start when battery powers device.
code: [select]
#include <arduino.h>
#include <wire.h>
#include <spi.h>
#include <adafruit_sensor.h>
#include <adafruit_bme280.h>
#if not defined (_variant_arduino_due_x_) && not defined (_variant_arduino_zero_)
#include <softwareserial.h>
#endif
#include <adafruit_neopixel.h>
#ifdef __avr__
#include <avr/power.h>
#endif
#include "adafruit_ble.h"
#include "adafruit_bluefruitle_spi.h"
#include "adafruit_bluefruitle_uart.h"
#include "bluefruitconfig.h"
#define sealevelpressure_hpa (1013.25)
#define pin 6
#define numpixels 4
#define factoryreset_enable 0
#define minimum_firmware_version "0.6.6"
#define mode_led_behaviour "mode"
int initialstart = 0;
/*=========================================================================*/
// create bluefruit object
adafruit_bluefruitle_uart ble(serial1, -1);
// create bme object
adafruit_bme280 bme; // i2c
// create neopixels
adafruit_neopixel pixels = adafruit_neopixel(numpixels, pin, neo_grb + neo_khz800);
// small helper
void error(const __flashstringhelper*err) {
serial.println(err);
while (1);
}
/**************************************************************************/
/*!
@brief sets hw ble module (this function called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
while (!serial); // required flora & micro
delay(500);
serial.begin(115200);
serial.println(f("checking bme280 sensor"));
if (!bme.begin()) {
serial.println("could not find valid bme280 sensor, check wiring!");
while (1);
}
serial.println(f("checking bluefruit command mode"));
if ( !ble.begin(verbose_mode) ){
error(f("couldn't find bluefruit, make sure it's in command mode & check wiring?"));
}
//serial.println(f("checking neopixels"));
pixels.begin();
pixels.setpixelcolor(0, pixels.color(255,255,255));
pixels.setpixelcolor(1, pixels.color(255,255,255));
pixels.setpixelcolor(2, pixels.color(255,255,255));
pixels.setpixelcolor(3, pixels.color(255,255,255));
pixels.show();
if ( factoryreset_enable )
{
/* perform factory reset make sure in known state */
serial.println(f("performing factory reset: "));
if ( ! ble.factoryreset() ){
error(f("couldn't factory reset"));
}
}
/* disable command echo bluefruit */
ble.echo(false);
//serial.println("requesting bluefruit info:");
/* print bluefruit information */
//ble.info();
serial.println(f("please use adafruit bluefruit le app connect in uart mode"));
serial.println(f("then enter characters send bluefruit"));
serial.println(001);
ble.verbose(false); // debug info little annoying after point!
/* wait connection */
while (! ble.isconnected()) {
delay(500);
}
// led activity command supported 0.6.6
if ( ble.isversionatleast(minimum_firmware_version) )
{
// change mode led activity
/* serial.println(f("******************************"));
serial.println(f("change led activity " mode_led_behaviour));
ble.sendcommandcheckok("at+hwmodeled=" mode_led_behaviour);
serial.println(f("******************************"));*/
}
}
void loop(void){
//checkstatsforpixels();
// check user input
char inputs[bufsize+1];
if ( getuserinput(inputs, bufsize) )
{
// send characters bluefruit
serial.print("[send] ");
serial.println(inputs);
ble.print("at+bleuarttx=");
ble.println(inputs);
// check response stastus
if (! ble.waitforok() ) {
serial.println(f("failed send?"));
}
}
// check incoming characters bluefruit
ble.println("at+bleuartrx");
ble.readline();
if (strcmp(ble.buffer, "ok") == 0) {
// no data
return;
}
// data found, in buffer
serial.print(f("[recv] ")); serial.println(ble.buffer);
//if(ble.buffer == "getstats"){
float temperature = bme.readtemperature();
float humidity = bme.readhumidity();
float pressure = bme.readpressure() / 100.0f * 0.7500616827;
float altitude = bme.readaltitude(sealevelpressure_hpa);
char valsize[10];
string temperatureasstring;
string humidityasstring;
string pressureasstring;
string altitudeasstring;
string d_stats;
dtostrf(temperature,1,2,valsize);
temperatureasstring = string(valsize);
dtostrf(humidity,1,2,valsize);
humidityasstring = string(valsize);
dtostrf(pressure,1,2,valsize);
pressureasstring = string(valsize);
dtostrf(altitude,1,2,valsize);
altitudeasstring = string(valsize);
d_stats = "{'d_stats':[{'t':'" + temperatureasstring + "','h':'"+humidityasstring+"','p':'"+pressureasstring+"','a':'"+altitudeasstring+"'}]}\n";
ble.print("at+bleuarttx=");
ble.println(d_stats);
if(initialstart == 0){
initialstart = 1;
checkstatsforpixels();
}
serial.println(d_stats);
//}
ble.waitforok();
}
/****************/
// biostats
uint32_t redcolor = pixels.color(255,0,0);
uint32_t bluecolor = pixels.color(0,0,255);
uint32_t greencolor = pixels.color(0,255,0);
int checkstatsforpixels(){
for(int = 0; < 10000000; i++) {
float t = bme.readtemperature();
float h = bme.readhumidity();
float p = bme.readpressure() / 100.0f * 0.7500616827;
float = bme.readaltitude(sealevelpressure_hpa);
uint32_t tempcolor = redcolor;
if(t < 12){
tempcolor = bluecolor;
}else
if(t > 12 && t < 20){
tempcolor = greencolor;
}else
if(t > 20){
tempcolor = redcolor;
}
pixels.setpixelcolor(0, tempcolor);
pixels.show();
serial.println(t);
ble.print("at+bleuarttx=");
ble.println(t);
delay(1000);
}
}
/**************************************************************************/
/*!
@brief checks user input (via serial monitor)
*/
/**************************************************************************/
bool getuserinput(char buffer[], uint8_t maxsize)
{
// timeout in 100 milliseconds
timeouttimer timeout(100);
memset(buffer, 0, maxsize);
while( (serial.peek() < 0) && !timeout.expired() ) {}
if ( timeout.expired() ) return false;
delay(2);
uint8_t count=0;
do
{
count += serial.readbytes(buffer+count, maxsize);
delay(2);
} while( (count < maxsize) && !(serial.peek() < 0) );
return true;
}
quote
i want code start when battery powers device.what evidence have setup() not run when power applied ?
which arduino using ?
Arduino Forum > Using Arduino > Programming Questions > Clean up code and fix setup
arduino
Comments
Post a Comment