Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BLACK 0x00
- #define BLUE 0xE0
- #define RED 0x03
- #define GREEN 0x1C
- #define DGREEN 0x0C
- #define YELLOW 0x1F
- #define WHITE 0xFF
- #define ALPHA 0xFE
- #define BROWN 0x32
- #include "SSD1331.h"
- #include "BMA250.h"
- #include <SPI.h>
- #include <Wire.h>
- #include <SD.h>
- SSD1331 display = SSD1331(0);
- BMA250 accel;
- const int chipSelect = 10;
- int x,y,z = 0;
- long count, time, delta = 0;
- uint8_t back = BLACK;
- void setup(void) {
- Serial.begin(9600); while (!Serial) { ; /* wait for serial port to connect. */ }
- Wire.begin();
- // Init accelerator
- accel.begin(BMA250_range_2g, BMA250_update_time_64ms); //This sets up the BMA250 accelerometer
- // Init display
- display.begin();
- // Init SD Card
- Serial.print("Initializing SD card...");
- pinMode(10, OUTPUT);
- // see if the card is present and can be initialized:
- if (!SD.begin(chipSelect)) {
- Serial.println("Card failed, or not present"); return; // don't do anything more:
- }
- Serial.println("card initialized.");
- }
- void loop() {
- accel.read(); // Retrieve accelerometer data
- display.setFont(liberationSans_12ptFontInfo);
- int math = abs(accel.X - x) + abs(accel.Y - y) + abs(accel.Z - z);
- if (math > 50){
- x = accel.X; y = accel.Y; z = accel.Z;
- count = 0;
- }
- if (count++ > 4){
- writeSD(delta, time); time = 0;
- back = BLACK;
- display.clearWindow(0,0,96,64);
- } else {
- time++;
- back = RED;
- display.drawRect(0,0,96,64,1,back);
- }
- display.setCursor(0,10);
- display.fontColor(BLUE,back);
- display.print("X = "+ String(accel.X) + " / " + String(x));
- display.setCursor(0,25);
- display.fontColor(YELLOW,back);
- display.print("Y = "+ String(accel.Y) + " / " + String(y));
- display.setCursor(0,40);
- display.fontColor(GREEN,back);
- display.print("Z = "+ String(accel.Z) + " / " + String(z));
- // Serial.print(" Temperature(C) = ");
- // Serial.println((accel.rawTemp*0.5)+24.0,1);
- delta++;
- delay(250); // Retrieve accelerometer data more than 64ms update time set on the BMA250
- }
- void writeSD(long delta, long time){
- if (time == 0) return;
- File dataFile = SD.open("datalog.json", FILE_WRITE);
- String str = "{ \"delta\": " + String(delta*250/1000) + ", \"duration\":" + String(time*250/1000) + "}";
- dataFile.println(str);
- dataFile.close();
- Serial.println(str);
- if (SD.exists("datalog.json")) {
- Serial.println("datalog.json exists.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement