|
| 1 | +// Package lia_lcd shows simple use of tinygo |
| 2 | +// to read Arduino Uno A0 pin and print value to LCD screen (hd44780, 4 bit config). |
| 3 | +// The arduino LED will blink as well. |
| 4 | +// |
| 5 | +// LCD connection scheme is same as shown at https://www.arduino.cc/en/Tutorial/LibraryExamples/HelloWorld |
| 6 | +// |
| 7 | +// E: Pin11, RS: Pin12, RW: NoPin |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "machine" |
| 12 | + "strconv" |
| 13 | + "time" |
| 14 | + |
| 15 | + // If tinygo errors when flashing due to package import error, install Go version 1.15.8, run go mod list (generate the go mod file) and reinstall Go 1.16 to be able to flash |
| 16 | + "tinygo.org/x/drivers/hd44780" |
| 17 | +) |
| 18 | + |
| 19 | +const maxUint16 uint16 = 0xffff |
| 20 | + |
| 21 | +// Arduino pins to LCD Dx pins 4 thru 7 |
| 22 | +var Dpins = [4]machine.Pin{ |
| 23 | + // Pins 4..7 on LCD are hooked to pins 5..2 on arduino board. |
| 24 | + machine.D5, machine.D4, machine.D3, machine.D2, |
| 25 | +} |
| 26 | + |
| 27 | +// Potentiometer pin shall be configured to be an ADC. We use pin A0 |
| 28 | +var pote = machine.ADC{ |
| 29 | + Pin: machine.ADC0, |
| 30 | +} |
| 31 | + |
| 32 | +func main() { |
| 33 | + // ADC configuration |
| 34 | + machine.InitADC() |
| 35 | + adcCfg := machine.ADCConfig{} |
| 36 | + pote.Configure(adcCfg) |
| 37 | + |
| 38 | + // LED will blink. Thus we configure it to be an output. |
| 39 | + machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput}) |
| 40 | + |
| 41 | + // LCD configs |
| 42 | + lcd, _ := hd44780.NewGPIO4Bit( |
| 43 | + Dpins[:], |
| 44 | + machine.D11, machine.D12, machine.NoPin, |
| 45 | + ) |
| 46 | + lcd.Configure(hd44780.Config{ |
| 47 | + Width: 16, |
| 48 | + Height: 2, |
| 49 | + CursorOnOff: false, |
| 50 | + CursorBlink: false, |
| 51 | + }) |
| 52 | + |
| 53 | + // Write tool versions at the time of writing this program |
| 54 | + lcd.ClearDisplay() |
| 55 | + lcd.Write([]byte("TinyGo0.17 Go16")) |
| 56 | + lcd.Display() |
| 57 | + |
| 58 | + var val uint8 |
| 59 | + for { |
| 60 | + // set line place to overwrite previous content |
| 61 | + lcd.SetCursor(0, 1) |
| 62 | + // read and print adc value |
| 63 | + val = adcToPcnt(pote.Get()) |
| 64 | + lcd.Write([]byte(strconv.Itoa(int(val)))) |
| 65 | + lcd.Display() |
| 66 | + // Blinky. On Arduino uno machine.LED is pin 13. (machine package specifies microchip pins, these may or may not coincide with board pin names) |
| 67 | + machine.LED.High() |
| 68 | + time.Sleep(time.Millisecond * 200) |
| 69 | + machine.LED.Low() |
| 70 | + time.Sleep(time.Millisecond * 200) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// Converts ADC uint16 value to a percent (uint8). |
| 75 | +// As of creating this program there is no full support of |
| 76 | +// 32bit floating point operations on AVR boards (Atmega boards mainly) |
| 77 | +func adcToPcnt(v uint16) uint8 { |
| 78 | + const div uint16 = maxUint16 / 100 |
| 79 | + return uint8(v / div) |
| 80 | +} |
0 commit comments