Advertisement
Guest User

Untitled

a guest
Nov 19th, 2012
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.34 KB | None | 0 0
  1. #include <SoftwareServo.h>
  2.  
  3. SoftwareServo onOffServo;
  4. SoftwareServo leftRightServo;
  5. SoftwareServo upDownServo;
  6.  
  7. #define ON_OFF_SERVO_PIN PA6
  8. #define LEFT_RIGHT_SERVO_PIN PA7
  9. #define UP_DOWN_SERVO_PIN PB2
  10.  
  11. #define A 0
  12. #define B 1
  13.  
  14. #define LASER_OFF_ANGLE 120
  15. #define LASER_ON_ANGLE 140
  16.  
  17. #define MIN_UP_DOWN_ANGLE 25
  18. #define MAX_UP_DOWN_ANGLE 47
  19.  
  20. #define MIN_LEFT_RIGHT_ANGLE 48
  21. #define MAX_LEFT_RIGHT_ANGLE 132
  22.  
  23. // Y is 0 to 11
  24. #define MIN_UP_DOWN_COORDINATE 0
  25. #define MAX_UP_DOWN_COORDINATE (MAX_UP_DOWN_ANGLE-MIN_UP_DOWN_ANGLE)/2
  26.  
  27. #define MIN_Y MIN_UP_DOWN_COORDINATE
  28. #define MAX_Y MAX_UP_DOWN_COORDINATE
  29.  
  30. // X is 0 to 40
  31. #define MIN_LEFT_RIGHT_COORDINATE 0
  32. #define MAX_LEFT_RIGHT_COORDINATE (MAX_LEFT_RIGHT_ANGLE-MIN_LEFT_RIGHT_ANGLE)/2
  33.  
  34. #define MIN_X MIN_LEFT_RIGHT_COORDINATE
  35. #define MAX_X MAX_LEFT_RIGHT_COORDINATE
  36.  
  37. #define DRAW_PAUSE 500
  38. #define DISPLAY_PAUSE 5000
  39.  
  40. #define DRAWING1_PIN PA0
  41. #define DRAWING2_PIN PA1
  42. #define DRAWING3_PIN PA2
  43. #define DRAWING4_PIN PA3
  44. #define DRAWING5_PIN PA4
  45. #define DRAWING6_PIN PA5
  46.  
  47. int DRAWING_PINS[6] = {
  48.   DRAWING1_PIN,
  49.   DRAWING2_PIN,
  50.   DRAWING3_PIN,
  51.   DRAWING4_PIN,
  52.   DRAWING5_PIN,
  53.   DRAWING6_PIN
  54. };
  55.  
  56. void setup()
  57. {
  58.   onOffServo.attach(ON_OFF_SERVO_PIN,A);
  59.   leftRightServo.attach(LEFT_RIGHT_SERVO_PIN,A);
  60.   upDownServo.attach(UP_DOWN_SERVO_PIN,B);
  61.  
  62.   for(int i = 0; i < 6; i++) {
  63.     pinMode(DRAWING_PINS[i],INPUT);
  64.     digitalWrite(DRAWING_PINS[i],HIGH); // pull down resistors  
  65.   }
  66.  
  67.   //Serial.begin(9600);
  68.  
  69.     laserOn();
  70. }
  71.  
  72. void loop() {
  73.   for(int i = 0; i < 5; i++) {
  74.     if( digitalRead(DRAWING_PINS[i]) == HIGH ) {
  75.       drawPicture(i);
  76.       laserOff();
  77.       delay(DISPLAY_PAUSE);
  78.     }
  79.   }
  80. }
  81.  
  82. void drawPicture(int which) {
  83.   if( which == 0 ) {
  84.     fab();
  85.   }
  86.   else if( which == 1 ) {
  87.     house();
  88.   }
  89.   else if( which == 2 ) {
  90.     shaun();
  91.   }
  92.   else if( which == 3 ) {
  93.     grid();
  94.   }
  95.   else if( which == 4 ) {
  96.     face();
  97.   }
  98. //  else if( which == 5 ) {
  99. //    squiggle();
  100. //  }
  101. }
  102.  
  103. void fab() {
  104.   int X_OFFSET = 0;
  105.   int Y_OFFSET = 0;
  106.   int LETTER_LENGTH = 7;
  107.   int LETTER_HEIGHT = MAX_Y;
  108.  
  109.   // F
  110.   laserOff();
  111.   moveTo(X_OFFSET,Y_OFFSET);
  112.   laserOn();
  113.   moveTo(X_OFFSET,Y_OFFSET + LETTER_HEIGHT);
  114.   moveTo(X_OFFSET + LETTER_LENGTH,Y_OFFSET + LETTER_HEIGHT);
  115.   laserOff();
  116.   moveTo(X_OFFSET,Y_OFFSET + LETTER_HEIGHT/2);
  117.   laserOn();
  118.   moveTo(X_OFFSET + LETTER_LENGTH/2,Y_OFFSET + LETTER_HEIGHT/2);
  119.   laserOff();
  120.  
  121.   // A
  122.   moveTo(X_OFFSET + 2 * LETTER_LENGTH,Y_OFFSET);
  123.   laserOn();
  124.   moveTo(X_OFFSET + 2 * LETTER_LENGTH,Y_OFFSET + LETTER_HEIGHT);
  125.   moveTo(X_OFFSET + 3 * LETTER_LENGTH,Y_OFFSET + LETTER_HEIGHT);
  126.   moveTo(X_OFFSET + 3 * LETTER_LENGTH,Y_OFFSET);
  127.   laserOff();
  128.   moveTo(X_OFFSET + 2 * LETTER_LENGTH,Y_OFFSET + LETTER_HEIGHT/2);
  129.   laserOn();
  130.   moveTo(X_OFFSET + 3 * LETTER_LENGTH,Y_OFFSET + LETTER_HEIGHT/2);
  131.   laserOff();
  132.  
  133.   // B
  134.   moveTo(X_OFFSET + 5 * LETTER_LENGTH,Y_OFFSET);
  135.   laserOn();
  136.   moveTo(X_OFFSET + 5 * LETTER_LENGTH,Y_OFFSET+LETTER_HEIGHT);
  137.   moveTo(X_OFFSET + 6 * LETTER_LENGTH,Y_OFFSET+LETTER_HEIGHT/4 * 3);
  138.   moveTo(X_OFFSET + 5 * LETTER_LENGTH,Y_OFFSET+LETTER_HEIGHT/4 * 2);
  139.   moveTo(X_OFFSET + 6 * LETTER_LENGTH,Y_OFFSET+LETTER_HEIGHT/4 * 1);
  140.   moveTo(X_OFFSET + 5 * LETTER_LENGTH,Y_OFFSET);
  141. }
  142.  
  143. void house() {
  144.   int X_OFFSET = 0;
  145.   int Y_OFFSET = 0;
  146.  
  147.   laserOff();
  148.  
  149.   // base
  150.   moveTo(MAX_X/4,MIN_Y);
  151.   laserOn();
  152.   moveTo(MAX_X/4*3,MIN_Y);
  153.   moveTo(MAX_X/4*3,MIN_Y+MAX_Y/2);
  154.   moveTo(MAX_X/4,MIN_Y+MAX_Y/2);
  155.   moveTo(MAX_X/4,MIN_Y);
  156.  
  157.   laserOff();
  158.  
  159.   // roof
  160.   moveTo(MAX_X/4,MIN_Y+MAX_Y/2);
  161.   laserOn();
  162.   moveTo(MAX_X/2,MAX_Y);
  163.   moveTo(MAX_X/4*3,MIN_Y+MAX_Y/2);
  164. }
  165.  
  166. void shaun() {
  167.   int LETTER_LENGTH = 4;
  168.   int LETTER_HEIGHT = MAX_Y;
  169.   int X_OFFSET = 4;
  170.  
  171.   laserOff();
  172.  
  173.   // S
  174.   moveTo(X_OFFSET,MIN_Y);
  175.   laserOn();
  176.   moveTo(X_OFFSET+LETTER_LENGTH,MIN_Y);
  177.   moveTo(X_OFFSET+LETTER_LENGTH,MIN_Y + MAX_Y/2);
  178.   moveTo(X_OFFSET,MIN_Y + MAX_Y/2);
  179.   moveTo(X_OFFSET,MAX_Y);
  180.   moveTo(X_OFFSET+LETTER_LENGTH,MAX_Y);
  181.  
  182.   laserOff();
  183.  
  184.   moveTo(X_OFFSET+LETTER_LENGTH*2,MIN_Y);
  185.   laserOn();
  186.   moveTo(X_OFFSET+LETTER_LENGTH*2,MAX_Y);
  187.   laserOff();
  188.   moveTo(X_OFFSET+LETTER_LENGTH*3,MIN_Y);
  189.   laserOn();
  190.   moveTo(X_OFFSET+LETTER_LENGTH*3,MAX_Y);
  191.   laserOff();
  192.   moveTo(X_OFFSET+LETTER_LENGTH*2,MAX_Y/2);
  193.   laserOn();
  194.   moveTo(X_OFFSET+LETTER_LENGTH*3,MAX_Y/2);
  195.  
  196.   laserOff();
  197.  
  198.   // A
  199.   moveTo(X_OFFSET + 4 * LETTER_LENGTH,MIN_Y);
  200.   laserOn();
  201.   moveTo(X_OFFSET + 4 * LETTER_LENGTH,MIN_Y + LETTER_HEIGHT);
  202.   moveTo(X_OFFSET + 5 * LETTER_LENGTH,MIN_Y + LETTER_HEIGHT);
  203.   moveTo(X_OFFSET + 5 * LETTER_LENGTH,MIN_Y);
  204.   laserOff();
  205.   moveTo(X_OFFSET + 4 * LETTER_LENGTH,MIN_Y + LETTER_HEIGHT/2);
  206.   laserOn();
  207.   moveTo(X_OFFSET + 5 * LETTER_LENGTH,MIN_Y + LETTER_HEIGHT/2);
  208.   laserOff();
  209.  
  210.   // U
  211.   moveTo(X_OFFSET + 6 * LETTER_LENGTH,MAX_Y);
  212.   laserOn();
  213.   moveTo(X_OFFSET + 6 * LETTER_LENGTH,MIN_Y);
  214.   moveTo(X_OFFSET + 7 * LETTER_LENGTH,MIN_Y);
  215.   moveTo(X_OFFSET + 7 * LETTER_LENGTH,MAX_Y);
  216.   laserOff();
  217.  
  218.   // N
  219.   moveTo(X_OFFSET + 8 * LETTER_LENGTH,MIN_Y);
  220.   laserOn();
  221.   moveTo(X_OFFSET + 8 * LETTER_LENGTH,MAX_Y);
  222.   moveTo(X_OFFSET + 9 * LETTER_LENGTH,MIN_Y);
  223.   moveTo(X_OFFSET + 9 * LETTER_LENGTH,MAX_Y);
  224. }
  225.  
  226. void grid() {
  227.   laserOff();
  228.  
  229.   moveTo(0,0);
  230.   laserOn();
  231.  
  232.   for(int x = MIN_X; x <= MAX_X; x++) {
  233.      moveTo(x,x % 2 == 0 ? MIN_Y : MAX_Y);
  234.      moveTo(x,x % 2 == 0 ? MAX_Y : MIN_Y);
  235.   }
  236.  
  237.   laserOff();
  238.  
  239.   moveTo(0,0);
  240.   laserOn();
  241.  
  242.   for(int y = MIN_Y; y <= MAX_Y; y++) {
  243.      moveTo(y % 2 == 0 ? MIN_X : MAX_X,y);
  244.      moveTo(y % 2 == 0 ? MAX_X : MIN_X,y);
  245.   }
  246.  
  247. }
  248.  
  249. //void squiggle() {
  250. // laserOff();
  251. //
  252. // moveTo((int)(MAX_X * 0.05),MAX_Y);
  253. // laserOn();
  254. //
  255. // for(double per = 0.05; per <= 1; per += 0.05) {
  256. //   moveTo((int)(MAX_X * per),(int)(MAX_Y * (1 - 0)));
  257. //   moveTo((int)(MAX_X * 0.0),(int)(MAX_Y * (1 - per)));
  258. // }
  259. //}
  260.  
  261. void face() {
  262.  laserOff();
  263.  
  264.  // head
  265.  moveTo(MAX_X/4,MAX_Y/4);
  266.  laserOn();
  267.  moveTo(MAX_X/4*3,MAX_Y/4);
  268.  moveTo(MAX_X/4*3,8);
  269.  moveTo(MAX_X/4,8);
  270.  moveTo(MAX_X/4,MAX_Y/4);
  271.  
  272.  laserOff();
  273.  
  274.  // eyes / nose
  275.  moveTo(MAX_X/3,MAX_Y/3*2);
  276.  laserOn();
  277.  pause();
  278.  laserOff();
  279.  moveTo(MAX_X/3*2,MAX_Y/3*2);
  280.  laserOn();
  281.  pause();
  282.  laserOff();
  283.  moveTo(MAX_X/2,MAX_Y/2);
  284.  laserOn();
  285.  pause();
  286.  laserOff();
  287.  
  288.  // mouth
  289.  moveTo(MAX_X/3,MAX_Y/3);
  290.  laserOn();
  291.  moveTo(MAX_X/3*2,MAX_Y/3);
  292.  
  293. }
  294.  
  295. void moveTo(int x, int y) {
  296.   //Serial.print("moveTo(");
  297.   //Serial.print(x);
  298.   //Serial.print(",");
  299.   //Serial.print(y);
  300.   //Serial.println(");");
  301.   moveToX(x);
  302.   moveToY(y);
  303.   pause();
  304. }
  305.  
  306. void pause() {
  307.    //Serial.println("pause();");
  308.    //delay(DRAW_PAUSE);
  309.    long now = millis();
  310.    while(millis() - now < DRAW_PAUSE) {
  311.      SoftwareServo::refresh();
  312.    }
  313. }
  314.  
  315. void moveToX(int x) {
  316.   int angle = map(MAX_LEFT_RIGHT_COORDINATE-x,MIN_LEFT_RIGHT_COORDINATE,MAX_LEFT_RIGHT_COORDINATE,MIN_LEFT_RIGHT_ANGLE,MAX_LEFT_RIGHT_ANGLE);
  317.   leftRightServo.write(angle);
  318. }
  319.  
  320. void moveToY(int y) {
  321.   int angle = map(y,MIN_UP_DOWN_COORDINATE,MAX_UP_DOWN_COORDINATE,MIN_UP_DOWN_ANGLE,MAX_UP_DOWN_ANGLE);
  322.   upDownServo.write(angle);
  323. }
  324.  
  325. void laserOn() {
  326.   //Serial.println("laserOn();");
  327.   onOffServo.write(LASER_ON_ANGLE);
  328.   pause();
  329. }
  330.  
  331. void laserOff() {
  332.   //Serial.println("laserOff();");
  333.   onOffServo.write(LASER_OFF_ANGLE);
  334.   pause();
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement