Advertisement
gio_aggiustatutto

Sketch contachilometri per la bicicletta con display SH1106

May 20th, 2022
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. // Sketch by GIOVANNI AGGIUSTATUTTO
  2.  
  3. /*********************************************************************
  4.   This is an example for our Monochrome OLEDs based on SSD1306 drivers
  5.  
  6.   Pick one up today in the adafruit shop!
  7.   ------> http://www.adafruit.com/category/63_98
  8.  
  9.   This example is for a 128x64 size display using I2C to communicate
  10.   3 pins are required to interface (2 I2C and one reset)
  11.  
  12.   Adafruit invests time and resources providing this open source code,
  13.   please support Adafruit and open-source hardware by purchasing
  14.   products from Adafruit!
  15.  
  16.   Written by Limor Fried/Ladyada  for Adafruit Industries.
  17.   BSD license, check license.txt for more information
  18.   All text above, and the splash screen must be included in any redistribution
  19. *********************************************************************/
  20.  
  21. /*********************************************************************
  22.   I change the adafruit SSD1306 to SH1106
  23.  
  24.   SH1106 driver don't provide several functions such as scroll commands.
  25.  
  26. *********************************************************************/
  27.  
  28. #include <SPI.h>
  29. #include <Wire.h>
  30. #include <Adafruit_GFX.h>
  31. #include <Adafruit_SH1106.h>
  32.  
  33. #define OLED_RESET 4
  34. Adafruit_SH1106 display(OLED_RESET);
  35.  
  36. const float circonferenza = 2.12;
  37.  
  38. int rp5s = 0;
  39. float rTOT = 0;
  40. int kmh = 0;
  41. float m = 0;
  42. float km = 0;
  43.  
  44. int b1;
  45. int b2;
  46. int b3;
  47. int b4;
  48. int b5;
  49. float battery = 0;
  50.  
  51. bool led = false;
  52.  
  53. void setup()   {
  54.   //Serial.begin(9600);
  55.   pinMode(2, INPUT);
  56.  
  57.   pinMode(13, OUTPUT);
  58.  
  59.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  60.   display.begin(SH1106_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  61.   // init done
  62.  
  63.   // Show image buffer on the display hardware.
  64.   // Since the buffer is intialized with an Adafruit splashscreen
  65.   // internally, this will display the splashscreen.
  66.   display.display();
  67.  
  68.   b1 = analogRead(A0);
  69.   delay(100);
  70.   b2 = analogRead(A0);
  71.   delay(100);
  72.   b3 = analogRead(A0);
  73.   delay(100);
  74.   b4 = analogRead(A0);
  75.   delay(100);
  76.   b5 = analogRead(A0);
  77.  
  78.   battery = (b1 + b2 + b3 + b4 + b5) / 5 * 5.0 / 1023;
  79.   display.clearDisplay();
  80.   delay(200);
  81.   display.setTextSize(2);
  82.   display.setTextColor(WHITE);
  83.   display.setCursor(0, 0);
  84.   display.println(" Ciao!");
  85.  
  86.   display.setTextSize(2);
  87.   display.setTextColor(WHITE);
  88.   display.setCursor(0, 25);
  89.   display.println(" Batteria");
  90.  
  91.   display.setTextSize(2);
  92.   display.setTextColor(WHITE);
  93.   display.setCursor(0, 45);
  94.   display.print(" ");
  95.   display.print(battery);
  96.   display.println(" V");
  97.  
  98.   display.display();
  99.  
  100.   delay(5000);
  101.  
  102.   // Clear the buffer.
  103.   display.clearDisplay();
  104. }
  105.  
  106. void loop() {
  107.   attachInterrupt(digitalPinToInterrupt(2), function, RISING);
  108.   delay(5000);
  109.   detachInterrupt(2);
  110.  
  111.   kmh = (rp5s) / 5.0 * circonferenza * 3.6;
  112.   m = (rTOT) * circonferenza;
  113.   km = m / 1000.0;
  114.  
  115.   display.clearDisplay();
  116.  
  117.   display.setTextSize(2);
  118.   display.setTextColor(WHITE);
  119.   display.setCursor(0, 0);
  120.   display.print(" ");
  121.   display.print(kmh);
  122.   display.println(" Km/h");
  123.  
  124.   display.setTextSize(2);
  125.   display.setTextColor(WHITE);
  126.   display.setCursor(0, 25);
  127.   display.print(" ");
  128.   display.print(km);
  129.   display.println(" Km");
  130.  
  131.   display.setTextSize(2);
  132.   display.setTextColor(WHITE);
  133.   display.setCursor(0, 45);
  134.   display.print(" ");
  135.   display.print(millis() / 60000);
  136.   display.println(" min");
  137.  
  138.   display.display();
  139.  
  140.   rp5s = 0;
  141. }
  142.  
  143. void function() {
  144.   rTOT = rTOT + 1.0;
  145.   rp5s = rp5s + 1.0;
  146.   //Serial.println(".");
  147.   digitalWrite(13, led);
  148.   led = !led;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement