2

I'm trying to turn a motor using An Arduino ATMega2560 with code written in Go. There's an example here that uses TinyGo v0.14.1: https://create.arduino.cc/projecthub/alankrantas/tinygo-on-arduino-uno-an-introduction-6130f6

The example in essence looks like this:

func main() {
    machine.InitPWM()
    led := machine.PWM{machine.D9}
    led.Configure()
    value := 0
    led.Set(uint16(value))
}

When I try to call machine.InitPWM() I get an error InitPWM not declared by package machine

TinyGo's current version (and the one I'm running) is v0.19. It seems as though the machine package has been modified to use PWM differently, however, I cannot find anywhere how to use it correctly.

Mor Blau
  • 420
  • 3
  • 15

2 Answers2

1

There is indeed no InitPWM function in machine package for ATMega2560 - https://tinygo.org/docs/reference/microcontrollers/machine/arduino-mega2560/

astax
  • 1,769
  • 1
  • 14
  • 23
  • I know, that's why I asked the question :) – Mor Blau Jul 23 '21 at 19:26
  • I mean that function is availabl in TinyGo for other platforms, but just wasn't implemented for yours. I guess you can either try to implement it yourself if you feel you can contribute to TinyGO, or not use TinyGo at all. It is still very early and limited, so not suprising it may not fit. – astax Jul 28 '21 at 10:24
0

You must set the machine.Timer1 in order to use pin9. The below code will do what you want except that nothing will happen because 'value' is set to 0. You must use a value between 0-256 in order to do something:

pwm := machine.Timer1
pin9 := machine.D9

err := pwm.Configure(machinePWMConfig{})
if err != nil{println(err.Error())}

ch, err := pwm.Channel(pin9)
if err != nil{println(err.Error())}

//note that values are between 0-256 for pwm:
value := uint32(0)
pwm.Set(ch, uint32(value))
Jadefox10200
  • 466
  • 1
  • 3
  • 12