/*=============================================
Program : Digital Angle Divider
Author : wickPro-PC\wickPro
Time : 9:30 PM
Date : 02/08/2018
Coded by: Pramod C Wickramasinghe
E-mail : wick.pro@gmail.com
Mobile : +94774585875
=============================================*/
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#define I2C_ADDRESS 0x3C
#define zero 2
#define units 3
SSD1306AsciiWire oled;
int measureValue, unitsVal, zeroVal = 0;
int noOfReadings = 10;
double _value, buff_1, buff_2, buff_3, buff_4, setZero = 0;
//-----------------------------------------------------------------------------
void reading() {
if (unitsVal % 2 == 0) {
//_value = (map(measureValue, 0, 1023, 0, 254) / 10.0) ;
// _value = (-0.0001)*(measureValue)*(measureValue) + (0.3249)*(measureValue) + (40.78) - 1; // 1.0 offset occured
buff_1 = (-0.00010954) * (measureValue) ;
buff_2 = buff_1 * (measureValue);
buff_3 = (0.32620281) * (measureValue);
buff_4 = buff_3 + 44.64231864;
_value = buff_2 + buff_4 - setZero;
} else {
//_value = (map(measureValue, 0, 1023, 0, 10) / 10.0) ;
buff_1 = (-0.00000426) * (measureValue) ;
buff_2 = buff_1 * (measureValue);
buff_3 = (0.01289839) * (measureValue);
buff_4 = buff_3 + 1.72506535;
_value = buff_2 + buff_4 - setZero; // coEff =
}
oled.setFont(Callibri15);
oled.setLetterSpacing(2);
oled.set2X();
oled.setCursor(0, 2);
oled.print(" ");
if (_value < 1000 && 100 <= _value) {
oled.setCursor(15, 2);
} else if (_value < 100 && 10 <= _value) {
oled.setCursor(28, 2);
} else if (_value < 10 && 0 <= _value) {
oled.setCursor(40, 2);
} else if (_value < 0 && -10 <= _value) {
oled.setCursor(30, 2);
} else if (_value < -10 && -100 <= _value) {
oled.setCursor(20, 2);
} else if (_value < -100 && -1000 <= _value) {
oled.setCursor(7, 2);
} else {
oled.setCursor(0, 2);
}
oled.print(_value);
}
//------------------------------------------------------------------------------
void setup() {
Wire.begin();
oled.begin(&Adafruit128x64, I2C_ADDRESS);
oled.setFont(Callibri11_bold);
oled.setCursor(0, 0);
oled.println("Measurement:");
//oled.setCursor(100, 2);
//oled.print("mm");
oled.setCursor(5, 6);
oled.print("Zero");
oled.setCursor(100, 6);
oled.print("Units");
pinMode(zero, INPUT);
pinMode(units, INPUT);
}
//------------------------------------------------------------------------------
void loop() {
if (digitalRead(units) == LOW) {
unitsVal++;
delay(100);
}
if (digitalRead(zero) == LOW) {
setZero = 0;
reading();
setZero = _value;
delay(100);
}
if (unitsVal % 2 == 0) {
oled.setFont(Callibri11_bold);
oled.setLetterSpacing(1);
oled.set1X();
oled.setCursor(100, 2);
oled.print("mm");
} else {
oled.setFont(Callibri11_bold);
oled.setLetterSpacing(1);
oled.set1X();
oled.setCursor(100, 2);
oled.print("inch");
}
for (byte i = 0; i < noOfReadings; i++) {
measureValue = measureValue + analogRead(A0);
delayMicroseconds(1000);
}
measureValue = measureValue / noOfReadings;
reading();
}
Comments