misaalanshori

attiny85ssd1331

Dec 1st, 2020
634
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ssd1306.h"
  2. #include "nano_gfx.h"
  3. // Attiny SPI PINS:     connect LCD to D4 (D/C), GND (CS), D3 (RES), D1(DIN), D2(CLK)
  4.  
  5. float usd(int trigger) {
  6.   float dur, dist;
  7.   pinMode(trigger, OUTPUT);
  8.   digitalWrite(trigger, LOW);   // set the trigger pin LOW
  9.   delayMicroseconds(2);
  10.   digitalWrite(trigger, HIGH);  // set the trigger pin HIGH for 10μs
  11.   delayMicroseconds(10);
  12.   digitalWrite(trigger, LOW);
  13.   pinMode(trigger, INPUT);
  14.   dur = pulseIn(trigger, HIGH);  // measure the echo time (μs)
  15.   dist = ((dur/2.0)*0.0343)/2;   // convert echo time to distance (cm)
  16.   return dist;
  17. }
  18.  
  19. void setup() {
  20.   // put your setup code here, to run once:
  21.  
  22.   ssd1331_96x64_spi_init(3, -1, 4);
  23.   ssd1306_clearScreen();
  24.   ssd1306_setFixedFont(ssd1306xled_font6x8);
  25.   ssd1306_setColor(RGB_COLOR8(255,255,255));
  26.  
  27. }
  28.  
  29. void loop() {
  30.  
  31.   char result[8]; // Buffer big enough for 7-character float
  32.   char cm[3] = "cm  ";
  33.   char finally[11];
  34.  
  35.     float distance = 0;
  36.     float a = 0;
  37.     int vari = 0;
  38.    
  39.     while (vari < 5) {
  40.     a = usd(0);
  41.       while (a > 400) {
  42.         a = usd(0);
  43.           }
  44.     distance += a;
  45.     vari++;
  46.   }
  47.  
  48.   distance = distance/5;
  49.  
  50.   dtostrf(distance, 6, 2, result); // Leave room for too large numbers!
  51.   strcpy(finally, result);
  52.   strcat(finally, cm);
  53.   ssd1306_printFixed2x(0,  16, finally, STYLE_NORMAL);
  54. }
RAW Paste Data