Tempat: Makmal Elektrik & Elektronik 1 (MEE 1)
Masa: 2.30 pm
projek pertama kumpulan kami adalah ultrasonic sensor. Objektrif utama adalah untuk test sensor ni. sekiranya terdapat halangan di hadapan, lampu LED akan menyala, jarak kepekaan sensor adalah sejauh 10cm.
gambarajah pendawaiaan adalah seperti berikut:
code pengaturcaraan bagi sensor ini adalah:
#include // Here we are just including the NewPing Library
NewPing sonar(3, 2, 100); // This line is giving a name to the NewPing and telling the Arduino the trig, echo, max. distance
int distance; // Variable that stores the distance between the sensor and an obstacle
int triggerDistance = 10; // The distance we want the LED to be lit (10 cm or less)
unsigned int time; // Variable that stores how long it took for the ping to go, and come back
int led = 7; // Here we are giving a name to digital pin 7, which is led
void setup() {
pinMode(led, OUTPUT); // We have to tell the Arduino that the led is an OUTPUT (this could be changed to : pinMode(led, 1);)
}
void loop() {
time = sonar.ping(); // The time variable is ---> the time it took for the ultrasonic wave to go and come back
distance = time / US_ROUNDTRIP_CM; // Convert that time into distance
if(distance == 0) { // If the distance is 0
distance = 100; // The maximum distance is 100 cm
}
delay(10); // In 10 milliseconds, the sensor should be able to do the whole process
if(distance < triggerDistance) { // If there's something within 10 cm...
digitalWrite(led, HIGH); // ... Turn on the LED
}
else { // If not...
digitalWrite(led, LOW); // ... Turn off the LED
}
}
semasa melaksanakan projek ini, kami menghadapi masalah untuk mengupload code ke arduino. sekiranya anda ingin mengunakan code di atas, pertama sekali anda perlu mendapatkan library Newping di internet. copy file tu dan simpan di dalam library arduino.
Selamat mencuba...
Dibawah ini merupakan orang-orang kuat dalam projek ni.
seperti biasa, orang yang ambil gambar, memang tak de la wajahnya di dalam gambar ni.. hhuhuhu
No comments:
Post a Comment