数码管驱动库使用
sevseg.Begin(0,10,11,12,13,2,3,4,5,6,7,8,9); //sevseg.Begin(共阴0共阳1,共极引脚,a-g,dp引脚);
int val = analogRead(A0); val = map(val,0,1023,0,9); //使用map函数来做数值映射
Serial.println("串口显示四位数码管"); sevseg.Begin(0,10,11,12,13,2,3,4,5,6,7,8,9);
while (Serial.available() > 0) { int inChar = Serial.read(); inString += (char)inChar;
Serial.print("输入的字符串数不能大于四!"); //Serial.print("Value:"); //Serial.println(inString.toInt()); Serial.print("String: "); Serial.println(inString);
int val[10]={0000,1111,2222,3333,4444,5555,6666,7777,8888,9999}; sevseg.Begin(0,10,11,12,13,2,3,4,5,6,7,8,9);
#if defined(ARDUINO) && ARDUINO >= 100
void NewNum(int number_in, byte DecPlace_in); void Begin(boolean mode_in,byte C1, byte C2, byte C3, byte C4, byte UC1, byte UC2, byte UC3, byte UC4, byte UC5, byte UC6, byte UC7, byte UC8);
boolean mode,DigitOn,DigitOff,SegOn,SegOff;
#if defined(ARDUINO) && ARDUINO >= 100 void NewNum(int number_in, byte DecPlace_in); void Begin(boolean mode_in,byte C1, byte C2, byte C3, byte C4, byte UC1, byte UC2, byte UC3, byte UC4, byte UC5, byte UC6, byte UC7, byte UC8); boolean mode,DigitOn,DigitOff,SegOn,SegOff;
/*******************************************************************************************/ //Set pin modes and turns all displays off void SevSeg::Begin(boolean mode_in,byte D1, byte D2, byte D3, byte D4, byte S1, byte S2, byte S3, byte S4, byte S5, byte S6, byte S7, byte S8){
//Assign input values to variables //Mode sets what the digit pins must be set at for it to be turned on. 0 for common cathode, 1 for common anode
//Set Pin Modes as outputs for (byte digit=0;digit<4;digit++) { pinMode(DigitPins[digit], OUTPUT); for (byte seg=0;seg<8;seg++) { pinMode(SegmentPins[seg], OUTPUT);
//Set all digit pins off. Low for common anode, high for common cathode for (byte digit=0;digit<4;digit++) { digitalWrite(DigitPins[digit], DigitOff); //Set all segment pins off. High for common anode, low for common cathode for (byte seg=0;seg<8;seg++) { digitalWrite(SegmentPins[seg], SegOff);
/*******************************************************************************************/ //Cycles through each segment and turns on the correct digits for each. void SevSeg: rintOutput(){ for (byte seg=0;seg<8;seg++) { //Turn the relevant segment on digitalWrite(SegmentPins[seg],SegOn);
//For each digit, turn relevant digits on for (byte digit=0;digit<4;digit++){ if (lights[digit][seg]==1) { digitalWrite(DigitPins[digit],DigitOn); //delay(200); //Uncomment this to see it in slow motion for (byte digit=0;digit<4;digit++){ digitalWrite(DigitPins[digit],DigitOff);
//Turn the relevant segment off digitalWrite(SegmentPins[seg],SegOff);
/*******************************************************************************************/ //Function to update the number displayed void SevSeg::NewNum(int number_in, byte DecPlace_in) //Feed the inputs into the library's variables
/*******************************************************************************************/ //Function to find the four individual digits to be displayed from the variable 'number' void SevSeg::FindNums() { //If the number received is negative, set the flag and make the number positive
//If the number is out of range, just display '----' if ((negative==0 && number>9999) || (negative==1 && number>999)) {
total=total-nums[0]*1000; nums[3]=total-nums[2]*10;
//If there are zeros, set them to 20 which means a blank //However, don't cut out significant zeros if (nums[0]==0 && DecPlace<3){ if (nums[1]==0 && DecPlace<2) { if (nums[2]==0 && DecPlace==0) { if (nums[1]==0 && DecPlace<2) { if (nums[2]==0 && DecPlace==0) {
/*******************************************************************************************/ //From the numbers found, says which LEDs need to be turned on void SevSeg::CreateArray() { for (byte digit=0;digit<4;digit++) {
//Set the decimal place lights
|