Counter Number On LCD with Microbit

📟 Counter Number On LCD with Microbit

Wiring:

  • GND to micro:bit GND
  • VCC to micro:bit 3V (or 5V if using an expansion board)
  • SDA to micro:bit Pin 20
  • SCL to micro:bit Pin 19
  • ប្រសិនបើ Microbit ចេញភ្លើង 3Vអ្នកត្រូវចម្លងភ្លើងពី Power 5v ដោយភ្ជាប់ Resistor និងខ្សែរភ្ផើងមួយទៀតចូលកាន់ 3V នៃ Adapter Mcrobit ។
  • ចំណាំបើ Microbit adapter មានភ្លើង 5v ចេញនោះមិនចាប់បាច់ភ្ជាប់ Resistor ទេ ។





💻 JavaScript Code


// Increase counter when Button A is pressed
input.onButtonPressed(Button.A, function () {
    counter += 1
    updateDisplay()
})
// Decrease counter when Button B is pressed
input.onButtonPressed(Button.B, function () {
    counter += 0 - 1
    updateDisplay()
})
// Function to clear the previous number and show the new one
function updateDisplay () {
    // We print spaces first to ensure old digits don't linger
    I2C_LCD1602.ShowString("       ", 9, 0)
    I2C_LCD1602.ShowNumber(counter, 9, 0)
}
let counter = 0
// Initialize the LCD with the standard I2C address (usually 39 or 63)
I2C_LCD1602.LcdInit(39)
I2C_LCD1602.ShowString("Counter: ", 0, 0)
I2C_LCD1602.ShowNumber(counter, 0, 0)

  

Post a Comment

Previous Post Next Post