1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 树莓派HC-SR501人体红外感应

树莓派HC-SR501人体红外感应

时间:2022-09-29 14:52:06

相关推荐

树莓派HC-SR501人体红外感应

HC-SR501数据输入线接入到GPIO.1上,java代码如下:

/** #%L* *********************************************************************** ORGANIZATION : Pi4J* PROJECT : Pi4J :: Java Examples* FILENAME: GpioInputExample.java** This file is part of the Pi4J project. More information about* this project can be found here: /* *********************************************************************** %%* Copyright (C) - Pi4J* %%* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.* #L%*/import com.pi4j.io.gpio.*;import com.pi4j.platform.PlatformAlreadyAssignedException;import com.mandArgumentParser;import com.pi4j.util.Console;import com.pi4j.util.ConsoleColor;/*** This example code demonstrates how to perform simple GPIO* 这个示例代码演示了如何读取树莓派引脚状态* pin state reading on the Raspberry Pi.** @author Robert Savage*/public class GpioInputExample {/*** [ARGUMENT/OPTION "--pin (#)" | "-p (#)" ]* This example program accepts an optional argument for specifying the GPIO pin (by number)* to use with this GPIO listener example. If no argument is provided, then GPIO #1 will be used.* -- EXAMPLE: "--pin 4" or "-p 0".** [ARGUMENT/OPTION "--pull (up|down|off)" | "-l (up|down|off)" | "--up" | "--down" ]* This example program accepts an optional argument for specifying pin pull resistance.* Supported values: "up|down" (or simply "1|0"). If no value is specified in the command* argument, then the pin pull resistance will be set to PULL_UP by default.* -- EXAMPLES: "--pull up", "-pull down", "--pull off", "--up", "--down", "-pull 0", "--pull 1", "-l up", "-l down".** @param args* @throws InterruptedException* @throws PlatformAlreadyAssignedException*/public static void main(String[] args) throws InterruptedException, PlatformAlreadyAssignedException {// create Pi4J console wrapper/helper// (This is a utility class to abstract some of the boilerplate code)final Console console = new Console();// print program title/headerconsole.title("<-- The Pi4J Project -->", "GPIO Input Example");// allow for user to exit program using CTRL-Cconsole.promptForExit();// create gpio controllerfinal GpioController gpio = GpioFactory.getInstance();// by default we will use gpio pin #01; however, if an argument// has been provided, then lookup the pin by addressPin pin = CommandArgumentParser.getPin(RaspiPin.class, // pin provider class to obtain pin instance fromRaspiPin.GPIO_01, // default pin if no pin argument foundargs); // argument array to search in// by default we will use gpio pin PULL-UP; however, if an argument// has been provided, then use the specified pull resistancePinPullResistance pull = CommandArgumentParser.getPinPullResistance(PinPullResistance.PULL_UP, // default pin pull resistance if no pull argument foundargs); // argument array to search in// provision gpio pin as an input pinfinal GpioPinDigitalInput input = gpio.provisionDigitalInputPin(pin, "MyInput", pull);// set shutdown state for this pin: unexport the pininput.setShutdownOptions(true);Pin pinOut = CommandArgumentParser.getPin(RaspiPin.class, // pin provider class to obtain pin instance fromRaspiPin.GPIO_04, // default pin if no pin argument foundargs); // argument array to search in// provision gpio pin as an output pin and turn onfinal GpioPinDigitalOutput output = gpio.provisionDigitalOutputPin(pinOut, "My Output", PinState.HIGH);// set shutdown state for this pin: keep as output pin, set to low stateoutput.setShutdownOptions(false, PinState.LOW);// prompt user that we are readyconsole.println("Successfully provisioned [" + pin + "] with PULL resistance = [" + pull + "]");console.emptyLine();console.box("The GPIO input pin states will be displayed below.");console.emptyLine();for (int i = 0; i <10000 ; i++) {// display pin stateconsole.emptyLine();console.println(" [" + input.toString() + "] digital state is: " + ConsoleColor.conditional(input.getState().isHigh(), // conditional expressionConsoleColor.GREEN, // positive conditional colorConsoleColor.RED,// negative conditional colorinput.getState()));console.emptyLine();if (input.getState().isHigh()) {output.setState(PinState.HIGH);}else{output.setState(PinState.LOW);}Thread.sleep(1000);}// stop all GPIO activity/threads by shutting down the GPIO controller// (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)gpio.shutdown();}}

感应到信号后输出绿色字体

在GPIO.4上接入继电器输入信号:

这样人体靠近的时候就接通继电器。

继电器上再接上电机,这样人体靠近后,就驱动电机转了起来。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。