j0h

Working_Etch-A-Sketch

j0h
Dec 4th, 2021
1,343
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. An IoT Etch-A-Sketch
  3. uses 2 rotory encoders to generate x,y data points
  4. in a svg poly line.
  5.  
  6. */
  7. #include <SPI.h>
  8. #include <NativeEthernet.h>
  9. #include <SD.h>
  10. File dataFile;
  11.  
  12. byte mac[] = {
  13.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  14. };
  15. //10.71.148.177 home
  16. //IPAddress ip(10, 71, 148, 177);
  17. IPAddress ip(10, 1, 0, 177);
  18. EthernetServer server(80);
  19. //Function defs
  20. void rotors();
  21. void rotorA(EthernetClient client);
  22. void rotorB(EthernetClient client);
  23.  
  24. void listenClient(EthernetClient client);
  25. void pageWrite(EthernetClient client);
  26. void polyLineBegin(EthernetClient client);
  27. void startPage(EthernetClient client);
  28. void endPage(EthernetClient client);
  29.  
  30. int potX = A0;    
  31. int potY = A1;    
  32.  
  33. int sensorValX = 0;  
  34. int sensorValY = 0;  
  35.  
  36. int oldX = 0;
  37. int oldY = 0;
  38.  
  39. /**Rotor Vars**/
  40. // Rotary Encoder Input defintions
  41. #define CLKA 2
  42. #define DTA 3
  43. #define SWA 4
  44.  
  45. #define CLKB 5
  46. #define DTB 6
  47. #define SWB 7
  48.  
  49. int Acounter = 0;
  50. int Bcounter = 0;
  51.  
  52. int AcurrentStateCLK;
  53. int BcurrentStateCLK;
  54.  
  55. int AlastStateCLK;
  56. int BlastStateCLK;
  57.  
  58. String AcurrentDir ="";  //just easier than cleverly combining this string for seprate IO
  59. String BcurrentDir ="";
  60.    
  61. void setup() {
  62.  
  63.   delay(10000);  //you want this delay. reason: tl;dr
  64.   /** Set encoder pins as inputs **/
  65.   pinMode(CLKA,INPUT);
  66.   pinMode(DTA,INPUT);
  67.   pinMode(SWA, INPUT_PULLUP);
  68.   pinMode(CLKB,INPUT);
  69.   pinMode(DTB,INPUT);
  70.   pinMode(SWB, INPUT_PULLUP);
  71.  
  72.   // Read the initial state of CLK
  73.   AlastStateCLK = digitalRead(CLKA);
  74.   BlastStateCLK = digitalRead(CLKB);
  75.   /***End Rotary Encoder vars****/
  76.  
  77.   Serial.begin(9600);
  78.  
  79.   Ethernet.begin(mac, ip);
  80.  
  81. if (Ethernet.linkStatus() == LinkOFF) {
  82.     Serial.println("Ethernet cable is not connected.");
  83.   }
  84.  
  85.   // start the server
  86.   server.begin();
  87.   delay(100);
  88.   Serial.print("server is at ");
  89.   Serial.println(Ethernet.localIP());
  90.   Serial.print("Initializing SD card...");
  91.  
  92.   if (!SD.begin(BUILTIN_SDCARD)) {
  93.     Serial.println("SD Card init failed!");
  94.     while (1);
  95.     }
  96. /****SD CARD and Ethernet intialized*****/  
  97. //if data file (on SD Card) Doesn't exist, create it.
  98. //if data file (on SD Card) does exist errase it and create a new one
  99.   if (SD.exists("datalog.txt")) {
  100.      Serial.println("datalog.txt exists: \n Removing it.");
  101.      while(SD.remove("datalog.txt")!=1){
  102.        Serial.println("Deleeting old file data");
  103.        if (SD.remove("datalog.txt")==1){
  104.           Serial.println ("old dataFile removed");
  105.        }
  106.      }  
  107.   } else {
  108.     Serial.println("datalog.txt doesn't exist.");
  109.          }
  110.  
  111.   // open a new file and immediately close it:
  112.   Serial.println("Creating datalog.txt...");
  113.   dataFile = SD.open("datalog.txt", FILE_WRITE);
  114.   dataFile.close();
  115.  
  116.   // Check to see if the file exists:
  117.   if (SD.exists("datalog.txt")) {
  118.     Serial.println("datalog.txt exists.");
  119.   } else {
  120.     Serial.println("datalog.txt wasn't created.");
  121.          }
  122. } //end setup
  123.  
  124. void loop() {
  125.  // listen for incoming clients
  126.   EthernetClient client = server.available();
  127. rotors();
  128. listenClient(client);
  129. }  //end loop  
  130. void listenClient(EthernetClient client){
  131.  
  132.    if (client) {
  133.     boolean currentLineIsBlank = true;
  134.     while (client.connected()) {
  135.        if (client.available()) {
  136.           char c = client.read();
  137.        // Serial.write(c);   //tells about the client connection
  138.         if (c == '\n' && currentLineIsBlank) {
  139.           pageWrite(client);
  140.           break;
  141.         }
  142.         if (c == '\n') {
  143.           // you're starting a new line
  144.           currentLineIsBlank = true;
  145.         } else if (c != '\r') {
  146.           // you've gotten a character on the current line
  147.           currentLineIsBlank = false;
  148.         }
  149.       }//end avail
  150.     } //end conect
  151.     // give the web browser time to receive the data
  152.     delay(1);
  153.     // close the connection:
  154.     client.stop();
  155.   } //end client
  156.   }//end listener()
  157.  
  158. void pageWrite(EthernetClient client){
  159.           startPage(client);  
  160.           polyLineBegin(client);
  161.           endPage(client);  
  162.   }
  163. void startPage(EthernetClient client){
  164.      // doctype svg no need for html
  165.           client.println("HTTP/1.1 200 OK");
  166.           client.println("Content-Type: text/html");
  167.           client.println("Connection: close");  // the connection will be closed after completion of the response
  168.           client.println("Refresh: 1");  // refresh the page automatically every 5 sec
  169.           client.println();
  170.           client.println("<!DOCTYPE svg>");
  171.           client.println("<svg   xmlns='http://www.w3.org/2000/svg'  viewBox='0 0 227.35277 156.29323' height='6.1532764in' width='8.9508963in'><path  style='fill:#ff0000; stroke:#ff0000; stroke-width:1.32300019;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1' d='m 12.000819,0.66145838 c -6.2819637,0 -11.33936062,5.05739692 -11.33936062,11.33936062 V 144.29249 c 0,6.28197 5.05739692,11.33936 11.33936062,11.33936 H 215.35224 c 6.28197,0 11.33885,-5.05739 11.33885,-11.33936 V 12.000819 c 0,-6.2819637 -5.05688,-11.33936062 -11.33885,-11.33936062 z M 18.932696,12.696383 H 205.5854 c 5.02557,0 9.07128,4.04571 9.07128,9.071282 v 77.03923 c 0,5.025575 -4.04571,9.071285 -9.07128,9.071285 H 18.932696 c -5.025572,0 -9.0712819,-4.04571 -9.0712819,-9.071285 v -77.03923 c 0,-5.025572 4.0457099,-9.071282 9.0712819,-9.071282 z' /><ellipse style='fill:#ffffff;fill-opacity:1; stroke:#ff0000; stroke-width:1.32300007; stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1' cx='21.16666'  cy='130.59085'  rx='10.583334' ry='9.0714283' /><ellipse  style='fill:#ffffff; fill-opacity:1; stroke:#ff0000; stroke-width:1.32300007; stroke-miterlimit:4; stroke-dasharray:none;stroke-opacity:1' cx='200.62982'   cy='131.57361' rx='10.583334' ry='9.0714283'/>");
  172.           client.println("\n<polyline points='15,15");          
  173.    }
  174.  void endPage(EthernetClient client){
  175.   client.print("' \nfill='none' stroke='#ff0000'/></svg>");
  176.    }
  177. void polyLineBegin(EthernetClient client){
  178. //read data points from SD card
  179.     File dataFile = SD.open("datalog.txt");
  180.   // if the file is available, read it:
  181.   if (dataFile) {
  182.     while (dataFile.available()) {
  183.       //Serial.write(dataFile.read());
  184.       client.write(dataFile.read());
  185.     }
  186.     dataFile.close();
  187.   }
  188. }
  189.  
  190.  
  191. int rotorA(){ //X coords
  192.   AcurrentStateCLK = digitalRead(CLKA);
  193.   if (AcurrentStateCLK != AlastStateCLK  && AcurrentStateCLK == 1){
  194.      if (digitalRead(DTA) != AcurrentStateCLK) {
  195.       Acounter --;
  196.       }else{
  197.        Acounter ++;
  198.        }
  199.      }
  200. // Remember last CLK state
  201.   AlastStateCLK = AcurrentStateCLK;
  202. //X limits
  203. if(Acounter<=15){
  204.   Acounter=15;
  205.   }
  206. if(Acounter>=215){
  207.   Acounter=215;
  208.   }  
  209. return Acounter;
  210.   }//endA rotor
  211.  
  212. int rotorB(){ //Y coords
  213.   BcurrentStateCLK = digitalRead(CLKB);
  214.   if (BcurrentStateCLK != BlastStateCLK  && BcurrentStateCLK == 1){
  215.     if (digitalRead(DTB) != BcurrentStateCLK) {
  216.        Bcounter --;
  217.        }else{
  218.         Bcounter ++;
  219.      }
  220.     //Serial.println(Bcounter);
  221.   }  
  222.   BlastStateCLK = BcurrentStateCLK;
  223. //Y limits
  224. if(Bcounter<=15){
  225.   Bcounter=15;
  226.   }
  227. if(Bcounter>=210){
  228.   Bcounter=210;
  229.   }  
  230.   return Bcounter;
  231.   }//EndB rotor
  232.  
  233. void rotors(){
  234. //read rotors and write x,y points to datalog.txt  
  235.  sensorValX=rotorA();
  236.  sensorValY=rotorB();
  237. String dataString = "";
  238.  
  239. if(sensorValX!=oldX || sensorValY!=oldY){
  240.    dataString += ",";
  241.    dataString += String(sensorValX);
  242.    dataString += ", ";
  243.    dataString += String(sensorValY);
  244.    
  245.    Serial.println(dataString);
  246.    
  247.    dataFile = SD.open("datalog.txt", FILE_WRITE);
  248.     // if the file is available, write to it:
  249.    if (dataFile) {
  250.       dataFile.print(dataString);
  251.       dataFile.close();
  252.       //clear dataString
  253.       dataString="";
  254.       }else{
  255.     // if the file isn't open, pop up an error:
  256.     Serial.println("error opening datalog.txt");
  257.   }
  258.   dataFile.close();
  259.   }else{
  260.   ;
  261.   }
  262.  
  263.  oldX=sensorValX;
  264.  oldY=sensorValY;
  265.  
  266. }//end rotors
RAW Paste Data