Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code used with flightgear
//PIN 0 -> 7 has positive segment part
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(49, OUTPUT);
pinMode(50, OUTPUT);
pinMode(51, OUTPUT);
pinMode(52, OUTPUT);
pinMode(53, OUTPUT);
Serial.begin(9600);
}
void writeNumber(int nr)
{
if(nr == 0)
{
digitalWrite(2, LOW); // midden
digitalWrite(3, HIGH); // lt
digitalWrite(4, HIGH); // t
digitalWrite(5, HIGH); // rt
digitalWrite(6, HIGH); // lb
digitalWrite(7, HIGH); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 1)
{
digitalWrite(2, LOW); // midden
digitalWrite(3, LOW); // lt
digitalWrite(4, LOW); // t
digitalWrite(5, HIGH); // rt
digitalWrite(6, LOW); // lb
digitalWrite(7, LOW); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 2)
{
digitalWrite(2, HIGH); // midden
digitalWrite(3, LOW); // lt
digitalWrite(4, HIGH); // t
digitalWrite(5, HIGH); // rt
digitalWrite(6, HIGH); // lb
digitalWrite(7, HIGH); // b
digitalWrite(8, LOW); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 3)
{
digitalWrite(2, HIGH); // midden
digitalWrite(3, LOW); // lt
digitalWrite(4, HIGH); // t
digitalWrite(5, HIGH); // rt
digitalWrite(6, LOW); // lb
digitalWrite(7, HIGH); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 4)
{
digitalWrite(2, HIGH); // midden
digitalWrite(3, HIGH); // lt
digitalWrite(4, LOW); // t
digitalWrite(5, HIGH); // rt
digitalWrite(6, LOW); // lb
digitalWrite(7, LOW); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 5)
{
digitalWrite(2, HIGH); // midden
digitalWrite(3, HIGH); // lt
digitalWrite(4, HIGH); // t
digitalWrite(5, LOW); // rt
digitalWrite(6, LOW); // lb
digitalWrite(7, HIGH); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 6)
{
digitalWrite(2, HIGH); // midden
digitalWrite(3, HIGH); // lt
digitalWrite(4, HIGH); // t
digitalWrite(5, LOW); // rt
digitalWrite(6, HIGH); // lb
digitalWrite(7, HIGH); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 7)
{
digitalWrite(2, LOW); // midden
digitalWrite(3, LOW); // lt
digitalWrite(4, HIGH); // t
digitalWrite(5, HIGH); // rt
digitalWrite(6, LOW); // lb
digitalWrite(7, LOW); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 8)
{
digitalWrite(2, HIGH); // midden
digitalWrite(3, HIGH); // lt
digitalWrite(4, HIGH); // t
digitalWrite(5, HIGH); // rt
digitalWrite(6, HIGH); // lb
digitalWrite(7, HIGH); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else if(nr == 9)
{
digitalWrite(2, HIGH); // midden
digitalWrite(3, HIGH); // lt
digitalWrite(4, HIGH); // t
digitalWrite(5, HIGH); // rt
digitalWrite(6, LOW); // lb
digitalWrite(7, HIGH); // b
digitalWrite(8, HIGH); // rb
digitalWrite(9, LOW); // dot
}
else
{
digitalWrite(2, LOW); // midden
digitalWrite(3, LOW); // lt
digitalWrite(4, LOW); // t
digitalWrite(5, LOW); // rt
digitalWrite(6, LOW); // lb
digitalWrite(7, LOW); // b
digitalWrite(8, LOW); // rb
digitalWrite(9, LOW); // dot
}
}
// the loop routine runs over and over again forever
long number = 0;
int decimals[5] = {0, 0, 0, 0, 0};
void loop() {
for(int i = 49; i < 54; i++)
{
// Disable the incorrect segment displays
if(i == 49)
{
digitalWrite(53, HIGH);
}
else
{
digitalWrite(i - 1, HIGH);
}
digitalWrite(i, LOW);
// Enable the segments
writeNumber(decimals[4 - (i - 49)]);
delay(1);
}
if(Serial.available() > 14) // Wait until there are two bytes available. Then read them out.
{
String command;
String var;
char lastchar;
while(lastchar != '=')
{
lastchar = Serial.read();
if(lastchar != '=')
{
command += lastchar;
}
}
while(lastchar != '\n')
{
lastchar = Serial.read();
if(lastchar != '\n')
{
var += lastchar;
}
}
if(command == "altitude" )
{
char buf[50];
var.toCharArray(buf, 50);
number = atol(buf);
}
/*if(number == 10000)
{
number = 0;
}*/
long currentnumber = number;
int remainder = currentnumber % 10;
currentnumber = (currentnumber - remainder) / 10;
decimals[4] = remainder;
remainder = currentnumber % 10;
currentnumber = (currentnumber - remainder) / 10;
decimals[3] = remainder;
remainder = currentnumber % 10;
currentnumber = (currentnumber - remainder) / 10;
decimals[2] = remainder;
remainder = currentnumber % 10;
currentnumber = (currentnumber - remainder) / 10;
decimals[1] = remainder;
remainder = currentnumber % 10;
currentnumber = (currentnumber - remainder) / 10;
decimals[0] = remainder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.