Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit arduino help --- May 24, 2019
- Version 1.0
- I don't have the hardware to test this sketch, so these are just guesses...
- */
- #include <Adafruit_GFX.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_ILI9341.h>
- #include <Adafruit_STMPE610.h>
- #include <Servo.h>
- //#define TS_MINX 4000 --------------------------- This line is wrong.
- //#define TS_MINY 4000 --------------------------- This line is wrong.
- #define TS_MINX 150 // --------------------------- This line is a guess, but should be pretty close to correct.
- #define TS_MINY 150 // --------------------------- This line is a guess, but should be pretty close to correct.
- #define TS_MAXX 4000
- #define TS_MAXY 4000
- /*
- The above four lines are supposed to be the minimum X, minimum Y, maximum X and maximum Y values for the touch screen.
- You had the minimums and the maximums set to the same numbers.
- Adafruit has a test program that allows you to discover the real minimum and maximum values.
- */
- #define ST7735_BLACK 0x0000
- #define ST7735_GRAY 0x8410
- #define ST7735_WHITE 0xFFFF
- #define ST7735_RED 0xF800
- #define ST7735_ORANGE 0xFA60
- #define ST7735_YELLOW 0xFFE0
- #define ST7735_LIME 0x07FF
- #define ST7735_GREEN 0x07E0
- #define ST7735_CYAN 0x07FF
- #define ST7735_AQUA 0x04FF
- #define ST7735_BLUE 0x001F
- #define ST7735_MAGENTA 0xF81F
- #define ST7735_PINK 0xF8FF
- /*
- 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:
- // Color definitions
- #define ILI9341_BLACK 0x0000 ///< 0, 0, 0
- #define ILI9341_NAVY 0x000F ///< 0, 0, 123
- #define ILI9341_DARKGREEN 0x03E0 ///< 0, 125, 0
- #define ILI9341_DARKCYAN 0x03EF ///< 0, 125, 123
- #define ILI9341_MAROON 0x7800 ///< 123, 0, 0
- #define ILI9341_PURPLE 0x780F ///< 123, 0, 123
- #define ILI9341_OLIVE 0x7BE0 ///< 123, 125, 0
- #define ILI9341_LIGHTGREY 0xC618 ///< 198, 195, 198
- #define ILI9341_DARKGREY 0x7BEF ///< 123, 125, 123
- #define ILI9341_BLUE 0x001F ///< 0, 0, 255
- #define ILI9341_GREEN 0x07E0 ///< 0, 255, 0
- #define ILI9341_CYAN 0x07FF ///< 0, 255, 255
- #define ILI9341_RED 0xF800 ///< 255, 0, 0
- #define ILI9341_MAGENTA 0xF81F ///< 255, 0, 255
- #define ILI9341_YELLOW 0xFFE0 ///< 255, 255, 0
- #define ILI9341_WHITE 0xFFFF ///< 255, 255, 255
- #define ILI9341_ORANGE 0xFD20 ///< 255, 165, 0
- #define ILI9341_GREENYELLOW 0xAFE5 ///< 173, 255, 41
- #define ILI9341_PINK 0xFC18 ///< 255, 130, 198
- 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.
- The UTFT library has them in the UTFT.h file.
- Warning! ---- some libraries use different values for transparency. Pretty much all small display screen chips use 5-6-5 color format however.
- */
- // -------- Also you can create your own custom colors from R,G,B bytes like so (below is untested, but should work?)...
- word createCustomColor(int r, int g, int b) {
- word tempColor = ((r & 248) << 8 | (g & 252) << 3 | (b & 248) >> 3);
- return tempColor;
- }
- // 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.
- // https://www.arduino.cc/reference/en/language/variables/data-types/word/
- // (a "word" variable type is basically a way to use one variable for more than one value, in some custom-defined format)
- // 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.
- // So it ends up being three different color values that are stored in only two bytes.
- // 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.
- // To learn more, search Google for "convert RGB to 5-6-5 format"
- #define STMPE_CS 8
- Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
- #define TFT_CS 10
- #define TFT_DC 9
- Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
- //#define FRAME_X -500
- //#define FRAME_Y -500
- /*
- The above two lines are wrong.
- These four values are supposed to be the graphical (pixel) screen limits. So the upper-left corner is always zero.
- 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.
- 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.
- */
- #define FRAME_X 0 // --------- corrected line
- #define FRAME_Y 0 // --------- corrected line
- #define FRAME_W 3800
- #define FRAME_H 4000
- /*
- It is also useful to point out here:
- the TOUCH screen uses one set of X-Y minimum and maximum values,
- and the GRAPHICS screen uses another set of X-Y minimum and maximum values.
- The two are never the same.
- The touch screen is using whatever resistances the screen film can generate.
- The GFX screen is using the actual pixel counts of the screen itself.
- */
- int trigPin = 22; // Trigger of HC-SR04 sensor
- int echoPin = 53; // Echo of HC-SR04 sensor
- long duration, cm, x;
- int number;
- void Frame1() // How Frame 1 needs to be drawn, this works perfect, the problem lies further down in the code
- {
- tft.fillRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ST7735_BLACK);
- tft.setCursor(FRAME_X + 622, FRAME_Y + 602);
- tft.setTextColor(ILI9341_RED);
- tft.setTextSize(6);
- tft.println("-");
- tft.setCursor(FRAME_X + 620, FRAME_Y + 600);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(6);
- tft.println("-");
- delay(100);
- tft.setCursor(FRAME_X + 622, FRAME_Y + 602);
- tft.setTextColor(ILI9341_RED);
- tft.setTextSize(6);
- tft.println("--");
- tft.setCursor(FRAME_X + 620, FRAME_Y + 600);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(6);
- tft.println("--");
- delay(100);
- tft.setCursor(FRAME_X + 622, FRAME_Y + 602);
- tft.setTextColor(ILI9341_RED);
- tft.setTextSize(6);
- tft.println("-->");
- tft.setCursor(FRAME_X + 620, FRAME_Y + 600);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(6);
- tft.println("-->");
- return;
- }
- void Frame2() // How Frame 2 needs to be drawn, this works perfect, the problem lies further down in the code
- {
- tft.fillRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ST7735_BLACK);
- tft.setCursor(FRAME_X + 542, FRAME_Y + 602);
- tft.setTextColor(ILI9341_RED);
- tft.setTextSize(6);
- tft.println("70 KM/H");
- tft.setCursor(FRAME_X + 540, FRAME_Y + 600);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(6);
- tft.println("70 KM/H");
- return;
- }
- void Frame3() // How Frame 3 needs to be drawn, this works perfect, the problem lies further down in the code
- {
- tft.fillRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ST7735_BLACK);
- tft.setCursor(FRAME_X + 659, FRAME_Y + 593);
- tft.setTextColor(ILI9341_RED);
- tft.setTextSize(7);
- tft.println("^");
- tft.setCursor(FRAME_X + 659, FRAME_Y + 595);
- tft.setTextColor(ILI9341_RED);
- tft.setTextSize(7);
- tft.println("|");
- tft.setCursor(FRAME_X + 659, FRAME_Y + 602);
- tft.setTextColor(ILI9341_RED);
- tft.setTextSize(7);
- tft.println("|");
- tft.setCursor(FRAME_X + 658, FRAME_Y + 592);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(7);
- tft.println("^");
- tft.setCursor(FRAME_X + 658, FRAME_Y + 594);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(7);
- tft.println("|");
- tft.setCursor(FRAME_X + 658, FRAME_Y + 601);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(7);
- tft.println("|");
- // 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.
- //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.
- }
- void setup(void)
- {
- Serial.begin (9600);
- Serial.println("inside 'void setup(void) loop");
- delay(500);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- // Serial.begin(9600); -------------------------------------- already did this in the first line...
- tft.begin();
- if (!ts.begin()) {
- Serial.println("Unable to start touchscreen.");
- }
- else {
- Serial.println("Touchscreen started.");
- }
- tft.fillScreen(ILI9341_BLACK);
- // origin = left,top landscape (USB left upper)
- tft.setRotation(1);
- delay(500);
- Frame1(); // -------------------------------------------------- why are you drawing all three screens in rapid-fire here???
- Frame2();
- Frame3();
- }
- void loop()
- {
- Serial.println("inside 'void loop()' loop");
- delay(500);
- digitalWrite(trigPin, LOW);
- delayMicroseconds(5);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- pinMode(echoPin, INPUT);
- duration = pulseIn(echoPin, HIGH);
- int cm = (duration / 2) / 29.1;
- int x = cm;
- Serial.println("setting up integers \n");
- delay(500);
- if (x < 50) {
- Serial.println("inside 'do while' loop \n");
- Serial.println("drawing Frame2 \n");
- Frame2(); // This line f*cks everything up, please help me :)
- delay(500);
- Serial.println("drawing Frame1 \n");
- Frame1(); // This line f*cks everything up, please help me :)
- }
- else {
- Serial.println("drawing Frame3 \n");
- Frame3(); // This line f*cks everything up, please help me :)
- delay(500);
- }
- Serial.print(cm);
- Serial.print("cm \n");
- delay(250);
- }
Add Comment
Please, Sign In to add comment