/************************************* MICRODEV.GR MAGIC PEN - ARDUINO MEGA 2560 (C) PANOS PAPAZOGLOU 2023 **************************************/ #include //LDR pins int pin[]={9,10,11,12,13,14,15}; //LDR status (1=LDR active, 0=LDR non active, based on value 70 as threshold) int stat[]={0,0,0,0,0,0,0}; //Basic color codes #define BLACK 0 #define GREY 21845 #define BLUE 31 #define RED 63488 #define GREEN 2016 #define DARKGREEN 1472 #define CYAN 2047 #define MAGENTA 63519 #define YELLOW 65504 #define GOLD 56768 #define WHITE 65535 //cross-cursor size int cw=20; //Declare object for accessing TFT library functions MCUFRIEND_kbv tft; //Set screen limits for cross-cursor movement int lx1=20; int lx2=450; int ly1=50; int ly2=240; //Coordinates for the START and END point of a line int x1,y1,x2,y2; //Cross-cursor current position (x,y coordinates) int posx,posy; //Pixel step for cross-cursor movement int step=25; /************************************* Initial function **************************************/ void setup() { //initialize serial communication for debugging purposes //Serial.begin(9600); //Initialize TFT screen / rotate for landscape view uint16_t ID = tft.readID(); if (ID == 0xD3D3) ID = 0x9486; tft.begin(ID); tft.setRotation(1); //Calculate middle position x,y of the screen int xx=tft.width() / 2.; int yy=tft.height() / 2.; //Set middle position for cross-cursor posx=xx; posy=yy; //Clear screen / fill with black color tft.fillScreen(BLACK); //draw titles and draw area frame init_draw(); //Draw cross-cursor draw_c(posx,posy,cw,RED); update_xy(); } /************************************* Main function **************************************/ void loop() { for(int i=0;i<7;i++) { //Read all LDR sensors int a=analogRead(pin[i]); //threshold value=70 (LDR activation) //update LDR status values based on analog reads if (a<70) stat[i]=1; else stat[i]=0; } //variable for deciding if the x,y value refresh will be performed int move=0; /************************************* Check if DOWN-LDR is activated **************************************/ if ((stat[4]==1) && (posy+steplx1)) { draw_c(posx,posy,cw,BLACK); posx-=step; draw_c(posx,posy,cw,RED);move=1; } /************************************* Check if UP-LDR is activated **************************************/ if ((stat[2]==1) && (posy-step>ly1)) //up { draw_c(posx,posy,cw,BLACK); posy-=step; draw_c(posx,posy,cw,RED);move=1; } //if the BEGIN-LDR is activated / store in x1,y1 the current cross-cursor for the line BEGIN position if (stat[0]==1) {x1=posx; y1=posy; disp_text(200,290,2,"Begin", GREEN);} //if the END-LDR is activated / store in x2,y2 the current cross-cursor for the line END position / Draw line (x1,y1 to x2,y2) if (stat[3]==1) {x2=posx; y2=posy; tft.drawLine(x1,y1,x2,y2,GREEN); disp_text(280,290,2,"End", GREEN);} //if the CLEAR/DEL-LDR is activated / Erase current line by using black drawing color if (stat[5]==1) {tft.drawLine(x1,y1,x2,y2,BLACK);} //Update x,y value on screen, only if a cross-cursor movement is performed if (move==1) update_xy(); delay(30); } /************************************* Prepare screen Display titles and frame **************************************/ void init_draw() { tft.setRotation(1); tft.fillScreen(BLACK); tft.setCursor(0, 10); tft.setTextSize(3); tft.setTextColor(WHITE, BLACK); tft.print("MICRODEV.GR"); disp_text(250,10,2,"Panos Papazoglou",CYAN); draw_frame(); } /************************************* Draw cross-cursor x: x-coordinate y: y-coordinate w: cross width color: cross color **************************************/ void draw_c(int x, int y, int w, int color) { tft.drawLine(x-w/2,y,x+w/2,y,color); tft.drawLine(x,y-w/2,x,y+w/2,color); } /************************************* Display text function **************************************/ void disp_text(int x, int y, int size, String text, int color) { tft.setTextSize(size); tft.setTextColor(color, BLACK); tft.setCursor(x,y); tft.print(text); } /************************************* Draw frame on screen **************************************/ void draw_frame() { tft.drawLine(0,35,479,35,BLUE); tft.drawLine(479,35,479,280,BLUE); tft.drawLine(479,280,0,280,BLUE); tft.drawLine(0,280,0,35,BLUE); } /************************************* Update x,y values on screen **************************************/ void update_xy() { tft.fillRect(0, 290, 319, 50, BLACK); disp_text(1,290,2,"X:",CYAN); disp_text(20,290,2,String(posx), CYAN); disp_text(120,290,2,"Y:",CYAN); disp_text(150,290,2,String(posy), CYAN); } /************************************* Function for debugging purposes **************************************/ /* void test_LDR() { for(int i=0;i<7;i++) { int a=analogRead(pin[i]); if (a<100) Serial.print("1"); else Serial.print("-"); Serial.print(" "); } Serial.println(""); } */