Skip to content

Instantly share code, notes, and snippets.

@freeformz
Created November 6, 2011 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save freeformz/1343483 to your computer and use it in GitHub Desktop.
Save freeformz/1343483 to your computer and use it in GitHub Desktop.
Arduino 101-02 Motor Control
int motorPin = 9;
int potPin = 0;
int lastValue = 0;
int motorSpeed = 0;
int delayTime = 250;
void setup() {
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
analogWrite(motorPin, motorSpeed);
}
void loop() {
int value = readPot();
updateSerial(value);
updateMotor(value);
delay(delayTime);
}
int readPot(){
return constrain(analogRead(potPin) / 4, 0, 255);
}
void updateSerial(int new_value){
if (lastValue != new_value) {
lastValue = new_value;
Serial.println(lastValue);
}
}
void updateMotor(int newSpeed) {
if (motorSpeed != newSpeed) {
motorSpeed = newSpeed;
analogWrite(motorPin, motorSpeed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment