diff --git a/README.md b/README.md index a9bb608..4a06dc0 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ With this module you can drive a stepper motor in 3 lines like this ```lua stepper = require ('stepper') stepper.init({5,6,7,8}) -stepper.rotate(stepper.FORWARD,2500,5,0,function () print('Rotation done.') end) +stepper.rotate(stepper.FORWARD,2500,5,function () print('Rotation done.') end) ``` As you can see, your code trigger rotate and let the module finish rotation and trigger callback. @@ -63,7 +63,7 @@ Note : this is a async method. It'll trigger rotation and return immediately. ##### Signature : ```lua -rotate( direction, desired_steps, interval, timer_to_use, callback) +rotate( direction, desired_steps, interval, callback) ``` ##### Parameters : @@ -73,7 +73,6 @@ rotate( direction, desired_steps, interval, timer_to_use, callback) | **direction** | _stepper.FORWARD or stepper.REVERSE_ | | **desired_steps** | _number between 0 to infinity - 2500 is default which is roughly half a rotation_ | | **interval** | _time delay in milliseconds between steps, smaller self number is, faster the motor rotates . 5 is default_ | -| **timer_to_use** | _which nodemcu timer to use, 0 is default_ | | **callback** | _callback to invoke on completion of given rotation | @@ -82,9 +81,9 @@ rotate( direction, desired_steps, interval, timer_to_use, callback) ```lua direction = stepper.FORWARD desired_steps = 2500 -interval = 5 +interval = 1 timer_to_use = 0 -stepper.rotate(direction,desired_steps,interval,timer_to_use,function () +stepper.rotate(direction,desired_steps,interval,function () print('Rotation done. inside callback.') -- do some thing useful end) @@ -94,5 +93,5 @@ stepper.rotate(direction,desired_steps,interval,timer_to_use,function () ```lua stepper = require ('stepper') stepper.init({5,6,7,8}) -stepper.rotate(stepper.FORWARD,2500,5,0,function () print('Rotation done.') end) +stepper.rotate(stepper.FORWARD,2500,5,function () print('Rotation done.') end) ``` diff --git a/stepper.lua b/stepper.lua index dbdb649..9ad76ca 100644 --- a/stepper.lua +++ b/stepper.lua @@ -36,6 +36,7 @@ do -- motor configuration data --------------------------------------------------------------------------------------- local motor_params = {} + local motor_timer = tmr.create() -- nodemcu pin numbers on which to motor is connected motor_params.pins = {5,6,7,8} @@ -47,7 +48,6 @@ do motor_params.step_interval = 5 -- milliseconds decides the speed. smaller the interval, higher the speed. motor_params.desired_steps = 2500 motor_params.direction = FORWARD - motor_params.timer_to_use = 0 motor_params.callback = nil @@ -84,7 +84,10 @@ do --increment the counter and check if there are steps to execute step_counter = step_counter + 1 if step_counter > motor_params.desired_steps then - tmr.stop(motor_params.timer_to_use) + motor_timer:stop() + for index,mcu_pin in ipairs(motor_params.pins) do + gpio.write(mcu_pin, gpio.LOW) + end node.task.post(2, motor_params.callback) -- node.task.HIGH_PRIORITY = 2 else updatePhaseForNextStep(); @@ -108,7 +111,7 @@ do -- D8 < ------ > IN4 local init = function ( pins ) if not pins or not interval then - print('Init params missing !!! initializing with defaults') + --print('Init params missing !!! initializing with defaults') local motor_pins = motor_params.pins; for i,pin in ipairs( motor_pins ) do gpio.mode(pin, gpio.OUTPUT) @@ -128,17 +131,17 @@ do -- direction = stepper.FORWARD or stepper.REVERSE -- desired_steps = number between 0 to infinity - 2500 is default -- interval = time delay in milliseconds between steps, smaller self number is, faster the motor rotates . 5 is default - local rotate = function ( direction, desired_steps, interval, timer_to_use, callback) + local rotate = function ( direction, desired_steps, interval, callback) motor_params.step_interval = interval -- milliseconds decides the speed. smaller the interval, higher the speed. motor_params.desired_steps = desired_steps motor_params.direction = direction - motor_params.timer_to_use = timer_to_use motor_params.callback = callback step_counter = 0 phase = 1 - tmr.alarm(motor_params.timer_to_use, motor_params.step_interval, REPEATING_TIMER, single_step) + --tmr.alarm(motor_params.timer_to_use, motor_params.step_interval, REPEATING_TIMER, single_step) + motor_timer:alarm(motor_params.step_interval, REPEATING_TIMER, single_step) end stepper = { diff --git a/stepper_test.lua b/stepper_test.lua index fccb041..cc16b06 100644 --- a/stepper_test.lua +++ b/stepper_test.lua @@ -1,9 +1,8 @@ stepper = require ('stepper') stepper.init() desired_steps = 2500 -interval = 5 -timer_to_use = 0 +interval = 1 print('stepper.rotate() - start') -stepper.rotate(stepper.FORWARD,desired_steps,interval,timer_to_use,function () +stepper.rotate(stepper.FORWARD,desired_steps,interval,function () print('Rotation done. inside callback.') end)