Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- based on the original work done by
- (c)2018 Pawel A. Hernik
- YouTube video:
- https://youtu.be/RIt08GJH2IA
- https://www.instructables.com/Fun-With-OLED-Display-and-Arduino/
- */
- /*
- Cheap digital caliper pinout (from top left):
- LOOKING AT THE FACE OF THE DEVICE
- #1 - GND (Brown) | BLACK IN VIDEO
- #2 - DAT (Blue) | GREEN IN VIDEO
- #3 - CLK (WH/Blue) | BLUE IN VIDEO
- #4 - VCC 1.5-1.8V (WH/BRown) | RED IN VIDEO
- */
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
- #define DATA_PIN 1 // green
- #define CLOCK_PIN 2 // white-green
- // These are pins A2 & A3
- // digitalWrite(pin, value)
- // for grabbing power and ground from other pins like A0 and A1
- void setup()
- {
- Serial.begin(9600);
- pinMode(0, OUTPUT); // sets the digital pin as output
- pinMode(3, OUTPUT); // sets the digital pin as output
- digitalWrite(0, LOW); // GROUND white-orange
- digitalWrite(3, HIGH); // +5V orange
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
- Serial.println(F("SSD1306 allocation failed"));
- for(;;);
- }
- Serial.println("Ready");
- pinMode(CLOCK_PIN, INPUT);
- pinMode(DATA_PIN, INPUT);
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(10, 0);
- // Display static text
- Serial.println("Hello World! Setup");
- // display.println("Hello World! Setup");
- // display.display();
- delay(500);
- // display.clearDisplay();
- // display.setCursor(10, 0);
- Serial.println("Caliper Setup");
- // display.println("Caliper Setup");
- // display.display();
- }
- char buf[20];
- unsigned long tmpTime;
- int sign;
- int inches;
- int cm;
- long value;
- float result;
- void loop()
- {
- while(digitalRead(CLOCK_PIN)==LOW) {}
- tmpTime=micros();
- while(digitalRead(CLOCK_PIN)==HIGH) {}
- // while(digitalRead(CLOCK_PIN)==HIGH) { Serial.println("Clock is still high."); }
- if((micros()-tmpTime)<500) return;
- readCaliper();
- Serial.println(result,2); Serial.println(inches);
- buf[0]=' ';
- dtostrf(result,6,3,buf+1); strcat(buf," in ");
- // display.setCursor(20, 0);
- // display.println(buf);
- // Serial.println(result,2); Serial.println(inches);
- // Serial.println("Inch Rreading");
- Serial.println(buf);
- dtostrf(result*2.54,6,3,buf+1); strcat(buf," cm ");
- // display.setCursor(30, 0);
- // display.println(buf);
- // Serial.println(result*2.54, 2); Serial.println(cm);
- // Serial.println("cm Rreading");
- Serial.println(buf);
- delay(250);
- // display.display();
- }
- void readCaliper()
- {
- sign=1;
- value=0;
- inches=0;
- for(int i=0;i<24;i++) {
- while(digitalRead(CLOCK_PIN)==LOW) {}
- while(digitalRead(CLOCK_PIN)==HIGH) {}
- if(digitalRead(DATA_PIN)==HIGH) {
- if(i<20) value|=(1<<i);
- if(i==20) sign=-1;
- if(i==23) inches=1; // doesn't work with my caliper, always returns inches
- }
- }
- result=(value*sign)/(inches?2000.0:100.0);
- }
Add Comment
Please, Sign In to add comment