Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DigiMouse.h"
- #define TRUE 1
- #define FALSE 0
- #define BUT1 PB0
- #define BUT2 PB2
- #define LED PB1
- int but1State = 0;
- int but2State = 0;
- int but1Temp = 0;
- int but2Temp = 0;
- int ledState = LOW;
- int b1t1;
- int b1t2;
- int b2t1;
- int b2t2;
- int ledT1;
- int ledT2;
- const int dl = 10; // wait 10 ms between readings
- const int ledTime = 500; // Time for LED to stay on
- const int mouseDelay = 100;
- const float pi = 3.14159265359;
- const float rad_per_deg = pi / 180;
- void on(){ // Turn the LED on
- #ifdef BEEP
- tone(LED,550); // If our pin is connected to a piezo we can use tone()
- #endif
- #ifndef BEEP
- digitalWrite(LED, HIGH);
- #endif
- }
- void off(){ // Turn the LED off
- #ifdef BEEP
- noTone(LED); // turn off piezo
- #endif
- #ifndef BEEP
- digitalWrite(LED, LOW);
- #endif
- }
- int btnPressed(int button, int &buttonState, int &butTemp, int &t1, int &t2) {
- // Check if a button was pressed and de-bounce it too.
- int retval = FALSE;
- t2 = millis();
- butTemp = digitalRead(button);
- if (butTemp != buttonState) {
- if (butTemp == HIGH && t2 - t1 > dl){
- // Button released
- retval = TRUE;
- } else {
- // Button pressed
- t1 = millis();
- }
- }
- buttonState = butTemp;
- return retval;
- }
- void setup() {
- // put your setup code here, to run once:
- pinMode(LED, OUTPUT);
- pinMode(BUT1, INPUT_PULLUP);
- pinMode(BUT2, INPUT_PULLUP);
- but1State = digitalRead(BUT1);
- but2State = digitalRead(BUT2);
- b1t1 = millis();
- b2t1 = millis();
- ledT1 = millis(); // Setting these 2 equal causes the LED to flash once
- ledT2 = ledT1; // when the board comes up (as if a button was
- // pressed)
- DigiMouse.begin(); // DigiMouse setup; start or reenumerate USB
- }
- void mouseTree(int branchLen,
- float branchAngle,
- float branchFactor,
- float branchSplitAngle,
- float branchSkewAngle,
- int maxLevel) {
- int dx;
- int dy;
- if (maxLevel <= 0) { // Bail out of recursion
- return;
- }
- DigiMouse.update(); // DigiMouse.update() at least every 50ms#
- DigiMouse.setButtons(1); //left click
- DigiMouse.delay(mouseDelay);
- dx = branchLen * sin(branchAngle); // X component of movement for this branch
- dy = branchLen * cos(branchAngle); // Y component of movement for this branch
- DigiMouse.moveY(-dy); // Move by (dy, dy) of the branch. Note that Y
- DigiMouse.moveX(dx); // inreases as we move down the screen and not up;
- DigiMouse.delay(mouseDelay); // We invert Y (-dy) to grow the tree upwards.
- // Left branch of next level ...
- mouseTree (branchLen * branchFactor,
- branchAngle + (((branchSplitAngle + branchSkewAngle) * rad_per_deg) / 2),
- branchFactor,
- branchSplitAngle,
- branchSkewAngle,
- maxLevel - 1);
- // Right branch of next level ...
- mouseTree (branchLen * branchFactor,
- branchAngle - (((branchSplitAngle - branchSkewAngle) * rad_per_deg) / 2),
- branchFactor,
- branchSplitAngle,
- branchSkewAngle,
- maxLevel - 1);
- DigiMouse.moveY(dy); // Move back to the base of the branch. See above
- DigiMouse.moveX(-dx); // comments concerning flipping Y-axis.
- DigiMouse.delay(mouseDelay);
- DigiMouse.setButtons(0); // un-click all
- DigiMouse.delay(mouseDelay);
- }
- void mousePoly (int sides, int intDia) {
- // Formula for polygon:
- //
- // side of a regular polygon (a) is:
- //
- // a = s tan (180/n)
- //
- // s = internal diameter (distance across flats for even polygon)
- // a = length of one side
- // n = number of sides.
- //
- // All of the above is in degrees, so we substitute 180 for pi if we're using
- // Radians:
- //
- // a = s tan (pi/n)
- //
- int dx;
- int dy;
- int len;
- int side;
- float angle;
- len = intDia * sin (pi / sides);
- DigiMouse.update(); // DigiMouse.update() at least every 50ms
- DigiMouse.setButtons(1); // left click
- DigiMouse.delay(mouseDelay);
- angle = 0;
- for (side = 0; side < sides; side++) {
- dy = len * cos(angle);
- dx = len * sin(angle);
- DigiMouse.update(); // DigiMouse.update() at least every 50ms
- DigiMouse.moveY(dy); // Move by (dy, dy).
- DigiMouse.moveX(dx);
- DigiMouse.delay(mouseDelay);
- angle += (2 * pi) / sides; // 2 pi radians in a circle, divided by the number
- // of sides in our polygon.
- }
- DigiMouse.setButtons(0); // un-click all
- DigiMouse.delay(mouseDelay);
- }
- void loop() {
- // Check buttons
- int btn1WasPressed = btnPressed(BUT1, but1State, but1Temp, b1t1, b1t2);
- int btn2WasPressed = btnPressed(BUT2, but2State, but2Temp, b2t1, b2t2);
- // Button pressed? Start the timer.
- if ( btn1WasPressed || btn2WasPressed ) {
- ledT1 = millis();
- }
- // Keep track of time
- ledT2 = millis();
- // Turn on the LED if we're within the time window of 0 -> ledTime ms of a button press
- if ( (ledT2 >= ledT1) && (ledT2 < ledT1 + ledTime) ) {
- on();
- } else {
- off();
- }
- DigiMouse.update(); // DigiMouse.update() at least every 50ms
- if ( btn1WasPressed ) {
- mouseTree(100, // Draw a Tree.
- 0,
- 0.75,
- 60,
- 10,
- 7);
- }
- if ( btn2WasPressed ) {
- mousePoly(100, 100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement