Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void setup() {
- pinMode(3, OUTPUT);
- pwm_init();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- //slowly ramp up speed until it reaches the maximum value
- for(int i = 0; i < 255; i+=10) {
- setSpeed(i);
- delay(100);
- }
- delay(1000);
- }
- /*Initialize pwm output on pin 3
- *This function should be placed in the setup to run once
- */
- void pwm_init() {
- TCCR2A |= ( _BV(COM2A1)| _BV(COM2B1) | _BV(WGM21) | _BV(WGM20) );
- TCCR2B |= ( _BV(WGM22) | _BV(CS22) | _BV(CS21) | _BV(CS22) );
- PRR &= ~_BV(PRTIM2);
- OCR2A = 124; //min value OCR2A should be
- }
- /*
- * Set the speed of the motors, it will map the input into a range that the stepper motors can support
- * Motors will stop moving at a speed of 0 and increase linearly from there
- * INPUTS: 8bit number 0-255
- * OUTPUTS: None
- */
- void setSpeed( uint8_t speed ) {
- //Absolute fastest it can go is 2ms
- //Speed is set by 16000000/500/1024
- if( speed == 0 ){
- OCR2A = 0;
- } else {
- int val = 0;
- val = map(speed, 0,255, 255, 124);
- OCR2A = val;
- }
- }
Add Comment
Please, Sign In to add comment