# 接线图

# 树莓派管脚图

# 代码
# C
#include <wiringPi.h> | |
#include <stdio.h> | |
#define LaserPin 0 | |
int main(void) | |
{ | |
if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen | |
printf("setup wiringPi failed !"); | |
return 1; | |
} | |
//printf("linker LedPin : GPIO %d(wiringPi pin)\n",LedPin); //when initialize wiring successfully,print message to screen | |
pinMode(LaserPin, OUTPUT); | |
while(1){ | |
digitalWrite(LaserPin, HIGH); | |
delay(500); | |
digitalWrite(LaserPin, LOW); | |
delay(500); | |
} | |
return 0; | |
} |
编译命令: gcc laser.c -o relay -lwiringPi
# Python
#!/usr/bin/env python | |
##################################################### | |
# | |
# DO NOT WATCH THE LASER DERECTELY IN THE EYE! | |
# | |
##################################################### | |
import RPi.GPIO as GPIO | |
import time | |
LedPin = 11 # pin11 | |
def setup(): | |
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location | |
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output | |
GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led | |
def loop(): | |
while True: | |
print '...Laser on' | |
GPIO.output(LedPin, GPIO.LOW) # led on | |
time.sleep(0.5) | |
print 'Laser off...' | |
GPIO.output(LedPin, GPIO.HIGH) # led off | |
time.sleep(0.5) | |
def destroy(): | |
GPIO.output(LedPin, GPIO.HIGH) # led off | |
GPIO.cleanup() # Release resource | |
if __name__ == '__main__': # Program start from here | |
setup() | |
try: | |
loop() | |
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. | |
destroy() |