Skip to content

Instantly share code, notes, and snippets.

@jonathanprozzi
Created March 25, 2017 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanprozzi/be56948a8b7aa24ae0f105a7cf73b3db to your computer and use it in GitHub Desktop.
Save jonathanprozzi/be56948a8b7aa24ae0f105a7cf73b3db to your computer and use it in GitHub Desktop.
/*
This sketch is from the Adafruit Arduino series by Simon Monk.
Additional comments added by Jonathan Prozzi.
Adafruit Arduino - Lesson 13. DC Motor
*/
int motorPin = 3; // set the pin that controls the motor. this is the one that attachs to the resistor
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600); // open the serial monitor so that you can input the speed value
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop()
{
if (Serial.available()) // as long as the Serial monitor is available, enter the motor speed
{
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255) // check to make sure that the value entered falls in the actual range
{
analogWrite(motorPin, speed); // this writes the speed value to the motor, causing it to rotate
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment