j0h

Arduino Etch-a-sketch

j0h
Jul 31st, 2021
1,069
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*EtchaSketch
  2.  * 2 pots, X,Y, increment, and plot lines. might require error checks, and corrections
  3.  * increment accordingly
  4.  *
  5.  * This program communicates on the serial port, to a seprate program that handles the drawing of recieved cordinates.  
  6.  *
  7.  */
  8. /**Fucntion Defs**/
  9. void svgHeader();
  10. void svgFooter();
  11. void polyLineBegin();
  12. void polyLineEnd();
  13.  
  14. int Reset = 4;     //programticaly reset the arduino  
  15. int potX = A0;    
  16. int potY = A1;    
  17.  
  18. int sensorValX = 0;  
  19. int sensorValY = 0;  
  20.  
  21. int oldX = 0;
  22. int oldY = 0;
  23.  
  24. int del = 2;       //a button on pin 2 for deleting drawn content
  25. int delbutton = 0;
  26.  
  27. void setup() {
  28.     Serial.begin(9600);
  29.     pinMode(del, INPUT);
  30.     delay(1000);
  31. }
  32.  
  33. void loop() {
  34.   // close the svg and start over
  35.   if(digitalRead(del) == LOW ){
  36.        delbutton=1;
  37.      polyLineEnd();
  38.      svgFooter();
  39.      digitalWrite(Reset, LOW);
  40.      digitalWrite(Reset, HIGH);
  41.      svgHeader();
  42.      polyLineBegin();
  43.       }else{
  44.               delbutton=0;
  45.           }  
  46.  
  47.   sensorValX = analogRead(potX);
  48.   sensorValY = analogRead(potY);
  49. /*dont draw if pots not changing    
  50.   Ah but the sensors are noisy, so thresholding
  51. */
  52. int threshold =3;
  53. int xVariance = abs(sensorValX-oldX);
  54. int yVariance = abs(sensorValY-oldY);
  55. if (xVariance >=threshold && yVariance >= threshold ||xVariance >=threshold || yVariance >=threshold){
  56.     Serial.print(sensorValX);
  57.     Serial.print(",");
  58.     Serial.print(sensorValY);
  59.     Serial.print(",");
  60.     delay(100);
  61.     oldX=sensorValX;
  62.     oldY=sensorValY;
  63.     }else{
  64.        ; //dont do shi.
  65.     }
  66. } //end loop
  67.  
  68.  /**Functions**/
  69.  
  70. void polyLineBegin(){
  71.   Serial.println ("<polyline points=\""); //x,y points go here
  72.  
  73.   }
  74.  
  75. void polyLineEnd(){
  76.    Serial.println ("0, 0\" stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>");  //a dirty hack to avoid dealing with 1 last trailing comma.
  77.   }
  78.  
  79. void svgHeader(){
  80. Serial.println("<svg width=\"1023\" height=\"1023\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">");
  81.   };  
  82.  
  83. void svgFooter(){
  84. Serial.println ("</svg>");
  85.   };
RAW Paste Data