简体   繁体   English

BeagleBone Black-for循环中的b.pinMode

[英]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. 我最近获得了BBB,并且一直在进行实验,但是发现有时我的程序仍在后台运行,因此某些数字输出仍然设置为高电平。 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 . 我决定尝试通过编写一种复位程序来解决此问题,该程序将循环访问并将所有引脚设置为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. 根据我在设置引脚模式方面所看到的情况,这里唯一的问题是pinModefor循环内,而Cloud9不喜欢这样。

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. 代码不起作用的原因是因为您不能从引脚1开始。引脚1和2被保留,因为2接地,而1用于其他引脚。 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. 完全正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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