Advertisement
j0h

te

j0h
Nov 23rd, 2021
1,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.78 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <NativeEthernet.h>
  3. #include <SD.h>
  4. File dataFile;
  5.  
  6. byte mac[] = {
  7.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  8. };
  9. //10.71.148.177 home
  10. IPAddress ip(10, 71, 148, 177);
  11. //IPAddress ip(10, 1, 0, 177);
  12. EthernetServer server(80);
  13. //Function defs
  14. void rotorSense(EthernetClient client);
  15. void rotorA(EthernetClient client);
  16. void rotorB(EthernetClient client);
  17. void startPage(EthernetClient client);
  18. void endPage(EthernetClient client);
  19. void svgHeader(EthernetClient client);
  20. void svgFooter(EthernetClient client);
  21. void polyLineBegin(EthernetClient client);
  22. void polyLineEnd(EthernetClient client);
  23. void pageWrite(EthernetClient client);
  24. void listenClient(EthernetClient client);
  25. void background(EthernetClient client); //only want to do this one once, maybe I can run an Iframe or something
  26. void sensors();//pots
  27. /**Pot Vars**/
  28. int potX = A0;    
  29. int potY = A1;    
  30.  
  31. int sensorValX = 0;  
  32. int sensorValY = 0;  
  33.  
  34. int oldX = 0;
  35. int oldY = 0;
  36.  
  37. /**Rotor Vars**/
  38. // Rotary Encoder Input efintions
  39. #define CLKA 2
  40. #define DTA 3
  41. #define SWA 4
  42.  
  43. #define CLKB 5
  44. #define DTB 6
  45. #define SWB 7
  46.  
  47. int Acounter = 0;
  48. int Bcounter = 0;
  49.  
  50. int AcurrentStateCLK;
  51. int BcurrentStateCLK;
  52.  
  53. int AlastStateCLK;
  54. int BlastStateCLK;
  55.  
  56. String AcurrentDir ="";  //just easier than cleverly combining this string for seprate IO
  57. String BcurrentDir ="";
  58.  
  59. unsigned long AlastButtonPress = 0;
  60. unsigned long BlastButtonPress = 0;
  61. //
  62.  
  63.    
  64. void setup() {
  65.  
  66.   delay(5000);  //you want this delay. reason: tl;dr
  67.   /** Set encoder pins as inputs **/
  68.   pinMode(CLKA,INPUT);
  69.   pinMode(DTA,INPUT);
  70.   pinMode(SWA, INPUT_PULLUP);
  71.   pinMode(CLKB,INPUT);
  72.   pinMode(DTB,INPUT);
  73.   pinMode(SWB, INPUT_PULLUP);
  74.  
  75.   // Read the initial state of CLK
  76.   AlastStateCLK = digitalRead(CLKA);
  77.   BlastStateCLK = digitalRead(CLKB);
  78.   /***End Rotary Encoder vars****/
  79.  
  80.   Serial.begin(9600);
  81.  
  82.   Ethernet.begin(mac, ip);
  83.  
  84. if (Ethernet.linkStatus() == LinkOFF) {
  85.     Serial.println("Ethernet cable is not connected.");
  86.   }
  87.  
  88.   // start the server
  89.   server.begin();
  90.   delay(100);
  91.   Serial.print("server is at ");
  92.   Serial.println(Ethernet.localIP());
  93.   Serial.print("Initializing SD card...");
  94.  
  95.   if (!SD.begin(BUILTIN_SDCARD)) {
  96.     Serial.println("SD Card init failed!");
  97.     while (1);
  98.     }
  99. /****SD CARD and Ethernet intialized*****/  
  100. //if data file (on SD Card) Doesn't exist, create it.
  101. //if data file (on SD Card) does exist errase it and create a new one
  102.   if (SD.exists("datalog.txt")) {
  103.     Serial.println("datalog.txt exists: \n Removing it.");
  104.     SD.remove("datalog.txt");
  105.   } else {
  106.     Serial.println("datalog.txt doesn't exist.");
  107.          }
  108.  
  109.   // open a new file and immediately close it:
  110.   Serial.println("Creating datalog.txt...");
  111.   dataFile = SD.open("datalog.txt", FILE_WRITE);
  112.   dataFile.close();
  113.  
  114.   // Check to see if the file exists:
  115.   if (SD.exists("datalog.txt")) {
  116.     Serial.println("datalog.txt exists.");
  117.   } else {
  118.     Serial.println("datalog.txt wasn't created.");
  119.          }
  120. } //end setup
  121.  
  122. void loop() {
  123. sensors();
  124.  // listen for incoming clients
  125.   EthernetClient client = server.available();
  126. listenClient(client);
  127. }  //end loop  
  128. void listenClient(EthernetClient client){
  129.  
  130.    if (client) {
  131.     boolean currentLineIsBlank = true;
  132.     while (client.connected()) {
  133.        if (client.available()) {
  134.           char c = client.read();
  135.        // Serial.write(c);   //tells about the client connection
  136.         if (c == '\n' && currentLineIsBlank) {
  137.           pageWrite(client);
  138.           break;
  139.         }
  140.         if (c == '\n') {
  141.           // you're starting a new line
  142.           currentLineIsBlank = true;
  143.         } else if (c != '\r') {
  144.           // you've gotten a character on the current line
  145.           currentLineIsBlank = false;
  146.         }
  147.       }//end avail
  148.     } //end conect
  149.     // give the web browser time to receive the data
  150.     delay(1);
  151.     // close the connection:
  152.     client.stop();
  153.   } //end client
  154.   }
  155. void sensors(){
  156.     int sensorValX = analogRead(8);
  157.   int sensorValY = analogRead(9);
  158.   String dataString = "";
  159.   int threshhold =3;
  160.   int xVariance = abs(sensorValX-oldX);
  161.   int yVariance = abs(sensorValY-oldY);
  162.  
  163. if (xVariance >=threshhold && yVariance >= threshhold ||xVariance >=threshhold || yVariance >=threshhold){
  164. //if sensor data is greater than threshold, write data to the sd card, then close the file
  165.     dataString += String(sensorValX);
  166.     dataString += ",";
  167.     dataString += String(sensorValY);
  168.  
  169.    dataFile = SD.open("datalog.txt", FILE_WRITE);
  170.     // if the file is available, write to it:
  171.    if (dataFile) {
  172.       dataFile.println(dataString);
  173.       dataFile.close();
  174.      
  175.       //clear dataString
  176.       dataString="";
  177.   } else {
  178.     // if the file isn't open, pop up an error:
  179.     Serial.println("error opening datalog.txt");
  180.   }
  181. dataFile.close();
  182.  
  183. }else{
  184.   ;
  185.   }
  186.  
  187.   }
  188.  
  189. void pageWrite(EthernetClient client){
  190.           startPage(client);  
  191.           svgHeader(client);          
  192.           polyLineBegin(client);
  193.           polyLineEnd(client);
  194.           svgFooter(client);      
  195.           endPage(client);  
  196.  
  197.   }
  198.  
  199.  
  200. void startPage(EthernetClient client){
  201.      // send a standard http response header
  202.           client.println("HTTP/1.1 200 OK");
  203.           client.println("Content-Type: text/html");
  204.           client.println("Connection: close");  // the connection will be closed after completion of the response
  205.           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  206.           client.println();
  207.           client.println("<!DOCTYPE HTML>");
  208.           client.println("<html>");
  209.   }
  210.  
  211.  void endPage(EthernetClient client){
  212.   client.println("</body>");
  213.   client.println("</html>");
  214.   }
  215.  
  216.  
  217. void polyLineBegin(EthernetClient client){
  218.   client.print("<polyline points=\""); //x,y points go here
  219.   //read data from SD card
  220.     File dataFile = SD.open("datalog.txt");
  221.  
  222.   // if the file is available, read it:
  223.   if (dataFile) {
  224.     while (dataFile.available()) {
  225.       //Serial.write(dataFile.read());
  226.       client.write(dataFile.read());
  227.     }
  228.     dataFile.close();
  229.   }
  230. }
  231.  
  232. void polyLineEnd(EthernetClient client){
  233.    client.println ("0, 0\" stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>");  //a dirty hack to avoid dealing with 1 last trailing comma.
  234.   }
  235.  
  236. void svgHeader(EthernetClient client){
  237. client.println("<svg width=\"1023\" height=\"1023\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">");
  238.   }
  239.  
  240. void svgFooter(EthernetClient client){
  241. client.println ("</svg>");
  242.   }
  243.  
  244. void background(EthernetClient client){
  245. //  client.println();
  246.  
  247. client.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
  248. "<svg"
  249. "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
  250. "xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\""
  251. "id=\"svg8\" version=\"1.1\" viewBox=\"0 0 227.35277 156.29323\""
  252. "height=\"6.1532764in\""
  253. "width=\"8.9508963in\""
  254. "sodipodi:docname=\"etchasketchborder.svg\">"
  255. "<defs"
  256. "id=\"defs2\" />"
  257. "<path"
  258. "style=\"fill:#ff0000;stroke:#ff0000;stroke-width:1.32300019;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\""
  259. "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\""
  260. "/>"
  261. "<ellipse"
  262. "style=\"fill:#ffffff;fill-opacity:1;stroke:#ff0000;stroke-width:1.32300007;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\""
  263. "cx=\"21.16666\""
  264. "cy=\"130.59085\""
  265. "rx=\"10.583334\""
  266. "ry=\"9.0714283\" />"
  267. "<ellipse"
  268. "style=\"fill:#ffffff;fill-opacity:1;stroke:#ff0000;stroke-width:1.32300007;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1\""
  269. "cx=\"200.62982\""
  270. "cy=\"131.57361\""
  271. "rx=\"10.583334\""
  272. "ry=\"9.0714283\" />"
  273. "</svg>");
  274.   }  
  275.  
  276.  void rotorA(EthernetClient client){
  277.  // Read the current state of CLK
  278.   AcurrentStateCLK = digitalRead(CLKA);
  279.  
  280.   // If last and current state of CLK are different, then pulse occurred
  281.   // React to only 1 state change to avoid double count
  282.   if (AcurrentStateCLK != AlastStateCLK  && AcurrentStateCLK == 1){
  283.  
  284.     // If the DT state is different than the CLK state then
  285.     // the encoder is rotating CCW so decrement
  286.     if (digitalRead(DTA) != AcurrentStateCLK) {
  287.       Acounter --;
  288.       AcurrentDir ="CCW";
  289.     } else {
  290.       // Encoder is rotating CW so increment
  291.       Acounter ++;
  292.       AcurrentDir ="CW";
  293.     }
  294.     Serial.print("A Direction: ");
  295.     Serial.print(AcurrentDir);
  296.     Serial.print(" | Counter: ");
  297.     Serial.println(Acounter);
  298.   }
  299.   int AbtnState = digitalRead(SWA);
  300.  
  301.   //If we detect LOW signal, button is pressed
  302.   if (AbtnState == LOW) {
  303.     //if 50ms have passed since last LOW pulse, it means that the
  304.     //button has been pressed, released and pressed again
  305.     if (millis() - AlastButtonPress > 500) {
  306.       Serial.println("Button A pressed!");
  307.     }
  308.   AlastButtonPress = millis();
  309.   }
  310.   // Remember last CLK state
  311.   AlastStateCLK = AcurrentStateCLK;
  312. //endA
  313.  
  314.  
  315.   }
  316. void rotorB(EthernetClient client){
  317.  
  318.   BcurrentStateCLK = digitalRead(CLKB);
  319.   // If last and current state of CLK are different, then pulse occurred
  320.   // React to only 1 state change to avoid double count
  321.   if (BcurrentStateCLK != BlastStateCLK  && BcurrentStateCLK == 1){
  322.  
  323.     // If the DT state is different than the CLK state then
  324.     // the encoder is rotating CCW so decrement
  325.     if (digitalRead(DTB) != BcurrentStateCLK) {
  326.       Bcounter --;
  327.       BcurrentDir ="CCW";
  328.     } else {
  329.       // Encoder is rotating CW so increment
  330.       Bcounter ++;
  331.       BcurrentDir ="CW";
  332.     }
  333.  
  334.     Serial.print("B Direction: ");
  335.     Serial.print(BcurrentDir);
  336.     Serial.print(" | Counter: ");
  337.     Serial.println(Bcounter);
  338.   }  
  339.   BlastStateCLK = BcurrentStateCLK;
  340.   // Read the button state
  341.   int BbtnState = digitalRead(SWB);
  342.     if (BbtnState == LOW) {
  343.     //if 50ms have passed since last LOW pulse, it means that the
  344.     //button has been pressed, released and pressed again
  345.     if (millis() - BlastButtonPress > 50) {
  346.       Serial.println("Button B pressed!");
  347.     }
  348.  
  349.  // Remember last button press event
  350.     BlastButtonPress = millis();
  351.   }
  352. //endB
  353. }
  354. void rotorSense(EthernetClient client){
  355.   rotorA(client);
  356.   rotorB(client);
  357.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement