官术网_书友最值得收藏!

Digital I/O functions - pinMode() and digitalWrite()

Digital components can be of an input or output type. A push button is an input type digital component. It does not make sense to write on a push button. LEDs and buzzers are output type digital components. We can program a digital I/O pin on BeagleBone to communicate with both the input and output type of components. However, it has to be specified first whether we are going to use that pin as an input or output pin. Once specified, the pin acts in that direction only. We will learn in detail about this in the next chapter. To blink an LED, we need to specify that we are going to use that pin as the output. Then only we can program it to turn on or off.

Debian on BeagleBone comes with a preinstalled Node.js library called BoneScript. It provides simple Arduino-like functions to interface the BeagleBone hardware. So, it becomes easier to program in BoneScript if there is prior experience in programming Arduino. The Bone101 page that we studied in the previous chapter has detailed information and many examples on BoneScript APIs. This is an open source library. You can get the source code at https://github.com/jadonk/bonescript.

Before doing the actual programming, let's learn essential functions required to communicate with the digital components. BoneScript provides you the pinMode() function to set the direction of the specified pin. The direction can be the input or output. Once the direction is set, the pin will work in that direction only. If you want to change the direction, you need to use the pinMode() function again. Here is a prototype of the pinMode() function:

pinMode(pin, direction)

Where

pin – BeagleBone pin identifier string

direction – Input or Output direction

The pinMode() function takes the name of the pin to be selected as the first argument. The second argument indicates if it is connected to the Input or Output type component. Once the Output type is specified, we can make the pin high or low. This is achieved by the BoneScript digitalWrite() function. Here is a prototype of the digitalWrite() function:

digitalWrite(pin, value)

Where

pin – BeagleBone pin identifier string

value – HIGH or LOW

This function takes the name of the selected pin as the first argument and the second argument as a HIGH or LOW value. It writes this value on the specified pin. This will be clear when we write a program using these functions. These functions are similar to the pinMode() and digitalWrite() functions from the Arduino world.

Note

Actually, the pinMode() function takes more than two arguments. However, they are optional and not required for our current goal of blinking LEDs. We will continue studying only required parts to avoid confusion throughout the book.

主站蜘蛛池模板: 宾川县| 霍城县| 田东县| 驻马店市| 泊头市| 吉安市| 石屏县| 英吉沙县| 吉林省| 永新县| 新安县| 绥宁县| 林芝县| 苗栗县| 霍城县| 洛南县| 甘谷县| 那曲县| 兴仁县| 罗江县| 宽城| 鄂托克旗| 宝应县| 达拉特旗| 杨浦区| 福建省| 扶余县| 鸡泽县| 巫溪县| 上虞市| 沙河市| 武陟县| 屏边| 随州市| 二手房| 绥化市| 射洪县| 柯坪县| 德兴市| 锦屏县| 莫力|