Guest User

Untitled

a guest
Dec 13th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. void setup() {
  2. pinMode(3, OUTPUT);
  3. pwm_init();
  4. }
  5.  
  6. void loop() {
  7. // put your main code here, to run repeatedly:
  8. //slowly ramp up speed until it reaches the maximum value
  9. for(int i = 0; i < 255; i+=10) {
  10. setSpeed(i);
  11. delay(100);
  12. }
  13. delay(1000);
  14. }
  15.  
  16. /*Initialize pwm output on pin 3
  17. *This function should be placed in the setup to run once
  18. */
  19. void pwm_init() {
  20. TCCR2A |= ( _BV(COM2A1)| _BV(COM2B1) | _BV(WGM21) | _BV(WGM20) );
  21. TCCR2B |= ( _BV(WGM22) | _BV(CS22) | _BV(CS21) | _BV(CS22) );
  22. PRR &= ~_BV(PRTIM2);
  23. OCR2A = 124; //min value OCR2A should be
  24. }
  25.  
  26. /*
  27. * Set the speed of the motors, it will map the input into a range that the stepper motors can support
  28. * Motors will stop moving at a speed of 0 and increase linearly from there
  29. * INPUTS: 8bit number 0-255
  30. * OUTPUTS: None
  31. */
  32. void setSpeed( uint8_t speed ) {
  33. //Absolute fastest it can go is 2ms
  34. //Speed is set by 16000000/500/1024
  35. if( speed == 0 ){
  36. OCR2A = 0;
  37. } else {
  38. int val = 0;
  39. val = map(speed, 0,255, 255, 124);
  40. OCR2A = val;
  41. }
  42. }
Add Comment
Please, Sign In to add comment