Fikri Wibowo
Published © CC BY

Digital Ruler

merupakan alat untuk mengukur panjang suatu benda secara digital

EasyProtip24 hours159
Digital Ruler

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

skema

Orkom2 laywennuef

Code

code programny digiital ruler

Arduino
const int trigPin = 10;
const int echoPin = 9;
int lampu = 13 ;

void setup() {
  Serial.begin(9600);
  pinMode(lampu, OUTPUT); 
}

long duration, inches, cm;
 
void loop()
{
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  inches = microsecondsKeInchi(duration);
  cm = microsecondsKeCenti(duration);
  
  Serial.print(inches);
  Serial.print(" in, ");
  Serial.print(cm);
  Serial.print(" cm");
  Serial.println();

  lampuNyala() ; 
 
  
  delay(10);
}

long microsecondsKeInchi(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsKeCenti(long microseconds)
{
  return microseconds / 29 / 2;
}

void lampuNyala(){
   if(cm > 0 && cm <=30){
      digitalWrite(lampu,HIGH) ; 
    }
    else{
      digitalWrite(lampu,LOW) ; 
      }
  }

Credits

Fikri Wibowo

Fikri Wibowo

1 project • 0 followers
"nothing impossible"
Contact

Comments

Add projectSign up / Login