~距離センサーに近づいたら画像を表示する~
距離センサーの前を人が横切ると、画面にムサシくんの画像が表示されます!
目次
int sensors[2];
int inByte;
int _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);
if (duration > 0) {
distance = duration / 2 * 340 * 500 / 1000000;
}
return distance;
}
void setup(){
Serial.begin(9600);
//pinMode(A5, INPUT);
pinMode(A1, INPUT);
digitalWrite(18, LOW);
sensors[0] = 0;
sensors[1] = 0;
inByte = 0;
establishContact();
}
void loop(){
if (Serial.available() > 0) {
inByte = Serial.read();
//sensors[0] = analogRead(A5);
sensors[0] = _sbeGetDistanceUsingDistanceSensor(18 , 19);
sensors[1] = analogRead(A1);
Serial.print(sensors[0]);
Serial.print(",");
Serial.println(sensors[1]);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("通信中");
delay(300);
}
}
import processing.serial.*;
Serial myPort;
PImage img;
float fillColor;
float diameter;
float x = 460;
float y = 460;
int spin = 0;
float rad;
void setup() {
size(940, 980);
myPort = new Serial(this, Serial.list()[4], 9600);
myPort.bufferUntil('\n');
img = loadImage("musashi_face.png");
}
void draw() {
background(255);
imageMode(CENTER);
translate(x, y);
image(img, 0, 0, diameter, diameter);
}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
myString = trim(myString);
int sensors[] = int(split(myString, ','));
if (sensors.length > 1) {;
if (sensors[0] < 50){
diameter = 900;
delay(3000);
diameter = 0;
}
}
myPort.write("A");
}
キャロット
このページのHelloNoは、050008 です!