-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading Digital Pin State #1371
Comments
ctk |
Related: firmata/firmata.js#167. |
Pin state reading aside, there are a number of issues with the code. Let try to make helpful points here. The first issue I'm seeing is that there is no There is no place where the mode is set for Next is an issue with return values: function mcsReadBit(){
board.digitalWrite(clk_pin, 0);
precise.setTimeout(function(){
board.digitalWrite(clk_pin, 1);
var r = data_stream.value;
precise.setTimeout(function() {
console.log(r);
return r;
} , '', cycle);
}, '', cycle);
} Based on the call to precise.setTimeout(function() {
console.log(r);
return r;
} , '', cycle); Which actually means it's returned to nowhere since if(mcsReadBit()){
b |= val;
} Given the operations inside What is the source of I put together a button circuit to try this out: const five = require("johnny-five");
const NanoTimer = require("nanotimer");
const board = new five.Board();
const precise = new NanoTimer();
board.on("ready", function() {
function readByte(callback) {
const {INPUT, OUTPUT} = board.io.MODES;
const clock = 7;
const data = 4;
const cycle = "25u";
let bits = 8;
let val = 0x80;
let byte = 0;
function read() {
let bit = 0;
board.digitalWrite(clock, 0);
precise.setTimeout(() => {
board.digitalWrite(clock, 1);
bit = board.pins[data].value;
// Remove this
console.log(bit);
precise.setTimeout(() => {
if (bit) {
byte |= val;
}
val = val >> 1;
if (--bits) {
read();
} else {
callback(byte);
}
} , '', cycle);
}, '', cycle);
}
board.pinMode(clock, OUTPUT);
board.pinMode(data, INPUT);
board.digitalRead(data, _ => _);
read();
}
readByte(byte => {
console.log(byte);
});
board.repl.inject({
byte() {
readByte(byte => {
console.log(byte);
});
}
});
});
Type:
You can see when I'm holding the button and when I'm not. I have no idea if this will work for you actual use case, but it will at least serve as an illustrative tutorial w/r to receiving async pin report data. |
Ahhh, I see what you're saying there. I intentionally omitted the |
Hello there,
Right now I am rewriting an arduino library in order to be able to use it in johnny-five. I essentially have it rewritten except for the logic surrounding johnny-five's digitalRead function. My code is shown below...
Essentially what I am doing is reading a stream of bits from the sensor I am using. I cannot simply create a sensor object because I need both data and clock pins which, to my understanding, is not supported. The issue here lies in the fact that the handler function for board.digitalRead(...) does not run unless the pin state changes. Is there any way to simply just read the current state of a digital pin without major logic changes in the codebase I have?
EDIT: fixed sync vs async logic; still doesn't work
The text was updated successfully, but these errors were encountered: