~距離センサーに近づいたら画像を表示する~
距離センサーの前を人が横切ると、画面にムサシくんの画像が表示されます!
目次
unsigned long distanceTimeOut = 100000;
float _sbeGetDistanceUsingDistanceSensor(int trigPin, int echoPin) {
long distance = 0;
long duration = 0;
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH, distanceTimeOut);
if (duration > 0) {
distance = duration / 2 * 340 * 100 / 1000000;
}
return distance;
}
void setup() {
Serial.begin(9600);
digitalWrite(18, LOW);
}
void loop() {
int analogValue = int(_sbeGetDistanceUsingDistanceSensor(18 , 19));
byte analog_highByte = (analogValue >> 8) & 0xFF;
byte analog_lowByte = analogValue & 0xFF;
Serial.write(analog_highByte);
Serial.write(analog_lowByte);
}
add_library('serial')
myPort = None
available_serialport = 2
arduinoPort = Serial.list()[available_serialport]
x = 460
y = 460
sensingValue = 0
diameter = 0
distance = 50
def setup():
global myPort, img
size(940, 980)
myPort = Serial(this, arduinoPort, 9600)
img = loadImage("musashi_face.png")
def draw():
background(255)
imageMode(CENTER)
translate(x, y)
image(img, 0, 0, diameter, diameter)
def serialEvent(myPort):
global sensingValue, diameter
if myPort.available() >= 2:
analog_highByte = myPort.read()
analog_lowByte = myPort.read()
sensingValue = (analog_highByte << 8) | analog_lowByte
println(sensingValue)
if sensingValue <= distance:
diameter = 900
delay(3000)
diameter = 0
キャロット
このページのHelloNoは、050018 です!