Arduino
循迹传感器
/******************************
* name: IR Tracking Sensor
* function: draw two black bold lines on the paper.
* If the rays emitted by sensor encounter thr black lines,
* the LED attached to pin 13 on the Arduino Uno board will light up;
* otherwise, it is off.
********************************/
const int trackingPin = 7;
const int ledPin = 13;
void setup()
{
pinMode(trackingPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
boolean val = digitalRead(trackingPin);
if (val == HIGH)
{
digitalWrite(ledPin, LOW); //turn off
}
else
{
digitalWrite(ledPin, HIGH); //turn on
}
}
红外线避障传感器
/******************************
* name: IR Obstacle Avoidance Sensor
* function: place a piece of paper in front of the Obstacle Avoidance Sensor,
* and the led attached tp pin 13 on the the Arduino Uno board will light up.
********************************/
const int avoidPin = 7;
const int ledPin = 13;
void setup()
{
pinMode(avoidPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
boolean val = digitalRead(avoidPin);
if (val == HIGH)
{
digitalWrite(ledPin, LOW); //turn off
}
else
{
digitalWrite(ledPin, HIGH); //turn on
}
}
树莓派
循迹传感器
C
#include <wiringPi.h>
#include <stdio.h>
#define TrackSensorPin 0
#define LedPin 1
int main(void)
{
if (wiringPiSetup() == -1){
printf("setup wiringPi failed !");
return 1;
}
pinMode(TrackSensorPin, INPUT);
pinMode(LedPin, OUTPUT);
while(1){
if (digitalRead(TrackSensorPin) == HIGH){
printf("White line is detected\n");
digitalWrite(LedPin, LOW); //led on
delay(100);
digitalWrite(LedPin, HIGH); //led off
}
else{
printf("..Black line is detected\n");
delay(100);
}
}
return 0;
}
Python
#!/usr/bin/env python
import RPi.GPIO as GPIO
TrackPin = 11
LedPin = 12
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(LedPin, GPIO.OUT)
GPIO.setup(TrackPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.output(LedPin, GPIO.HIGH)
def loop():
while True:
if GPIO.input(TrackPin) == GPIO.HIGH:
print("White line detected")
GPIO.output(LedPin, GPIO.HIGH) # led on
else:
print("...Black line detected")
GPIO.output(LedPin, GPIO.LOW) # led off
def destroy():
GPIO.output(LedPin, GPIO.LOW)
GPIO.cleanup()
if __name__ == "__main__":
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
红外线避障传感器
C
#include <wiringPi.h>
#include <stdio.h>
#define ObstaclePin 0
void myISR(void)
{
printf("Detected Barrier !\n");
}
int main(void)
{
if (wiringPiSetup() == -1){
printf("setup wiringPi failed !\n");
return 1;
}
if (wiringPiISR(ObstaclePin, INT_EDGE_FALLING, &myISR) < 0){
printf("Unable to setup ISR !!!\n");
return 1;
}
while(1){
if(digitalRead(ObstaclePin) == LOW){
myISR();
}
};
return 0;
}
Python
#!/usr/bin/env python
import RPi.GPIO as GPIO
ObstaclePin = 11
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ObstaclePin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def loop():
while True:
if (0 == GPIO.input(ObstaclePin)):
print("Detected Barrier!")
def destroy():
GPIO.cleanup()
if __name__ == '__main__':
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
网友评论