简体   繁体   中英

BeagleBone Black - b.pinMode in for loop

I recently got a BBB and have been experimenting, but have found out that sometimes my programs still run in the background, so some digital outs are still set to high. I decided to try to fix that by writing a sort of reset program that would iterate through and set all the pins to b.LOW . My code is as follows:

var b = require("bonescript");

var port = "P8_0";

for(var i = 0; i < 46; i++){
    var j = i + 1;
    port = port.substring(0, 3) + j;
    b.pinMode(port, b.OUTPUT);
    b.digitalWrite(port, b.LOW);
}

Here's where it gets interesting. I'm getting the following error:

/usr/lib/node_modules/bonescript/index.js:195
    throw('Invalid pin object for pinMode: ' + pin);
                                            ^
Invalid pin object for pinMode: [object Object]

Based on what I've seen in terms of setting the pin mode, the only thing that is the problem here is that pinMode is inside the for loop, and that Cloud9 doesn't like that.

Okay, so as I was typing the question, I had a brainwave hit me. The reason the code doesn't work is because you can't start from pin 1. Pins 1 and 2 are reserved, because 2 is ground, and 1 is for something else. So if we change the code to this:

var b = require("bonescript");

var port = "P8_0";

for(var i = 2; i < 46; i++){
    var j = i + 1;
    port = port.substring(0, 3) + j;
    b.pinMode(port, b.OUTPUT);
    b.digitalWrite(port, b.LOW);
}

Works totally fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM