From ca88faa7ca97ecdb322067d07ce97bcedee26e65 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Tue, 22 Oct 2019 22:05:49 +0100 Subject: [PATCH] Fix arduino nodes servo/pwm output to reduce jitter to close #584 --- hardware/Arduino/35-arduino.js | 14 +++++++------- hardware/Arduino/package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hardware/Arduino/35-arduino.js b/hardware/Arduino/35-arduino.js index 465fe007..fc3ffe1f 100644 --- a/hardware/Arduino/35-arduino.js +++ b/hardware/Arduino/35-arduino.js @@ -76,9 +76,11 @@ module.exports = function(RED) { node.status({fill:"grey",shape:"ring",text:"node-red:common.status.connecting"}); var doit = function() { node.running = true; + if (node.state === "ANALOG") { node.board.pinMode(node.pin, 0x02); } + if (node.state === "INPUT") { node.board.pinMode(node.pin, 0x00); } + if (node.state === "PULLUP") { node.board.pinMode(node.pin, 0x0B); } node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); if (node.state === "ANALOG") { - node.board.pinMode(node.pin, 0x02); node.board.analogRead(node.pin, function(v) { if (v !== node.oldval) { node.oldval = v; @@ -87,7 +89,6 @@ module.exports = function(RED) { }); } if (node.state === "INPUT") { - node.board.pinMode(node.pin, 0x00); node.board.digitalRead(node.pin, function(v) { if (v !== node.oldval) { node.oldval = v; @@ -96,7 +97,6 @@ module.exports = function(RED) { }); } if (node.state === "PULLUP") { - node.board.pinMode(node.pin, 0x0B); node.board.digitalRead(node.pin, function(v) { if (v !== node.oldval) { node.oldval = v; @@ -149,11 +149,13 @@ module.exports = function(RED) { node.status({fill:"grey",shape:"ring",text:"node-red:common.status.connecting"}); var doit = function() { node.running = true; + if (node.state === "OUTPUT") { node.board.pinMode(node.pin, 0x01); } + if (node.state === "PWM") { node.board.pinMode(node.pin, 0x03); } + if (node.state === "SERVO") { node.board.pinMode(node.pin, 0x04); } node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); node.on("input", function(msg) { if (node.board.isReady) { if (node.state === "OUTPUT") { - node.board.pinMode(node.pin, 0x01); if ((msg.payload === true)||(msg.payload.toString() == "1")||(msg.payload.toString().toLowerCase() == "on")) { node.board.digitalWrite(node.pin, node.board.HIGH); } @@ -161,15 +163,13 @@ module.exports = function(RED) { node.board.digitalWrite(node.pin, node.board.LOW); } } - if (node.state === "PWM") { - node.board.pinMode(node.pin, 0x03); + if (node.state === "PWM") { msg.payload = parseInt((msg.payload * 1) + 0.5); if ((msg.payload >= 0) && (msg.payload <= 255)) { node.board.analogWrite(node.pin, msg.payload); } } if (node.state === "SERVO") { - node.board.pinMode(node.pin, 0x04); msg.payload = parseInt((msg.payload * 1) + 0.5); if ((msg.payload >= 0) && (msg.payload <= 180)) { node.board.servoWrite(node.pin, msg.payload); diff --git a/hardware/Arduino/package.json b/hardware/Arduino/package.json index c919cabb..0e325ac9 100644 --- a/hardware/Arduino/package.json +++ b/hardware/Arduino/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-arduino", - "version" : "0.2.3", + "version" : "0.2.4", "description" : "A Node-RED node to talk to an Arduino running firmata", "dependencies" : { "firmata" : "^2.0.0"