Skip to content

Instantly share code, notes, and snippets.

@woodif
Last active August 29, 2015 14:23
Show Gist options
  • Save woodif/e15bc8e3772ccdd1679b to your computer and use it in GitHub Desktop.
Save woodif/e15bc8e3772ccdd1679b to your computer and use it in GitHub Desktop.
ESP8266_Shield_Nano_gravitech
/*Example CMMC ESP8266*/
#include "Wire.h" // enable I2C bus
byte saa1064 = 0x70 >> 1; // define the I2C bus address for our SAA1064 (pin 1 to GND)
int digits[17] = {
63, 6, 91, 79, 102, 109, 125, 7, 127, 111, 119, 124, 57, 94, 121, 113, 0
};
// these are the byte representations of pins required to display each digit 0~9 then A~F, and blank digit
int sensorPin = A0;
int sensorValue = 0;
void setup()
{
Wire.begin(); // start up I2C bus
delay(500);
initDisplay();
pinMode(15, OUTPUT); //direction
pinMode(16, OUTPUT); //pwm
}
void initDisplay()
// turns on dynamic mode and adjusts segment current to 12mA
{
Wire.beginTransmission(saa1064);
Wire.write(B00000000); // this is the instruction byte. Zero means the next byte is the control byte
Wire.write(B01000111); // control byte (dynamic mode on, digits 1+3 on, digits 2+4 on, 12mA segment current
Wire.endTransmission();
}
void clearDisplay()
{
Wire.beginTransmission(saa1064);
Wire.write(1); // start with digit 1 (right-hand side)
Wire.write(0); // blank digit 1
Wire.write(0); // blank digit 2
Wire.write(0); // blank digit 3
Wire.write(0); // blank digit 4
Wire.endTransmission();
}
void displayInteger(int num, int zero)
// displays the number 'num'
// zero = 0 - no leading zero
// zero = 1 - leading zero
{
int thousand, hundred, ten, one;
// breakdown number into columns
thousand = num / 1000;
hundred = (num - (thousand * 1000)) / 100;
ten = (num - ((thousand * 1000) + (hundred * 100))) / 10;
one = num - ((thousand * 1000) + (hundred * 100) + (ten * 10));
if (zero == 1) // yes to leading zero
{
Wire.beginTransmission(saa1064);
Wire.write(1);
Wire.write(digits[one]);
Wire.write(digits[ten]);
Wire.write(digits[hundred]);
Wire.write(digits[thousand]);
Wire.endTransmission();
delay(10);
}
else if (zero == 0) // no to leading zero
{
if (thousand == 0) {
thousand = 16;
}
if (hundred == 0 && num < 100) {
hundred = 16;
}
if (ten == 0 && num < 10) {
ten = 16;
}
Wire.beginTransmission(saa1064);
Wire.write(1);
Wire.write(digits[one]);
Wire.write(digits[ten]);
Wire.write(digits[hundred]);
Wire.write(digits[thousand]);
Wire.endTransmission();
delay(10);
}
}
void loop()
{
sensorValue = analogRead(sensorPin) - 168;
digitalWrite(15, HIGH);
analogWrite(16, 1023);
displayInteger(sensorValue, 1);
delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment