Friday, November 26, 2021

Arduino Uno - 4-DIGIT 7-SEGMENT LED - Mode Common-Anode - animation running text from right to left

 Require components:

1x Arduino Uno

4x 220ohm Resistors

12x wires

1x 4-DIGIT 7-SEGMENT LED (model MR-3461SRB-2 13 20)



Code:
const byte PINS_DIGIT[] = {811127}; // 4 digits 7 segment pins: 12, 9, 8, 6
const byte TOTAL_DIGITS = sizeof(PINS_DIGIT);
const byte PINS_ABCDEFGP[] = {9135321064};  // pins from left to right: A, B, C, D, E, F, G, DotPoint, 4 digits 7 segment pins: 
const byte MESSAGE[] = { // list of bytes to display from top to bottom, 1 byte = 8 bits, bit froms right to left
  B00111101, // G
  B01011100, // o
  B01011100, // o
  B01011110, // d
  B00000000, // (space)
  B00110111, // N
  B00110111, // N
  B01011100, // o
  B01010000, // r
  B01010100, // n
  B00010000, // i
  B01010100, // n
  B01101111, // 9
  B00000000, // (space)
  B01110110, // H,X
  B01011100, // o
  B00011100, // u
  B00011100, // u
  B00000000, // (space)
  B11011100, // a
  B01010000, // r
  B01111011, // e
  B00000000, // (space)
  B01101110, // Y
  B01011100, // o
  B00011100, // u
  B00000000, // (space)
  B00110111, // N
  B00110111, // N
  B01101110, // Y
  B00000000, // (space)
  B01010100, // n
  B11011100, // a
  B01010100, // n
  B01010100, // n
  B01111011, // e
  B00000000, // (space)
  B00000110, // 1
  B01101101, // 5,S
  B00000000, // (space)
  B00111000, // L
  B01111011, // e
  B00000000, // (space)
  B00111001, // C
  B01011100, // o
  B01010100, // n
  B01101111, // 9
  B00000000, // (space)
  B01011110, // d
  B00011100, // u
  B01011000, // c
  B00000000, // (space)
  B00000000, // (space)
  B00000000, // (space)
};
const short MESSAGE_LENGTH = sizeof(MESSAGE);
const short DELAY_DIGIT = 50;
const short DELAY_MOVE = 1000;  // "short" cost 2 byte, value from -32,768 to 32,767
short delayRemain = DELAY_MOVE;
short offsetMessage = 0;
byte indexDigit = 0;

void setup() {
  for(byte indexPin = 0; indexPin < sizeof(PINS_ABCDEFGP); indexPin++) {
    pinMode(PINS_ABCDEFGP[indexPin], OUTPUT); // set pins A, B, C, D, E, F, G are output.
  }
  for(byte indexPin = 0; indexPin < TOTAL_DIGITS; indexPin++) {
    pinMode(PINS_DIGIT[indexPin], OUTPUT); // set pins digits 1, 2, 3, 4 are output
  }
}

void switchDigit(byte indexDigit) { // turn on 1 digit at time
  for(byte indexPin = 0; indexPin < TOTAL_DIGITS; indexPin++) {
    bool isOn = indexPin == indexDigit;
    digitalWrite(PINS_DIGIT[indexPin], isOn);
  }
}

void writeDigit(short byteDisplay) { // digitValue value from 0 to 9, this function turn on/off 8 LED (A, B, C, D, E, F, G, DotPoint)
  for(byte indexPin = 0; indexPin < sizeof(PINS_ABCDEFGP); indexPin++) { // indexPin (from left to right in array) = indexBit (from right to left in calculator)
    bool isOnModeCommonCathod = byteDisplay & (1 << indexPin); // get bit at indexPin; from right to left; value 0 = LED off = LOW; value 1 = LED on = HIGH
    bool isOnModeCommonAnode = !isOnModeCommonCathod;
    digitalWrite(PINS_ABCDEFGP[indexPin], isOnModeCommonAnode); // write out put pins (A, B, C, D, E, F, G, DotPoint) on or off, we are using mode common anode (CA)
  }
}

void loop() {
  while(delayRemain > 0) { // switch 4 digits until 
    switchDigit(indexDigit);
    byte byteDisplay = MESSAGE[(offsetMessage + indexDigit) % MESSAGE_LENGTH];
    writeDigit(byteDisplay);
    delay(DELAY_DIGIT);
    delayRemain -= DELAY_DIGIT; // each digit display will decrease time display the number.
    indexDigit = (indexDigit + 1) % TOTAL_DIGITS; // switch to next digit on the right
  }
  delayRemain += DELAY_MOVE;
  offsetMessage = (offsetMessage + 1) % MESSAGE_LENGTH;
}


Video:


Wednesday, November 24, 2021

Arduino Uno - 4-DIGIT 7-SEGMENT LED - Mode Common-Anode - Count up to 9999

 Require components:

1x Arduino Uno

4x 220ohm Resistors

12x wires

1x 4-DIGIT 7-SEGMENT LED (model MR-3461SRB-2 13 20)


Code:

const byte PINS_DIGIT[] = {811127}; // 4 digits 7 segment pins: 12, 9, 8, 6
const byte TOTAL_DIGITS = sizeof(PINS_DIGIT);
const byte PINS_ABCDEFGP[] = {9135321064};  // pins from left to right: A, B, C, D, E, F, G, DotPoint, 4 digits 7 segment pins: 
const byte BYTES_DISPLAY_DIGIT[] = { // list of bytes to display from top to bottom, 1 byte = 8 bits, bit froms right to left
  B00111111, // 0
  B00000110, // 1
  B01011011, // 2
  B01001111, // 3
  B01100110, // 4 
  B01101101, // 5
  B01111101, // 6
  B00000111, // 7
  B01111111, // 8
  B01101111, // 9
};
const byte DELAY_DIGIT = 1;
const short DELAY_NUMBER = 1000;  // "short" cost 2 byte, value from -32,768 to 32,767
short delayRemain = DELAY_NUMBER;

void setup() {
  // put your setup code here, to run once:
  for(byte indexPin = 0; indexPin < sizeof(PINS_ABCDEFGP); indexPin++) {
    pinMode(PINS_ABCDEFGP[indexPin], OUTPUT); // set pins A, B, C, D, E, F, G are output.
  }
  for(byte indexPin = 0; indexPin < TOTAL_DIGITS; indexPin++) {
    pinMode(PINS_DIGIT[indexPin], OUTPUT); // set pins digits 1, 2, 3, 4 are output
  }
}

void switchDigit(byte indexDigit) { // turn on 1 digit at time
  for(byte indexPin = 0; indexPin < TOTAL_DIGITS; indexPin++) {
    bool isOn = indexPin == indexDigit;
    digitalWrite(PINS_DIGIT[indexPin], isOn);
  }
}

void writeDigit(byte digitValue) { // digitValue value from 0 to 9, this function turn on/off 8 LED (A, B, C, D, E, F, G, DotPoint)
  byte byteDisplay = BYTES_DISPLAY_DIGIT[digitValue];
  for(byte indexPin = 0; indexPin < sizeof(PINS_ABCDEFGP); indexPin++) { // indexPin (from left to right in array) = indexBit (from right to left in calculator)
    bool isOnModeCommonCathod = byteDisplay & (1 << indexPin); // get bit at indexPin; from right to left; value 0 = LED off = LOW; value 1 = LED on = HIGH
    bool isOnModeCommonAnode = !isOnModeCommonCathod;
    digitalWrite(PINS_ABCDEFGP[indexPin], isOnModeCommonAnode); // write out put pins (A, B, C, D, E, F, G, DotPoint) on or off, we are using mode common anode (CA)
  }
}

byte getDigitValue(short numberbyte indexDigit) { // input a number, with index of digit to get
  short offset = pow(10, TOTAL_DIGITS - indexDigit - 1);
  return (number / offset) % 10;
}

void showNumber(short number) { // number value from 0 to 9999
  byte indexDigit = TOTAL_DIGITS - 1; // update digit from right to left is better, value from the most right changed more times than left one.
  while(delayRemain > 0) { // display 4 digits until this number timeout
    byte digitValue = getDigitValue(number, indexDigit);
    if(digitValue == 0) { // DO NOT display 0 value, saving power, focus on displaying number, digits on the right display better
      indexDigit = TOTAL_DIGITS - 1;  // update digit from right to left
      digitValue = getDigitValue(number, indexDigit);
    } 
    switchDigit(indexDigit);
    writeDigit(digitValue);
    delay(DELAY_DIGIT);
    delayRemain -= DELAY_DIGIT; // each digit display will decrease time display the number.
    indexDigit = (TOTAL_DIGITS + indexDigit - 1) % TOTAL_DIGITS; // switch to next digit on the left
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  for (short number = 0; number < 9999; number++) { // for each number, from min to max
    showNumber(number);    
    delayRemain += DELAY_NUMBER; // reset display number time
  }
}

How to make this:



Saturday, November 20, 2021

Arduino Uno - 7 segments LED display from 0 to 9 - model 5611AH

7 segment LED codes:
https://zxcongducxz.000webhostapp.com/arduino/SevenSegmentLEDDisplay.html


Arduino IDE Code:

const unsigned int DELAY_DISPLAY = 500;
const byte PINS[] = {4, 5, 10, 9, 8, 3, 2, 11};  // pins from left to right: A, B, C, D, E, F, G, DotPoint
const byte BYTES_DISPLAY[] = { // list of bytes to display from top to bottom, 1 byte = 8 bits, bit froms right to left
  B00111111, // 0
  B00000110, // 1
  B01011011, // 2
  B01001111, // 3
  B01100110, // 4
  B01101101, // 5
  B01111101, // 6
  B00000111, // 7
  B01111111, // 8
  B01101111, // 9
};

void setup() {
  // put your setup code here, to run once:
  for(byte indexPin = 0; indexPin < sizeof(PINS); indexPin++) {
    pinMode(PINS[indexPin], OUTPUT); // set pins A, B, C, D, E, F, G are out put.
  }
}

void writeDigit(byte digit) {
  for(byte indexPin = 0; indexPin < sizeof(PINS); indexPin++) { // indexPin (from left to right in array) = indexBit (from right to left in calculator)
    bool isOn = digit & (1 << indexPin); // get bit at indexPin; from right to left; value 0 = LED off = LOW; value 1 = LED on = HIGH
    digitalWrite(PINS[indexPin], isOn); // write out put pins (A, B, C, D, E, F, G, DotPoint) on or off
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  for(byte indexNumber = 0; indexNumber < sizeof(BYTES_DISPLAY); indexNumber++) {  // each display for single digit
    writeDigit(BYTES_DISPLAY[indexNumber]);    
    delay(DELAY_DISPLAY);
  }
}

How to make it: