skizziks_53

Reddit screen u-sonic attempt v1.0

May 24th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.75 KB | None | 0 0
  1. /*
  2.    Reddit arduino help --- May 24, 2019
  3.   Version 1.0
  4.  
  5.   I don't have the hardware to test this sketch, so these are just guesses...
  6.  
  7. */
  8.  
  9.  
  10. #include <Adafruit_GFX.h>
  11. #include <SPI.h>
  12. #include <Wire.h>
  13. #include <Adafruit_ILI9341.h>
  14. #include <Adafruit_STMPE610.h>
  15. #include <Servo.h>
  16.  
  17.  
  18. //#define TS_MINX 4000 --------------------------- This line is wrong.
  19. //#define TS_MINY 4000 --------------------------- This line is wrong.
  20. #define TS_MINX 150 // --------------------------- This line is a guess, but should be pretty close to correct.
  21. #define TS_MINY 150 // --------------------------- This line is a guess, but should be pretty close to correct.
  22. #define TS_MAXX 4000
  23. #define TS_MAXY 4000
  24. /*
  25.    The above four lines are supposed to be the minimum X, minimum Y, maximum X and maximum Y values for the touch screen.
  26.    You had the minimums and the maximums set to the same numbers.
  27.    Adafruit has a test program that allows you to discover the real minimum and maximum values.
  28. */
  29.  
  30.  
  31.  
  32. #define ST7735_BLACK 0x0000
  33. #define ST7735_GRAY 0x8410
  34. #define ST7735_WHITE 0xFFFF
  35. #define ST7735_RED 0xF800
  36. #define ST7735_ORANGE 0xFA60
  37. #define ST7735_YELLOW 0xFFE0
  38. #define ST7735_LIME 0x07FF
  39. #define ST7735_GREEN 0x07E0
  40. #define ST7735_CYAN 0x07FF
  41. #define ST7735_AQUA 0x04FF
  42. #define ST7735_BLUE 0x001F
  43. #define ST7735_MAGENTA 0xF81F
  44. #define ST7735_PINK 0xF8FF
  45. /*
  46.    You don't really need to declare the above colors, since there is already a bunch of them declared in the Adafruit_ILI9341.h file:
  47.  
  48.   // Color definitions
  49.   #define ILI9341_BLACK       0x0000  ///<   0,   0,   0
  50.   #define ILI9341_NAVY        0x000F  ///<   0,   0, 123
  51.   #define ILI9341_DARKGREEN   0x03E0  ///<   0, 125,   0
  52.   #define ILI9341_DARKCYAN    0x03EF  ///<   0, 125, 123
  53.   #define ILI9341_MAROON      0x7800  ///< 123,   0,   0
  54.   #define ILI9341_PURPLE      0x780F  ///< 123,   0, 123
  55.   #define ILI9341_OLIVE       0x7BE0  ///< 123, 125,   0
  56.   #define ILI9341_LIGHTGREY   0xC618  ///< 198, 195, 198
  57.   #define ILI9341_DARKGREY    0x7BEF  ///< 123, 125, 123
  58.   #define ILI9341_BLUE        0x001F  ///<   0,   0, 255
  59.   #define ILI9341_GREEN       0x07E0  ///<   0, 255,   0
  60.   #define ILI9341_CYAN        0x07FF  ///<   0, 255, 255
  61.   #define ILI9341_RED         0xF800  ///< 255,   0,   0
  62.   #define ILI9341_MAGENTA     0xF81F  ///< 255,   0, 255
  63.   #define ILI9341_YELLOW      0xFFE0  ///< 255, 255,   0
  64.   #define ILI9341_WHITE       0xFFFF  ///< 255, 255, 255
  65.   #define ILI9341_ORANGE      0xFD20  ///< 255, 165,   0
  66.   #define ILI9341_GREENYELLOW 0xAFE5  ///< 173, 255,  41
  67.   #define ILI9341_PINK        0xFC18  ///< 255, 130, 198
  68.  
  69.   When you use a library for a screen driver chip, try looking in the .h and .cpp files. There may be a bunch of standard colors already defined.
  70.   The UTFT library has them in the UTFT.h file.
  71.  
  72.   Warning! ---- some libraries use different values for transparency. Pretty much all small display screen chips use 5-6-5 color format however.
  73. */
  74. //  -------- Also you can create your own custom colors from R,G,B bytes like so (below is untested, but should work?)...
  75. word createCustomColor(int r, int g, int b) {
  76.   word tempColor = ((r & 248) << 8 | (g & 252) << 3 | (b & 248) >> 3);
  77.   return tempColor;
  78. }
  79. // The function above takes three int values (all must be from zero to 255!) and returns a word with the colors compressed into 5-6-5 format.
  80. // https://www.arduino.cc/reference/en/language/variables/data-types/word/
  81. // (a "word" variable type is basically a way to use one variable for more than one value, in some custom-defined format)
  82.  
  83. // What is these color words??? ...Most small TFT screen driver chips don't really accept 255-bit color levels; they use a two-byte word and store the red value as 5 bits, the green as 6 bits, and the blue as 5 bits.
  84. // So it ends up being three different color values that are stored in only two bytes.
  85. // Many other graphics libraries let you specify screen colors in RGB (0-255) values and it converts them automatically. The Adafruit library doesn't seem to include that function.
  86. // To learn more, search Google for "convert RGB to 5-6-5 format"
  87.  
  88.  
  89.  
  90.  
  91.  
  92. #define STMPE_CS 8
  93. Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
  94. #define TFT_CS 10
  95. #define TFT_DC 9
  96. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  97.  
  98.  
  99. //#define FRAME_X -500
  100. //#define FRAME_Y -500
  101. /*
  102.    The above two lines are wrong.
  103.    These four values are supposed to be the graphical (pixel) screen limits. So the upper-left corner is always zero.
  104.  
  105.    You can *request* the GFX library to draw an object that has end points that are negative, but it should ignore any points that lie off the screen.
  106.    So the errors you are getting may be caused by the GFX library trying to draw a pixel at -500,-500, that does not really exist in the hardware.
  107. */
  108. #define FRAME_X 0 // --------- corrected line
  109. #define FRAME_Y 0 // --------- corrected line
  110. #define FRAME_W 3800
  111. #define FRAME_H 4000
  112.  
  113. /*
  114.    It is also useful to point out here:
  115.    the TOUCH screen uses one set of X-Y minimum and maximum values,
  116.    and the GRAPHICS screen uses another set of X-Y minimum and maximum values.
  117.    The two are never the same.
  118.    The touch screen is using whatever resistances the screen film can generate.
  119.    The GFX screen is using the actual pixel counts of the screen itself.
  120. */
  121.  
  122.  
  123.  
  124. int trigPin = 22; // Trigger of HC-SR04 sensor
  125. int echoPin = 53; // Echo of HC-SR04 sensor
  126. long duration, cm, x;
  127. int number;
  128.  
  129.  
  130. void Frame1() // How Frame 1 needs to be drawn, this works perfect, the problem lies further down in the code
  131. {
  132.   tft.fillRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ST7735_BLACK);
  133.   tft.setCursor(FRAME_X + 622, FRAME_Y + 602);
  134.   tft.setTextColor(ILI9341_RED);
  135.   tft.setTextSize(6);
  136.   tft.println("-");
  137.   tft.setCursor(FRAME_X + 620, FRAME_Y + 600);
  138.   tft.setTextColor(ILI9341_WHITE);
  139.   tft.setTextSize(6);
  140.   tft.println("-");
  141.   delay(100);
  142.   tft.setCursor(FRAME_X + 622, FRAME_Y + 602);
  143.   tft.setTextColor(ILI9341_RED);
  144.   tft.setTextSize(6);
  145.   tft.println("--");
  146.   tft.setCursor(FRAME_X + 620, FRAME_Y + 600);
  147.   tft.setTextColor(ILI9341_WHITE);
  148.   tft.setTextSize(6);
  149.   tft.println("--");
  150.   delay(100);
  151.   tft.setCursor(FRAME_X + 622, FRAME_Y + 602);
  152.   tft.setTextColor(ILI9341_RED);
  153.   tft.setTextSize(6);
  154.   tft.println("-->");
  155.   tft.setCursor(FRAME_X + 620, FRAME_Y + 600);
  156.   tft.setTextColor(ILI9341_WHITE);
  157.   tft.setTextSize(6);
  158.   tft.println("-->");
  159.   return;
  160. }
  161.  
  162.  
  163.  
  164. void Frame2() // How Frame 2 needs to be drawn, this works perfect, the problem lies further down in the code
  165. {
  166.   tft.fillRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ST7735_BLACK);
  167.   tft.setCursor(FRAME_X + 542, FRAME_Y + 602);
  168.   tft.setTextColor(ILI9341_RED);
  169.   tft.setTextSize(6);
  170.   tft.println("70 KM/H");
  171.   tft.setCursor(FRAME_X + 540, FRAME_Y + 600);
  172.   tft.setTextColor(ILI9341_WHITE);
  173.   tft.setTextSize(6);
  174.   tft.println("70 KM/H");
  175.   return;
  176. }
  177.  
  178.  
  179.  
  180. void Frame3() // How Frame 3 needs to be drawn, this works perfect, the problem lies further down in the code
  181. {
  182.   tft.fillRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ST7735_BLACK);
  183.   tft.setCursor(FRAME_X + 659, FRAME_Y + 593);
  184.   tft.setTextColor(ILI9341_RED);
  185.   tft.setTextSize(7);
  186.   tft.println("^");
  187.   tft.setCursor(FRAME_X + 659, FRAME_Y + 595);
  188.   tft.setTextColor(ILI9341_RED);
  189.   tft.setTextSize(7);
  190.   tft.println("|");
  191.   tft.setCursor(FRAME_X + 659, FRAME_Y + 602);
  192.   tft.setTextColor(ILI9341_RED);
  193.   tft.setTextSize(7);
  194.   tft.println("|");
  195.   tft.setCursor(FRAME_X + 658, FRAME_Y + 592);
  196.   tft.setTextColor(ILI9341_WHITE);
  197.   tft.setTextSize(7);
  198.   tft.println("^");
  199.   tft.setCursor(FRAME_X + 658, FRAME_Y + 594);
  200.   tft.setTextColor(ILI9341_WHITE);
  201.   tft.setTextSize(7);
  202.   tft.println("|");
  203.   tft.setCursor(FRAME_X + 658, FRAME_Y + 601);
  204.   tft.setTextColor(ILI9341_WHITE);
  205.   tft.setTextSize(7);
  206.   tft.println("|");
  207.   // return 0; -------------------------------------------- this line is incorrect, as you are returning a value (zero) but none of the calls for this function are set to accept any return value.
  208.   //return; // ---------------------------------------------- Also FYI: you don't really need to put the return line unless you are actually returning a value or unless you want to exit the function early.
  209. }
  210.  
  211.  
  212. void setup(void)
  213. {
  214.   Serial.begin (9600);
  215.   Serial.println("inside 'void setup(void) loop");
  216.   delay(500);
  217.   pinMode(trigPin, OUTPUT);
  218.   pinMode(echoPin, INPUT);
  219.   // Serial.begin(9600); -------------------------------------- already did this in the first line...
  220.   tft.begin();
  221.   if (!ts.begin()) {
  222.     Serial.println("Unable to start touchscreen.");
  223.   }
  224.   else {
  225.     Serial.println("Touchscreen started.");
  226.   }
  227.  
  228.   tft.fillScreen(ILI9341_BLACK);
  229.   // origin = left,top landscape (USB left upper)
  230.   tft.setRotation(1);
  231.  
  232.   delay(500);
  233.   Frame1(); // -------------------------------------------------- why are you drawing all three screens in rapid-fire here???
  234.   Frame2();
  235.   Frame3();
  236.  
  237. }
  238.  
  239.  
  240. void loop()
  241. {
  242.   Serial.println("inside 'void loop()' loop");
  243.   delay(500);
  244.   digitalWrite(trigPin, LOW);
  245.   delayMicroseconds(5);
  246.   digitalWrite(trigPin, HIGH);
  247.   delayMicroseconds(10);
  248.   digitalWrite(trigPin, LOW);
  249.   pinMode(echoPin, INPUT);
  250.   duration = pulseIn(echoPin, HIGH);
  251.   int cm = (duration / 2) / 29.1;
  252.   int x = cm;
  253.   Serial.println("setting up integers \n");
  254.   delay(500);
  255.   if (x < 50) {
  256.     Serial.println("inside 'do while' loop \n");
  257.     Serial.println("drawing Frame2 \n");
  258.     Frame2(); // This line f*cks everything up, please help me :)
  259.     delay(500);
  260.     Serial.println("drawing Frame1 \n");
  261.     Frame1(); // This line f*cks everything up, please help me :)
  262.   }
  263.   else {
  264.     Serial.println("drawing Frame3 \n");
  265.     Frame3(); // This line f*cks everything up, please help me :)
  266.     delay(500);
  267.   }
  268.  
  269.   Serial.print(cm);
  270.   Serial.print("cm \n");
  271.   delay(250);
  272.  
  273. }
Add Comment
Please, Sign In to add comment