TFTBitmapLogo

Arduino TFT ekran için bu örnek bir SD karttan bir bitmap dosyasını okur ve rastgele bir yerde kullanın görüntüler.

Bu logo çalışması için "logo.bmp" adlı kartviziti SD kartın kök dizinine kaydetmeniz gerekir. SD kartın FAT16 ve FAT32 formatlı olması gerekir. SD kitaplarıyla belgelerine bakın.

Gerekli Donanım

  • Arduino Uno
  • Arduino TFT ekran
  • Mikro SD kart

Ekranı breadboard'a bağlıdır. Ekranın yan tarafındaki küçük mavi sekmeli ve oklu üstbilgiler. Ekranın yönüne dikkat edin, bu görüntülerde baş aşağı.

BL ve + 5V pinlerini güce ve GND'yi toprağa çıkıyor. CS-LD'yi pin 10'a, DC 9 pinine, RESET pin 8'e, MOSI pin 11'e, MISO pin 12'ye, SD_CS pin 4'e ve SCK 13 pin'e göre. Bir Leonardo kullanarak, farklı pimleri kullanarak. daha fazla ayrıntı için başlangıç sayfasına bakın.


Daha büyük sürüm için resimin üzerine tıklayınız

Kod

Ekranı kullanmak için önce SPI ve TFT kitaplıklarını eklemeniz gerekir. Görüntüyü karttan okumak için SD kitaplığını da eklemeniz gerekir.

#include <SD.h>
#include <SPI.h>
#include <TFT.h>
[Kodu Al]

Ekranı kontrol etmek için SD çip seçimi için bir pin tanımlayın. TFTscreen adlı bir TFT kitaplığı örneğinde.

#define sd_cs 4
#define lcd_cs 10
#define dc 9
#define rst 8

TFT TFT ekran = TFT ( cs , dc , rst ) ;

Görüntüde tutmak için PImage adı verilen özel bir veri türü vardır. PImage'ın adlandırılmış bir olduğu oluşturma

PImage logosu ;

setup() da seri bağlantı noktasını başlatacak ve başlatmadan önce etkinleşmesini bekleyeceksiniz. Durum bilgisini yok saymak için while() döngü halinde yorumlayın.

Seri iletişim başından sonra, SD kütüphanesini açar. Bir hata varsa, seri monitöre bir mesaj gönderin.

void setup ( ) {
Seri . başlar ( 9600 ) ;
while ( ! Seri ) {
}

Seri . yazdır ( "SD kart başlatılıyor ..." ) ;
Eger (! SD. baslar (SD_CS)) {
Seri . println ( "başarısız oldu!" ) ;
dönüş ;
}
Seri . println ( "Tamam!" ) ;

Ekranı başlat ve temizle

TFTscreen. begin ( ) ;
TFTscreen. arka plan ( 255 , 255 , 255 ) ;

loadimage() ile Daha Önce adlandırdığınız PImage icine Görüntü dosyasini Okuyun. loadImage() , görüntü detaylı kullanım bilgileri seri monitöre yazdırır.

logo = TFT ekran. loadImage ( "logo.bmp" ) ;
if ( ! logo. isValid ( ) ) {
Seri . println ( "arduino.bmp yüklenirken hata" ) ;
}
}

Görüntü doğru yüklenmediyse, devam etmeden önce çizimi durdurun.

void loop ( ) {
if ( logo. isValid ( ) == false ) {
dönüş ;
}

Görüntü bilgileri geçerliyse, rastgele bir nokta seçin. Tüm çizimler için bkz.

int x = rastgele ( TFT ekran. genişlik ( ) - logo. genişlik ( ) ) ;
int y = rastgele ( TFT ekran. yükseklik ( ) - logo. yükseklik ( ) ) ;

Önceki loop() girmeden önce biraz bekleyin loop()

TFTscreen. görüntü ( logo , x , y ) ;

gecikme ( 1500 ) ;
}

Çizimin listesinde aşağıdakidır:

 /* Arduino TFT Bitmap Logo example This example reads an image file from a micro-SD card and draws it on the screen, at random locations. In this sketch, the Arduino logo is read from a micro-SD card. There is a .bmp file included with this sketch. - open the sketch folder (Ctrl-K or Cmd-K) - copy the "arduino.bmp" file to a micro-SD - put the SD into the SD slot of the Arduino TFT module. This example code is in the public domain. Created 19 April 2013 by Enrico Gueli http://www.arduino.cc/en/Tutorial/TFTBitmapLogo */ // include the necessary libraries #include #include #include // Arduino LCD library // pin definition for the Uno #define sd_cs 4 #define lcd_cs 10 #define dc 9 #define rst 8 // pin definition for the Leonardo //#define sd_cs 8 //#define lcd_cs 7 //#define dc 0 //#define rst 1 TFT TFTscreen = TFT(lcd_cs, dc, rst); // this variable represents the image to be drawn on screen PImage logo; void setup() { // initialize the GLCD and show a message // asking the user to open the serial line TFTscreen.begin(); TFTscreen.background(255, 255, 255); TFTscreen.stroke(0, 0, 255); TFTscreen.println(); TFTscreen.println(F("Arduino TFT Bitmap Example")); TFTscreen.stroke(0, 0, 0); TFTscreen.println(F("Open serial monitor")); TFTscreen.println(F("to run the sketch")); // initialize the serial port: it will be used to // print some diagnostic info Serial.begin(9600); while (!Serial) { // wait for serial port to connect. Needed for native USB port only } // clear the GLCD screen before starting TFTscreen.background(255, 255, 255); // try to access the SD card. If that fails (eg // no card present), the setup process will stop. Serial.print(F("Initializing SD card...")); if (!SD.begin(sd_cs)) { Serial.println(F("failed!")); return; } Serial.println(F("OK!")); // initialize and clear the GLCD screen TFTscreen.begin(); TFTscreen.background(255, 255, 255); // now that the SD card can be access, try to load the // image file. logo = TFTscreen.loadImage("arduino.bmp"); if (!logo.isValid()) { Serial.println(F("error while loading arduino.bmp")); } } void loop() { // don't do anything if the image wasn't loaded correctly. if (logo.isValid() == false) { return; } Serial.println(F("drawing image")); // get a random location where to draw the image. // To avoid the image to be draw outside the screen, // take into account the image size. int x = random(TFTscreen.width() - logo.width()); int y = random(TFTscreen.height() - logo.height()); // draw the image to the screen TFTscreen.image(logo, x, y); // wait a little bit before drawing again delay(1500); }  /* Arduino TFT Bitmap Logo example This example reads an image file from a micro-SD card and draws it on the screen, at random locations. In this sketch, the Arduino logo is read from a micro-SD card. There is a .bmp file included with this sketch. - open the sketch folder (Ctrl-K or Cmd-K) - copy the "arduino.bmp" file to a micro-SD - put the SD into the SD slot of the Arduino TFT module. This example code is in the public domain. Created 19 April 2013 by Enrico Gueli http://www.arduino.cc/en/Tutorial/TFTBitmapLogo */ // include the necessary libraries #include #include #include // Arduino LCD library // pin definition for the Uno #define sd_cs 4 #define lcd_cs 10 #define dc 9 #define rst 8 // pin definition for the Leonardo //#define sd_cs 8 //#define lcd_cs 7 //#define dc 0 //#define rst 1 TFT TFTscreen = TFT(lcd_cs, dc, rst); // this variable represents the image to be drawn on screen PImage logo; void setup() { // initialize the GLCD and show a message // asking the user to open the serial line TFTscreen.begin(); TFTscreen.background(255, 255, 255); TFTscreen.stroke(0, 0, 255); TFTscreen.println(); TFTscreen.println(F("Arduino TFT Bitmap Example")); TFTscreen.stroke(0, 0, 0); TFTscreen.println(F("Open serial monitor")); TFTscreen.println(F("to run the sketch")); // initialize the serial port: it will be used to // print some diagnostic info Serial.begin(9600); while (!Serial) { // wait for serial port to connect. Needed for native USB port only } // clear the GLCD screen before starting TFTscreen.background(255, 255, 255); // try to access the SD card. If that fails (eg // no card present), the setup process will stop. Serial.print(F("Initializing SD card...")); if (!SD.begin(sd_cs)) { Serial.println(F("failed!")); return; } Serial.println(F("OK!")); // initialize and clear the GLCD screen TFTscreen.begin(); TFTscreen.background(255, 255, 255); // now that the SD card can be access, try to load the // image file. logo = TFTscreen.loadImage("arduino.bmp"); if (!logo.isValid()) { Serial.println(F("error while loading arduino.bmp")); } } void loop() { // don't do anything if the image wasn't loaded correctly. if (logo.isValid() == false) { return; } Serial.println(F("drawing image")); // get a random location where to draw the image. // To avoid the image to be draw outside the screen, // take into account the image size. int x = random(TFTscreen.width() - logo.width()); int y = random(TFTscreen.height() - logo.height()); // draw the image to the screen TFTscreen.image(logo, x, y); // wait a little bit before drawing again delay(1500); }  /* Arduino TFT Bitmap Logo example This example reads an image file from a micro-SD card and draws it on the screen, at random locations. In this sketch, the Arduino logo is read from a micro-SD card. There is a .bmp file included with this sketch. - open the sketch folder (Ctrl-K or Cmd-K) - copy the "arduino.bmp" file to a micro-SD - put the SD into the SD slot of the Arduino TFT module. This example code is in the public domain. Created 19 April 2013 by Enrico Gueli http://www.arduino.cc/en/Tutorial/TFTBitmapLogo */ // include the necessary libraries #include #include #include // Arduino LCD library // pin definition for the Uno #define sd_cs 4 #define lcd_cs 10 #define dc 9 #define rst 8 // pin definition for the Leonardo //#define sd_cs 8 //#define lcd_cs 7 //#define dc 0 //#define rst 1 TFT TFTscreen = TFT(lcd_cs, dc, rst); // this variable represents the image to be drawn on screen PImage logo; void setup() { // initialize the GLCD and show a message // asking the user to open the serial line TFTscreen.begin(); TFTscreen.background(255, 255, 255); TFTscreen.stroke(0, 0, 255); TFTscreen.println(); TFTscreen.println(F("Arduino TFT Bitmap Example")); TFTscreen.stroke(0, 0, 0); TFTscreen.println(F("Open serial monitor")); TFTscreen.println(F("to run the sketch")); // initialize the serial port: it will be used to // print some diagnostic info Serial.begin(9600); while (!Serial) { // wait for serial port to connect. Needed for native USB port only } // clear the GLCD screen before starting TFTscreen.background(255, 255, 255); // try to access the SD card. If that fails (eg // no card present), the setup process will stop. Serial.print(F("Initializing SD card...")); if (!SD.begin(sd_cs)) { Serial.println(F("failed!")); return; } Serial.println(F("OK!")); // initialize and clear the GLCD screen TFTscreen.begin(); TFTscreen.background(255, 255, 255); // now that the SD card can be access, try to load the // image file. logo = TFTscreen.loadImage("arduino.bmp"); if (!logo.isValid()) { Serial.println(F("error while loading arduino.bmp")); } } void loop() { // don't do anything if the image wasn't loaded correctly. if (logo.isValid() == false) { return; } Serial.println(F("drawing image")); // get a random location where to draw the image. // To avoid the image to be draw outside the screen, // take into account the image size. int x = random(TFTscreen.width() - logo.width()); int y = random(TFTscreen.height() - logo.height()); // draw the image to the screen TFTscreen.image(logo, x, y); // wait a little bit before drawing again delay(1500); }  /* Arduino TFT Bitmap Logo example This example reads an image file from a micro-SD card and draws it on the screen, at random locations. In this sketch, the Arduino logo is read from a micro-SD card. There is a .bmp file included with this sketch. - open the sketch folder (Ctrl-K or Cmd-K) - copy the "arduino.bmp" file to a micro-SD - put the SD into the SD slot of the Arduino TFT module. This example code is in the public domain. Created 19 April 2013 by Enrico Gueli http://www.arduino.cc/en/Tutorial/TFTBitmapLogo */ // include the necessary libraries #include #include #include // Arduino LCD library // pin definition for the Uno #define sd_cs 4 #define lcd_cs 10 #define dc 9 #define rst 8 // pin definition for the Leonardo //#define sd_cs 8 //#define lcd_cs 7 //#define dc 0 //#define rst 1 TFT TFTscreen = TFT(lcd_cs, dc, rst); // this variable represents the image to be drawn on screen PImage logo; void setup() { // initialize the GLCD and show a message // asking the user to open the serial line TFTscreen.begin(); TFTscreen.background(255, 255, 255); TFTscreen.stroke(0, 0, 255); TFTscreen.println(); TFTscreen.println(F("Arduino TFT Bitmap Example")); TFTscreen.stroke(0, 0, 0); TFTscreen.println(F("Open serial monitor")); TFTscreen.println(F("to run the sketch")); // initialize the serial port: it will be used to // print some diagnostic info Serial.begin(9600); while (!Serial) { // wait for serial port to connect. Needed for native USB port only } // clear the GLCD screen before starting TFTscreen.background(255, 255, 255); // try to access the SD card. If that fails (eg // no card present), the setup process will stop. Serial.print(F("Initializing SD card...")); if (!SD.begin(sd_cs)) { Serial.println(F("failed!")); return; } Serial.println(F("OK!")); // initialize and clear the GLCD screen TFTscreen.begin(); TFTscreen.background(255, 255, 255); // now that the SD card can be access, try to load the // image file. logo = TFTscreen.loadImage("arduino.bmp"); if (!logo.isValid()) { Serial.println(F("error while loading arduino.bmp")); } } void loop() { // don't do anything if the image wasn't loaded correctly. if (logo.isValid() == false) { return; } Serial.println(F("drawing image")); // get a random location where to draw the image. // To avoid the image to be draw outside the screen, // take into account the image size. int x = random(TFTscreen.width() - logo.width()); int y = random(TFTscreen.height() - logo.height()); // draw the image to the screen TFTscreen.image(logo, x, y); // wait a little bit before drawing again delay(1500); }