kullanılan malzemeler: 1-Arduino Mega ADK 2560 2- TFT 3.2" 320x240 Touch Screen (SSD1289) 3- 2 adet HC-SR04 ultrasonic sensor 4-UTFT kütüphanesi http://www.henningkarlsen.com/electronics/library.php?id=51 kod: /* 2 adet ultrasonic sensor ile (ön ve yan) ölçülen mesafenin LCD ekranda temsili bir grafikle anlık olarak gösterilmesi. */ #include /UTFT.h> UTFT myGLCD(ITDB32S,38,39,40,41); extern uint8_t SmallFont; int eski=0; int eski2=0; int fark=0; int fark2=0; #define echoPin2 12 // Echo Pin #define trigPin2 11 // Trigger Pin #define echoPin 2 // Echo Pin #define trigPin 3 // Trigger Pin long duration, distance; // Duration used to calculate distance long duration2, distance2; // Duration used to calculate distance extern uint8_t SevenSegNumFont; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(trigPin2, OUTPUT); pinMode(echoPin2, INPUT); myGLCD.InitLCD(); myGLCD.clrScr(); myGLCD.fillScr(VGA_WHITE); myGLCD.setColor(VGA_BLACK); // SİYAH ÇERÇEVE myGLCD.drawRect(99, 99, 221, 141); //AREA WHITE myGLCD.setColor(VGA_WHITE); myGLCD.fillRect(130, 100, 190, 119); myGLCD.setFont(SmallFont); myGLCD.setColor(VGA_BLACK); myGLCD.setBackColor(VGA_WHITE); myGLCD.printNumI(0, 158, 104); //AREA LIME myGLCD.setColor(VGA_LIME); myGLCD.fillRect(130, 120, 190, 140); myGLCD.setFont(SmallFont); myGLCD.setColor(VGA_BLACK); myGLCD.setBackColor(VGA_LIME); myGLCD.printNumI(0, 150, 124); //AREA YELLOW myGLCD.setColor(VGA_YELLOW); myGLCD.fillRect(100, 100, 129, 140); myGLCD.setFont(SmallFont); myGLCD.setColor(VGA_BLACK); myGLCD.setBackColor(VGA_YELLOW); myGLCD.printNumI(0, 111, 114); //AREA RED myGLCD.setColor(VGA_RED); myGLCD.fillRect(191, 100, 220, 140); myGLCD.setFont(SmallFont); myGLCD.setColor(VGA_BLACK); myGLCD.setBackColor(VGA_RED); myGLCD.printNumI(0, 202, 114); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration/58.2; digitalWrite(trigPin2, LOW); delayMicroseconds(2); digitalWrite(trigPin2, HIGH); delayMicroseconds(10); digitalWrite(trigPin2, LOW); duration2 = pulseIn(echoPin2, HIGH); distance2 = duration2/58.2; //---------------------------------------------------------------------------- //-------------------- DISTANCE 1 ------------------------------------ //---------------------------------------------------------------------------- if(eski99)// mesafe 99 dan büyükse grafikte bu alanı boş göster
{
myGLCD.setColor(VGA_WHITE);
myGLCD.fillRect(99, 142, 221,239);
}
if (distance < 10)
{
myGLCD.setColor(VGA_LIME);
myGLCD.fillRect(130, 120, 190, 140);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_BLACK);
myGLCD.setBackColor(VGA_LIME);
// myGLCD.printNumI(distance,158,124);
myGLCD.printNumI(distance,155,124);
myGLCD.setColor(VGA_LIME);
myGLCD.fillRect(99,(distance+141),221,239);
}
else if (distance > 9 && distance <100)
{
myGLCD.setColor(VGA_LIME);
myGLCD.fillRect(130, 120, 190, 140);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_BLACK);
myGLCD.setBackColor(VGA_LIME);
myGLCD.printNumI(distance,155,124);
myGLCD.setColor(VGA_LIME);
myGLCD.fillRect(99,(distance+141),221,239);
}
else if(distance > 99 )// Mesafe 99 dan büyük olduğu sürece ‘else if’ kodu işleyecek ekrana yazacak
{
myGLCD.setColor(VGA_LIME);
myGLCD.fillRect(130, 120, 190, 140);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_BLACK);
myGLCD.setBackColor(VGA_LIME);
//myGLCD.printNumI(distance,152,124);
myGLCD.printNumI(distance,155,124);
if (distance <100) // mesafe 100 den küçükken grafik çizdirebiliyoruz aksi halde ekrana sığmıyor grafik
{
myGLCD.setColor(VGA_LIME);
myGLCD.fillRect(99,(distance+141),221,239);
}
}
eski=distance;
//----------------------------------------------------------------------------
//-------------------- DISTANCE 2 ------------------------------------
//----------------------------------------------------------------------------
if(eski299)// mesafe 99 dan büyükse grafikte bu alanı boş göster
{
myGLCD.setColor(VGA_WHITE);
myGLCD.fillRect(222, 101, 319,140);
}
if (distance2 < 10)
{
myGLCD.setColor(VGA_RED);
myGLCD.fillRect(191, 100, 220, 140);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_BLACK);
myGLCD.setBackColor(VGA_RED);
myGLCD.printNumI(distance2,195,117);
myGLCD.setColor(VGA_RED);
myGLCD.fillRect((distance2+222),101,319,141);
}
else if (distance2 > 9 && distance2 <100)
{
myGLCD.setColor(VGA_RED);
myGLCD.fillRect(191, 100, 220, 140);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_BLACK);
myGLCD.setBackColor(VGA_RED);
myGLCD.printNumI(distance2,195,117);
myGLCD.setColor(VGA_RED);
myGLCD.fillRect((distance2+222),101,319,141);
}
else if(distance2 > 99 )
{
myGLCD.setColor(VGA_RED);
myGLCD.fillRect(191, 100, 220, 140);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_BLACK);
myGLCD.setBackColor(VGA_RED);
myGLCD.printNumI(distance2,195,117);
if (distance2 <100)
{
myGLCD.setColor(VGA_RED);
myGLCD.fillRect((distance2+222),101,319,141);
}
}
eski2=distance2;
delay(100);
}