Last active
April 7, 2022 14:29
-
-
Save derekwheee/b01767dee020e37c25c56b4c2310f6b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Board, Stepper } = require('johnny-five'); | |
const board = new Board(); | |
const STEPS = 200; | |
const MS_PER_STEP = 12 * 60 * 60 * 1000 / STEPS; | |
let LAST_STEP = 0; | |
const getCurrentStep = (now = new Date()) => { | |
const offsetDate = new Date(now.getTime()); | |
const currentMs = offsetDate.getTime(); | |
const startMs = offsetDate.setHours(0, 0, 0, 0); | |
const elapsed = currentMs - startMs; | |
const step = Math.round(elapsed / MS_PER_STEP); | |
return -1 * Math.abs(step - STEPS) + STEPS; | |
}; | |
const program = (fn) => { | |
const currentStep = getCurrentStep(); | |
const steps = currentStep - LAST_STEP; | |
const direction = steps >= 0 ? Stepper.DIRECTION.CW : Stepper.DIRECTION.CCW; | |
fn(Math.abs(steps), direction); | |
LAST_STEP = currentStep; | |
}; | |
board.on('ready', () => { | |
const stepper = new Stepper({ | |
type: Stepper.TYPE.FOUR_WIRE, | |
stepsPerRev: STEPS, | |
pins: { | |
motor1: 10, | |
motor2: 11, | |
motor3: 12, | |
motor4: 13 | |
} | |
}); | |
const run = () => program((steps, direction) => { | |
stepper.step({ | |
steps, | |
direction | |
}); | |
}); | |
setInterval(run, 1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment