Thanks to Adafruit we can build up our smart watch very fast.

Btw. if you are unsure if your i2C Components are right addressed, use the i2C scanner you'll find it here :



http://playground.arduino.cc/Main/I2cScanner

I implemented different views like a clock ( of course ;) ) movement, acceleration, roll & pitch a breakout game, pedometer ( here I 'll come back later )

Simple start

So let's start, first include some header, you'll find them in the Arduino library.



#include SPI.h

#include Wire.h

#include Adafruit_GFX.h

#include Adafruit_SSD1306.h #include L3G4200D.h #include Adafruit_Sensor.h #include Adafruit_ADXL345_U.h #include Adafruit_BMP085.h

Define the obj we later use.

Adafruit_SSD1306 display(4);

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); Adafruit_BMP085 bmp; L3G4200D gyro;

Init everything we need

void setup() {

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // adress display.clearDisplay(); // cls, clear screen Serial.begin(9600); // console output Serial1.begin(9600); // serial bluetooth output display.setTextSize(2); // define some display styles display.setTextColor(WHITE); Wire.begin(); gyro.enableDefault(); accel.begin(); accel.setRange(ADXL345_RANGE_16_G); }

The Tricky Part, time counting



In our loop we will process everything. Getting messages from Serail1 ( bluetooth ) counting the time, display different views etc.

Because of this, we won't delay a second and then increase the time. We need to react quite fast, if there is a message pending in the serail1 queue or of operation time like calculation roll & pitch, displaying information or running the breakout game.

This leads to a dynamic delay time to reduce the error (every calculation costs cpu time )

void loop() {

int mydelay = 100; // get current milli seconds long mimi_start = millis(); long v = 0; bt_settings(); // check bluetooth queue clockTick(); // update periodontally if( counter == 0){ // update these function every second switch( show_screen ){ case CLOCK : renderClock(); break; case TEMP : show_temp(); break; case ACCEL : show_accel(); break; case GYRO : show_gyro(); break; } } // this function will be updated more often if( show_screen == PEDO ){ show_pedo(); } // the game will also be more often be updated if( show_screen == GAME1 ){ play_game_1(); } v = millis() - mimi_start; // dynamical delay calculation // wait const. 100 micro seconds // millis() will be reset after 50 days ! so we will get a delay if( v < 0 ){ mydelay = 100; } else if( v > 100 ){ mydelay = 0; count_error += v - 100; while( count_error > 100 ){ count_error -= 100; counter++; } } else{ mydelay = 100-v; } delay(mydelay); counter++; }

In Fact that's it.

The bt_settings function can also cost a lot of time so we need to handle that. In my case I do some string operations to set different displays or set the time.

void bt_settings(){

if ( Serial1.available() ) { // if there is something in the queue, let's start long v1 = millis(); // we calculate our time error later long v2; BT_Text = Serial1.readString(); // some string operations v2 = (millis() - v1); while( v2 >= 100 ){ v2 -= 100; counter ++; // correct the time if necessary } } }

And finally the clock

void renderClock(){

display.setTextSize(2); display.clearDisplay(); display.setCursor(12,10); display.print( formatValue(std_) + ":" + formatValue(min_) + ":"+ formatValue(sec_) ); display.display(); }

A helper function

String formatValue( int v ){

String s = ""; if( v <10) s = "0"; s += String(v); return s; }

End? -