0% found this document useful (0 votes)
28 views

Motor

The document contains code to control a motor's speed and movement in different ways. It includes a function to turn the motor on at a set speed for a duration, then off at a different speed for another duration. Another function accelerates the motor from stopped to full speed over time, then decelerates back to stopped. Both functions use analogWrite to gradually change the motor's speed through values from 0 to 255.

Uploaded by

api-277007871
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Motor

The document contains code to control a motor's speed and movement in different ways. It includes a function to turn the motor on at a set speed for a duration, then off at a different speed for another duration. Another function accelerates the motor from stopped to full speed over time, then decelerates back to stopped. Both functions use analogWrite to gradually change the motor's speed through values from 0 to 255.

Uploaded by

api-277007871
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

* motorOnThenOffWithSpeed() - turns motor on then off but uses


speed values as well
* (notice this code is identical to the code we used for
* the blinking LED)
*/
void motorOnThenOffWithSpeed(){
int onSpeed = 200; // a number between 0 (stopped) and 255 (full
speed)
int onTime = 2500; //the number of milliseconds for the motor to
turn on for
int offSpeed = 50; // a number between 0 (stopped) and 255 (full
speed)
int offTime = 1000; //the number of milliseconds for the motor to turn
off for
analogWrite(motorPin, onSpeed); // turns the motor On
delay(onTime);

// waits for onTime milliseconds

analogWrite(motorPin, offSpeed); // turns the motor Off


delay(offTime);
}

// waits for offTime milliseconds

/*
* motorAcceleration() - accelerates the motor to full speed then
* back down to zero
*/

void motorAcceleration(){
int delayTime = 50; //milliseconds between each speed step
//Accelerates the motor
for(int i = 0; i < 256; i++){ //goes through each speed from 0 to 255
analogWrite(motorPin, i); //sets the new speed
delay(delayTime);
}

// waits for delayTime milliseconds

//Decelerates the motor


for(int i = 255; i >= 0; i--){ //goes through each speed from 255 to 0
analogWrite(motorPin, i); //sets the new speed
delay(delayTime);
}
}

// waits for delayTime milliseconds

You might also like