Advertisement
Guest User

TFT+Sonar_Arduino

a guest
Dec 26th, 2014
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <UTFT.h>
  2. #include <NewPing.h>
  3.  
  4. #define TRIGGER_PIN  5  // Arduino pin tied to trigger pin on the ultrasonic sensor.
  5. #define ECHO_PIN     3  // Arduino pin tied to echo pin on the ultrasonic sensor.
  6. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  7.  
  8. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  9.  
  10. //for QD_TFT180X SPI LCD Modle
  11. //Param1:Value Can be:QD_TFT180A/QD_TFT180B/QD_TFT180C
  12. //Param2 instructions:Connect to LCD_Pin #4 SDA/SDI/MOSI(it means LCD_Model Pin_SDA/SDI/MOSI Connect to Arduino_UNO Pin11)
  13. //Param3 instructions:Connect to LCD_Pin #5 SCL/CLK/SCLK(it means LCD_Model Pin_SCL/CLK/SCLK Connect to Arduino_UNO Pin10)
  14. //Param4 instructions:Connect to LCD_Pin #2 CS/CE(it means LCD_Model Pin_CS/CE Connect to Arduino_UNO Pin9)
  15. //Param5 instructions:Connect to LCD_Pin #1 RST/RESET(it means LCD_Model Pin_RST/RESET Connect to Arduino_UNO Pin12)
  16. //Param6 instructions:Connect to LCD_Pin #3 RS/DC(it means LCD_Model Pin_RS/DC Connect to Arduino_UNO Pin8)
  17. //                                       #6 & #7 to 5V
  18. //                                       #8 to GND
  19. UTFT myGLCD(QD_TFT180A,11,10,9,12,8);   // Remember to change the model parameter to suit your display module!
  20.  
  21. // Declare which fonts we will be using
  22. extern uint8_t SmallFont[];
  23. extern uint8_t SevenSegNumFont[];
  24.  
  25. void setup()
  26. {
  27. // Setup the LCD
  28.   myGLCD.InitLCD();   // initiaise the screen;
  29.   myGLCD.setFont(SmallFont);   // use the SmallFont (also available: BigFont and SevenSegNumFont)
  30.   myGLCD.clrScr();   // clear the screen
  31.   myGLCD.setColor(255, 255, 255);  // set colour white (NOTE: BGR and not RGB)
  32.   myGLCD.print("Distance in cm:",CENTER, 1);   // print header 1 pixel down and at center
  33. }
  34.  
  35. void loop()
  36. {
  37.   delay(50);  // delay for sonar ping
  38.   int uS = sonar.ping_cm();  // uS will be the ping distance in CM
  39.  
  40.   Serial.begin(9600);
  41.  
  42.   Serial.println(uS);  // serial print the distance
  43.   myGLCD.setColor(255,255,0);  // set colour to light blue
  44.   myGLCD.setFont(SevenSegNumFont);  // use SevenSegNumFont
  45.   myGLCD.print(String(uS)+String(" "), LEFT, 24);  // print distance as a string and add a space to eraase pevious reading
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement