Stap 3: De code
#include "SevSeg.h"SevSeg sevseg;
int refreshTime = 1000;
int pingPin = 14; 10
int inPin = 15; 9
lange duur, inch, cm;
int indec, cmdec;
int inchconv = 147;
int cmconv = 59;
niet-ondertekende lange timer;
VOID Setup {}
sevseg. Begin(0,2,3,4,5,6,7,8,9,10,11,12,13);
pinMode (pingPin, OUTPUT);
pinMode (inPin, INPUT);
}
void loop
{sevseg. PrintOutput();
digitalWrite (pingPin, laag);
delayMicroseconds(2);
digitalWrite (pingPin, hoge);
delayMicroseconds(10);
digitalWrite (pingPin, laag);
duur = pulseIn (inPin, hoge);
duim = microsecondsToInches(duration);
indec = (duur - inch * inchconv) * 10 / inchconv;
cm = microsecondsToCentimeters(duration);
cmdec = (duur - cm * cmconv) * 10 / cmconv;
unsigned long mils=millis();
Als (mils-timer > = refreshTime) {}
timer = mils;
sevseg. NewNum(cm,(byte) 2);
}
}
lange microsecondsToInches(long microseconds)
{
retourneren van microseconden / inchconv;
}
lange microsecondsToCentimeters(long microseconds)
{
retourneren van microseconden / cmconv;
}