Arduino-Processing Ultrasonik Sensör Uygulaması

0 1.118

Processing arayüz geliştirici ile ultrasonik sensörden aldığımız mesafeleri görsel olarak izleyeceğiz.Hc-04 mesafe sensörü birçok uygulamalarda kolaylıkla kullanabiliceğemiz sensörlerden biridir.Bu projede de rahatlıkla kullanabiliriz.

 

Aşağıdaki şekildeki gibi bağlantılarımızı yapalım.

 

 

 

Arduino Kodu:

#define echoPin 11
#define trigPin 12
#define LEDPin 13
 
int maximumRange = 200;
int minimumRange = 0;
long duration, distance;
 
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT);
}
 
void loop() {
 
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
 
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
 
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
 
if (distance >= maximumRange || distance <= minimumRange){
 
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
}
else {
 
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
 
delay(50);
}

Processing Kodu:

import processing.serial.*;
int numOfShapes = 60;
int shapeSpeed = 2;
 
Square[] mySquares = new Square[numOfShapes];
int shapeSize, distance;
String comPortString;
Serial myPort;
 
void setup(){
size(displayWidth,displayHeight);
smooth();
 
shapeSize = (width/numOfShapes);
for(int i = 0; i<numOfShapes; i++){
mySquares[i]=new Square(int(shapeSize*i),height-40);
}
 
myPort = new Serial(this, "COM8", 9600);
myPort.bufferUntil('\n');
}
 
void draw(){
background(0);
delay(50);
drawSquares();
}
 
void serialEvent(Serial cPort){
comPortString = cPort.readStringUntil('\n');
if(comPortString != null) {
comPortString=trim(comPortString);
 
distance = int(map(Integer.parseInt(comPortString),1,200,1,height));
if(distance<0){
 
distance = 0;
}
}
}
 
void drawSquares(){
int oldY, newY, targetY, redVal, blueVal;
 
mySquares[0].setY((height-shapeSize)-distance);
 
for(int i = numOfShapes-1; i>0; i--){
 
targetY=mySquares[i-1].getY();
oldY=mySquares[i].getY();
 
if(abs(oldY-targetY)<2){
newY=targetY;
}else{
newY=oldY-((oldY-targetY)/shapeSpeed);
}
mySquares[i].setY(newY);
 
blueVal = int(map(newY,0,height,0,255));
redVal = 255-blueVal;
fill(redVal,0,blueVal);
rect(mySquares[i].getX(), mySquares[i].getY(),shapeSize,shapeSize);
}
}
 
boolean sketchFullScreen() {
return true;
}
class Square{
int xPosition, yPosition;
 
Square(int xPos, int yPos){
xPosition = xPos;
yPosition = yPos;
}
 
int getX(){
return xPosition;
}
 
int getY(){
return yPosition;
}
 
void setY(int yPos){
yPosition = yPos;
}
}
Bunlar da İlgini Çekebilir

Cevap bırakın

E-posta hesabınız yayımlanmayacak.