[136] | 1 | /*
|
---|
| 2 |
|
---|
| 3 | TFT EtchASketch
|
---|
| 4 |
|
---|
| 5 | This example for the Arduino screen draws a white point
|
---|
| 6 | on the GLCD based on the values of 2 potentiometers.
|
---|
| 7 | To clear the screen, press a button attached to pin 2.
|
---|
| 8 |
|
---|
| 9 | This example code is in the public domain.
|
---|
| 10 |
|
---|
| 11 | Created 15 April 2013 by Scott Fitzgerald
|
---|
| 12 |
|
---|
| 13 | http://arduino.cc/en/Tutorial/TFTEtchASketch
|
---|
| 14 |
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | #include <TFT.h> // Arduino LCD library
|
---|
| 18 | #include <SPI.h>
|
---|
| 19 |
|
---|
| 20 | // pin definition for the Uno
|
---|
| 21 | #define cs 10
|
---|
| 22 | #define dc 9
|
---|
| 23 | #define rst 8
|
---|
| 24 |
|
---|
| 25 | // pin definition for the Leonardo
|
---|
| 26 | // #define cs 7
|
---|
| 27 | // #define dc 0
|
---|
| 28 | // #define rst 1
|
---|
| 29 |
|
---|
| 30 | TFT TFTscreen = TFT(cs, dc, rst);
|
---|
| 31 |
|
---|
| 32 | // initial position of the cursor
|
---|
| 33 | int xPos = TFTscreen.width() / 2;
|
---|
| 34 | int yPos = TFTscreen.height() / 2;
|
---|
| 35 |
|
---|
| 36 | // pin the erase switch is connected to
|
---|
| 37 | int erasePin = 2;
|
---|
| 38 |
|
---|
| 39 | void setup() {
|
---|
| 40 | // declare inputs
|
---|
| 41 | pinMode(erasePin, INPUT);
|
---|
| 42 | // initialize the screen
|
---|
| 43 | TFTscreen.begin();
|
---|
| 44 | // make the background black
|
---|
| 45 | TFTscreen.background(0, 0, 0);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | void loop()
|
---|
| 49 | {
|
---|
| 50 | // read the potentiometers on A0 and A1
|
---|
| 51 | int xValue = analogRead(A0);
|
---|
| 52 | int yValue = analogRead(A1);
|
---|
| 53 |
|
---|
| 54 | // map the values and update the position
|
---|
| 55 | xPos = xPos + (map(xValue, 0, 1023, 2, -2));
|
---|
| 56 | yPos = yPos + (map(yValue, 0, 1023, -2, 2));
|
---|
| 57 |
|
---|
| 58 | // don't let the point go past the screen edges
|
---|
| 59 | if (xPos > 159) {
|
---|
| 60 | (xPos = 159);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (xPos < 0) {
|
---|
| 64 | (xPos = 0);
|
---|
| 65 | }
|
---|
| 66 | if (yPos > 127) {
|
---|
| 67 | (yPos = 127);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | if (yPos < 0) {
|
---|
| 71 | (yPos = 0);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | // draw the point
|
---|
| 75 | TFTscreen.stroke(255, 255, 255);
|
---|
| 76 | TFTscreen.point(xPos, yPos);
|
---|
| 77 |
|
---|
| 78 | // read the value of the pin, and erase the screen if pressed
|
---|
| 79 | if (digitalRead(erasePin) == HIGH) {
|
---|
| 80 | TFTscreen.background(0, 0, 0);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | delay(33);
|
---|
| 84 | }
|
---|