ultrasonic
wirng diagram

code Uno
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins int ledDelay; // delay between changes int direction = 1; int currentLED = 0; unsigned long changeTime; int potPin = 2; // select the input pin for the potentiometer void setup() { for (int x=0; x<10; x++) { // set all pins to output pinMode(ledPin[x], OUTPUT); } changeTime = millis(); } void loop() { ledDelay = analogRead(potPin); // read the value from the pot if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change changeLED(); changeTime = millis(); } } void changeLED() { for (int x=0; x<10; x++) { // turn off all LED's digitalWrite(ledPin[x], LOW); } digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED currentLED += direction; // increment by the direction value // change direction if we reach the end if (currentLED == 9) {direction = -1;} if (currentLED == 0) {direction = 1;} }
ultrasonic with lcd
code Uno
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const int trigPin = 11;
const int echoPin = 10;
const int led = 13;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
lcd.begin(16, 2);
lcd.print ("Ultrasonic ");
lcd.setCursor(0, 1);
lcd.print ("Range Meter");
delay (5000);
}
long duration, r;
float distance;
void loop()
{
lcd.clear();
lcd.print("Distance in cm");
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
long r = 3.4 * duration / 2;
float distance = r / 100.00;
lcd.setCursor(0, 1);
lcd.print(distance);
delay (300);
if(distance<10)
{
digitalWrite(led,HIGH);
}
else
{
digitalWrite(led,LOW);
}
delay(300);
}https://youtu.be/9gI_lCXdA78
Komentar
Posting Komentar