Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
file 32 lines (24 sloc) 0.716 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*

Ultimate Microcontroller Pack Sample Code - Using a 5V Motor with the Arduino
For build instructions, head over to Make Projects: http://makeprojects.com/Project/Control+a+5V+Motor+with+the+Arduino/3270/1#.UQxOI1pdfNo

Upon uploading, open the serial monitor and type in a motor speed, from 0 to 255.

*/
 
 
int motorPin = 3;
 
void setup()
{
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Speed 0 to 255");
}
 
 
void loop()
{
  if (Serial.available())
  {
    int speed = Serial.parseInt();
    if (speed >= 0 && speed <= 255) // This if statement will prevent the user from entering invalid motor speeds
    {
      analogWrite(motorPin, speed);
    }
  }
}
Something went wrong with that request. Please try again.