Advertisement
Guest User

TinyScreen / Accel

a guest
Mar 18th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #define BLACK           0x00
  2. #define BLUE            0xE0
  3. #define RED             0x03
  4. #define GREEN           0x1C
  5. #define DGREEN           0x0C
  6. #define YELLOW          0x1F
  7. #define WHITE           0xFF
  8. #define ALPHA           0xFE
  9. #define BROWN           0x32
  10.  
  11. #include "SSD1331.h"
  12. #include "BMA250.h"
  13. #include <SPI.h>
  14. #include <Wire.h>
  15. #include <SD.h>
  16.  
  17. SSD1331 display = SSD1331(0);
  18. BMA250 accel;
  19. const int chipSelect = 10;
  20.  
  21. int x,y,z  = 0;
  22. long count, time, delta = 0;
  23. uint8_t back = BLACK;
  24.  
  25. void setup(void) {
  26.   Serial.begin(9600); while (!Serial) { ; /*  wait for serial port to connect. */ }
  27.   Wire.begin();
  28.  
  29.   // Init accelerator
  30.   accel.begin(BMA250_range_2g, BMA250_update_time_64ms); //This sets up the BMA250 accelerometer
  31.  
  32.   // Init display
  33.   display.begin();
  34.  
  35.   // Init SD Card
  36.   Serial.print("Initializing SD card...");
  37.   pinMode(10, OUTPUT);
  38.  
  39.   // see if the card is present and can be initialized:
  40.   if (!SD.begin(chipSelect)) {
  41.     Serial.println("Card failed, or not present"); return; // don't do anything more:
  42.   }
  43.   Serial.println("card initialized.");
  44. }
  45.  
  46. void loop() {
  47.   accel.read(); // Retrieve accelerometer data
  48.   display.setFont(liberationSans_12ptFontInfo);
  49.  
  50.   int math = abs(accel.X - x) + abs(accel.Y - y) + abs(accel.Z - z);
  51.   if (math > 50){
  52.     x = accel.X;  y = accel.Y;  z = accel.Z;
  53.     count = 0;
  54.   }
  55.  
  56.   if (count++ > 4){
  57.     writeSD(delta, time); time = 0;
  58.     back = BLACK;
  59.     display.clearWindow(0,0,96,64);
  60.   } else {
  61.     time++;
  62.     back = RED;
  63.     display.drawRect(0,0,96,64,1,back);
  64.   }
  65.  
  66.   display.setCursor(0,10);
  67.   display.fontColor(BLUE,back);
  68.   display.print("X = "+ String(accel.X) + " / " + String(x));
  69.  
  70.   display.setCursor(0,25);
  71.   display.fontColor(YELLOW,back);
  72.   display.print("Y = "+ String(accel.Y) + " / " + String(y));
  73.  
  74.   display.setCursor(0,40);
  75.   display.fontColor(GREEN,back);
  76.   display.print("Z = "+ String(accel.Z) + " / " + String(z));
  77.  
  78.   // Serial.print("  Temperature(C) = ");
  79.   // Serial.println((accel.rawTemp*0.5)+24.0,1);
  80.   delta++;
  81.   delay(250); // Retrieve accelerometer data more than 64ms update time set on the BMA250
  82. }
  83.  
  84. void writeSD(long delta, long time){
  85.   if (time == 0) return;
  86.   File dataFile = SD.open("datalog.json", FILE_WRITE);
  87.   String str = "{ \"delta\": " + String(delta*250/1000) + ", \"duration\":" + String(time*250/1000) + "}";
  88.   dataFile.println(str);
  89.   dataFile.close();
  90.   Serial.println(str);
  91.  
  92.   if (SD.exists("datalog.json")) {
  93.     Serial.println("datalog.json exists.");
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement