Advertisement
Guest User

MouseTree/Polygon

a guest
Oct 5th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.74 KB | None | 0 0
  1.  
  2. #include "DigiMouse.h"
  3.  
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7. #define BUT1 PB0
  8. #define BUT2 PB2
  9. #define LED PB1
  10.  
  11. int but1State = 0;
  12. int but2State = 0;
  13. int but1Temp = 0;
  14. int but2Temp = 0;
  15. int ledState = LOW;
  16. int b1t1;
  17. int b1t2;
  18. int b2t1;
  19. int b2t2;
  20. int ledT1;
  21. int ledT2;
  22.  
  23. const int dl = 10;       // wait 10 ms between readings
  24. const int ledTime = 500; // Time for LED to stay on
  25.  
  26. const int mouseDelay = 100;
  27.  
  28. const float pi = 3.14159265359;
  29. const float rad_per_deg = pi / 180;
  30.  
  31. void on(){        // Turn the LED on
  32.  
  33.   #ifdef BEEP
  34.     tone(LED,550); // If our pin is connected to a piezo we can use tone()
  35.   #endif
  36.   #ifndef BEEP
  37.     digitalWrite(LED, HIGH);
  38.   #endif
  39. }
  40.    
  41. void off(){       // Turn the LED off
  42.   #ifdef BEEP
  43.    noTone(LED); // turn off piezo
  44.   #endif
  45.   #ifndef BEEP
  46.     digitalWrite(LED, LOW);
  47.   #endif
  48. }
  49.  
  50. int btnPressed(int button, int &buttonState, int &butTemp, int &t1, int &t2) {
  51.  
  52.   // Check if a button was pressed and de-bounce it too.
  53.  
  54.   int retval = FALSE;
  55.  
  56.   t2 = millis();
  57.   butTemp = digitalRead(button);
  58.  
  59.   if (butTemp != buttonState) {
  60.     if (butTemp == HIGH && t2 - t1 > dl){
  61.       // Button released
  62.       retval = TRUE;
  63.     } else {
  64.       // Button pressed
  65.       t1 = millis();
  66.     }
  67.   }
  68.   buttonState = butTemp;
  69.   return retval;
  70. }
  71.  
  72. void setup() {
  73.   // put your setup code here, to run once:
  74.  
  75.   pinMode(LED, OUTPUT);
  76.   pinMode(BUT1, INPUT_PULLUP);
  77.   pinMode(BUT2, INPUT_PULLUP);
  78.   but1State = digitalRead(BUT1);
  79.   but2State = digitalRead(BUT2);
  80.  
  81.   b1t1 = millis();
  82.   b2t1 = millis();
  83.  
  84.   ledT1 = millis();               // Setting these 2 equal causes the LED to flash once
  85.   ledT2 = ledT1;                  // when the board comes up (as if a button was
  86.                                   // pressed)
  87.  
  88.   DigiMouse.begin();              // DigiMouse setup; start or reenumerate USB
  89.  
  90. }
  91.  
  92. void mouseTree(int branchLen,
  93.                float branchAngle,
  94.                float branchFactor,
  95.                float branchSplitAngle,
  96.                float branchSkewAngle,
  97.                int maxLevel) {
  98.  
  99.   int dx;
  100.   int dy;
  101.  
  102.   if (maxLevel <= 0) {               // Bail out of recursion
  103.     return;
  104.   }
  105.  
  106.   DigiMouse.update();                // DigiMouse.update() at least every 50ms#
  107.  
  108.   DigiMouse.setButtons(1);           //left click
  109.   DigiMouse.delay(mouseDelay);
  110.  
  111.   dx = branchLen * sin(branchAngle); // X component of movement for this branch
  112.   dy = branchLen * cos(branchAngle); // Y component of movement for this branch
  113.  
  114.   DigiMouse.moveY(-dy);              // Move by (dy, dy) of the branch.  Note that Y
  115.   DigiMouse.moveX(dx);               // inreases as we move down the screen and not up;
  116.   DigiMouse.delay(mouseDelay);       // We invert Y (-dy) to grow the tree upwards.
  117.  
  118.   // Left branch of next level ...
  119.  
  120.   mouseTree (branchLen * branchFactor,
  121.              branchAngle + (((branchSplitAngle + branchSkewAngle) * rad_per_deg) / 2),
  122.              branchFactor,
  123.              branchSplitAngle,
  124.              branchSkewAngle,
  125.              maxLevel - 1);
  126.  
  127.   // Right branch of next level ...
  128.  
  129.   mouseTree (branchLen * branchFactor,
  130.              branchAngle - (((branchSplitAngle - branchSkewAngle) * rad_per_deg) / 2),
  131.              branchFactor,
  132.              branchSplitAngle,
  133.              branchSkewAngle,
  134.              maxLevel - 1);
  135.              
  136.   DigiMouse.moveY(dy);              // Move back to the base of the branch.  See above
  137.   DigiMouse.moveX(-dx);             // comments concerning flipping Y-axis.
  138.   DigiMouse.delay(mouseDelay);
  139.  
  140.   DigiMouse.setButtons(0);          // un-click all
  141.   DigiMouse.delay(mouseDelay);
  142.  
  143. }
  144.  
  145. void mousePoly (int sides, int intDia) {
  146.  
  147.   // Formula for polygon:
  148.   //
  149.   // side of a regular polygon (a) is:
  150.   //
  151.   //  a = s tan (180/n)
  152.   //
  153.   //   s = internal diameter (distance across flats for even polygon)
  154.   //   a = length of one side
  155.   //   n = number of sides.
  156.   //
  157.   // All of the above is in degrees, so we substitute 180 for pi if we're using
  158.   // Radians:
  159.   //
  160.   //   a = s tan (pi/n)
  161.   //
  162.  
  163.   int dx;
  164.   int dy;
  165.  
  166.   int len;
  167.  
  168.   int side;
  169.   float angle;
  170.  
  171.   len = intDia * sin (pi / sides);
  172.  
  173.   DigiMouse.update();                // DigiMouse.update() at least every 50ms
  174.  
  175.   DigiMouse.setButtons(1);           // left click
  176.   DigiMouse.delay(mouseDelay);
  177.  
  178.   angle = 0;
  179.  
  180.   for (side = 0; side < sides; side++) {
  181.  
  182.     dy = len * cos(angle);
  183.     dx = len * sin(angle);
  184.  
  185.     DigiMouse.update();              // DigiMouse.update() at least every 50ms
  186.    
  187.     DigiMouse.moveY(dy);             // Move by (dy, dy).
  188.     DigiMouse.moveX(dx);
  189.     DigiMouse.delay(mouseDelay);
  190.  
  191.     angle += (2 * pi) / sides;       // 2 pi radians in a circle, divided by the number
  192.                                      // of sides in our polygon.
  193.   }
  194.  
  195.   DigiMouse.setButtons(0);           // un-click all
  196.   DigiMouse.delay(mouseDelay);
  197. }
  198.  
  199. void loop() {
  200.  
  201.   // Check buttons
  202.  
  203.   int btn1WasPressed = btnPressed(BUT1, but1State, but1Temp, b1t1, b1t2);
  204.   int btn2WasPressed = btnPressed(BUT2, but2State, but2Temp, b2t1, b2t2);
  205.  
  206.   // Button pressed?  Start the timer.
  207.  
  208.   if ( btn1WasPressed || btn2WasPressed ) {
  209.     ledT1 = millis();
  210.   }
  211.  
  212.   //  Keep track of time
  213.  
  214.   ledT2 = millis();
  215.  
  216.   // Turn on the LED if we're within the time window of 0 -> ledTime ms of a button press
  217.  
  218.   if ( (ledT2 >= ledT1) && (ledT2 < ledT1 + ledTime) ) {
  219.     on();
  220.   } else {
  221.     off();
  222.   }
  223.  
  224.   DigiMouse.update();        // DigiMouse.update() at least every 50ms
  225.  
  226.   if ( btn1WasPressed ) {
  227.  
  228.     mouseTree(100,                     // Draw a Tree.
  229.               0,
  230.               0.75,
  231.               60,
  232.               10,
  233.               7);
  234.   }
  235.  
  236.   if ( btn2WasPressed ) {
  237.  
  238.     mousePoly(100, 100);
  239.   }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement