Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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