Skip to content
Tactiverge

△ Motor control · loops building arrays

PWM duty-cycle table

Build a table of pulse on-times for a PWM signal and plot it.

  • Level 2 · Builder
  • 5 tests
  • technically checked

Your task

Pulse-width modulation (PWM) switches a pin on and off quickly. The duty cycle is the percentage of each period that the pin is on. A hobby servo, for example, uses a 20 000 µs period, where an on-time of 1000–2000 µs sets the angle.

Write:

  • onTime(periodUs, duty): on-time in µs = period × duty / 100.
  • dutyTable(periodUs, step): an array of { duty, onUs } objects for duty = 0, step, 2·step, … up to and including 100.

Press Run to plot the table: plot(xs, ys, label) draws a line.

Hints (0 of 2 shown)

    What gets checked

    • onTime(20000, 7.5) → 1500
    • dutyTable(1000, 25) → [{ duty: 0, onUs: 0 }, { duty: 25, onUs: 250 }, { duty: 50,…

    Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.