# 接线图
# 树莓派管脚图
# 代码
| #include <wiringPi.h> |
| #include <stdio.h> |
| |
| #define LaserPin 0 |
| |
| int main(void) |
| { |
| if(wiringPiSetup() == -1){ |
| printf("setup wiringPi failed !"); |
| return 1; |
| } |
| |
| |
| pinMode(LaserPin, OUTPUT); |
| |
| while(1){ |
| digitalWrite(LaserPin, HIGH); |
| delay(500); |
| digitalWrite(LaserPin, LOW); |
| delay(500); |
| } |
| |
| return 0; |
| } |
编译命令: gcc laser.c -o relay -lwiringPi
# Python
| |
| |
| |
| |
| |
| |
| import RPi.GPIO as GPIO |
| import time |
| |
| LedPin = 11 |
| |
| def setup(): |
| GPIO.setmode(GPIO.BOARD) |
| GPIO.setup(LedPin, GPIO.OUT) |
| GPIO.output(LedPin, GPIO.HIGH) |
| |
| def loop(): |
| while True: |
| print '...Laser on' |
| GPIO.output(LedPin, GPIO.LOW) |
| time.sleep(0.5) |
| print 'Laser off...' |
| GPIO.output(LedPin, GPIO.HIGH) |
| time.sleep(0.5) |
| |
| def destroy(): |
| GPIO.output(LedPin, GPIO.HIGH) |
| GPIO.cleanup() |
| |
| if __name__ == '__main__': |
| setup() |
| try: |
| loop() |
| except KeyboardInterrupt: |
| destroy() |