Created
November 6, 2011 21:03
-
-
Save freeformz/1343483 to your computer and use it in GitHub Desktop.
Arduino 101-02 Motor Control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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