Guest User

Laser Engraved Art

a guest
Oct 27th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <Servo.h>
  2. #include <math.h>
  3.  
  4. #define MOTOR_SPEED 1161 //RPM: took 310ms to rotate 6 times
  5. #define CIRCLE 2*M_PI/360
  6. #define PERIOD 50 //how many milliseconds pass before calling main
  7. #define MAX_SPEED 0 //if 1, will go as fast as possible
  8. #define MOTOR_SPEED 5 //how quickly the motor moves through its motions //also dependant on period
  9. #define FUNC_SWITCH_PERIOD 5000 //in milliseconds
  10. #define DIRECTION_SWITCH_PERIOD 10000
  11. #define FUNC_SWITCH_SHIFT 2500 //the shift forward of the function switcher so that it doesn't switch at thr same time as the direction switch
  12.  
  13. int motor_angle = 0; //the motor angle can point between 0 (pointing directly down) and about 45 (pointing midway down to the horizontal plane)
  14. int servo_pin = 11; //digital
  15. int laser_pin = 10; //digital
  16. double x = 360;
  17. double millis_time = millis();
  18. int laser_on = 0;
  19. int dir = 0;
  20. int function_type = 0;
  21. double switch_func_timer = FUNC_SWITCH_SHIFT;
  22. double switch_dir_timer = 0;
  23.  
  24. Servo servo;
  25.  
  26. void laser_switch() {
  27.   if (laser_on) {
  28.     digitalWrite(laser_pin, 0);
  29.     laser_on = 0;
  30.   }
  31.   else {
  32.     digitalWrite(laser_pin, 1);
  33.     laser_on = 1;
  34.   }
  35. }
  36.  
  37. //### functions
  38. double sine_waveform(double x) {
  39.   double angle = 22.5+22.5*sin(x);  //need to generate a sin wave with peak to peak amplitude of 45 and a vertical shift of 22.5 degrees
  40.   return(angle);
  41. }
  42.  
  43. double square_waveform(double x) {
  44.   double angle = 0;
  45.   angle += 45*(((int)x % 360) % 2);
  46.   return(angle);
  47. }
  48.  
  49. double stepwise_waveform(double x) {
  50.   double angle = 0;
  51.   angle += 15*(((int)x % 360) % 4);
  52.   return(angle);
  53. }
  54.  
  55. //###
  56.  
  57. void function_switch() {
  58.   function_type++;
  59.   function_type = function_type % 3;
  60. }
  61.  
  62. void dir_switch() {
  63.   dir += 1;
  64.   dir = dir % 2;
  65. }
  66.  
  67. void setup() {
  68.   // put your setup code here, to run once:
  69.   pinMode(laser_pin, OUTPUT);
  70.   Serial.begin(9600);
  71.   servo.attach(servo_pin);
  72.   servo.write(0); //starting angle is pointing midway between max and min angle
  73.   delay(100);
  74.   laser_switch();
  75. }
  76.  
  77. void loop() {
  78.   // put your main code here, to run repeatedly:
  79.   if (MAX_SPEED || (millis()-millis_time) > PERIOD) {
  80.     millis_time = millis();
  81.  
  82.     //direction change using y
  83.     if (dir = 0) {
  84.       x += MOTOR_SPEED * CIRCLE;
  85.     }
  86.     else {
  87.       x -= MOTOR_SPEED * CIRCLE;
  88.     }
  89.  
  90.     //function call based on current function type
  91.     if (function_type == 0) {
  92.       motor_angle = sine_waveform(x);
  93.     } else if (function_type == 1) {
  94.       motor_angle = square_waveform(x);
  95.     } else if (function_type == 2) {
  96.       motor_angle = stepwise_waveform(x);
  97.     }
  98.     servo.write(motor_angle);
  99.   }
  100.   if (millis()-switch_func_timer > FUNC_SWITCH_PERIOD) {
  101.     function_switch();
  102.     switch_func_timer = millis();
  103.   }
  104.   if (millis() - switch_dir_timer > DIRECTION_SWITCH_PERIOD) {
  105.     dir_switch();
  106.     switch_dir_timer = millis();
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment