Created
October 16, 2019 17:32
-
-
Save tirtawr/22e4207dae52036caa7e1785517303e5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#define STEPPER_PIN_1 9 | |
#define STEPPER_PIN_2 10 | |
#define STEPPER_PIN_3 11 | |
#define STEPPER_PIN_4 12 | |
#define POTENTIOMETER_PIN 0 | |
int stepNumber = 0; | |
int sensorValue = 0; | |
int mappedSensorValue = 0; | |
int delayValue = 2; | |
void setup() { | |
pinMode(STEPPER_PIN_1, OUTPUT); | |
pinMode(STEPPER_PIN_2, OUTPUT); | |
pinMode(STEPPER_PIN_3, OUTPUT); | |
pinMode(STEPPER_PIN_4, OUTPUT); | |
} | |
void loop() { | |
sensorValue = analogRead(POTENTIOMETER_PIN); | |
mappedSensorValue = map(sensorValue, 0, 1023, 2, 98); | |
if (mappedSensorValue > 55) { | |
stepMotor(false); | |
delayValue = 100 - mappedSensorValue; | |
} else if (mappedSensorValue < 45) { | |
stepMotor(true); | |
delayValue = mappedSensorValue; | |
} else { | |
// make the potentiometer more stable in the off position | |
delayValue = 20; | |
} | |
delay(delayValue); | |
} | |
void stepMotor(bool isClockwise) { | |
if(isClockwise) { | |
switch(stepNumber) { | |
case 0: | |
digitalWrite(STEPPER_PIN_1, HIGH); | |
digitalWrite(STEPPER_PIN_2, LOW); | |
digitalWrite(STEPPER_PIN_3, LOW); | |
digitalWrite(STEPPER_PIN_4, LOW); | |
break; | |
case 1: | |
digitalWrite(STEPPER_PIN_1, LOW); | |
digitalWrite(STEPPER_PIN_2, HIGH); | |
digitalWrite(STEPPER_PIN_3, LOW); | |
digitalWrite(STEPPER_PIN_4, LOW); | |
break; | |
case 2: | |
digitalWrite(STEPPER_PIN_1, LOW); | |
digitalWrite(STEPPER_PIN_2, LOW); | |
digitalWrite(STEPPER_PIN_3, HIGH); | |
digitalWrite(STEPPER_PIN_4, LOW); | |
break; | |
case 3: | |
digitalWrite(STEPPER_PIN_1, LOW); | |
digitalWrite(STEPPER_PIN_2, LOW); | |
digitalWrite(STEPPER_PIN_3, LOW); | |
digitalWrite(STEPPER_PIN_4, HIGH); | |
break; | |
} | |
} else { | |
switch(stepNumber) { | |
case 0: | |
digitalWrite(STEPPER_PIN_1, LOW); | |
digitalWrite(STEPPER_PIN_2, LOW); | |
digitalWrite(STEPPER_PIN_3, LOW); | |
digitalWrite(STEPPER_PIN_4, HIGH); | |
break; | |
case 1: | |
digitalWrite(STEPPER_PIN_1, LOW); | |
digitalWrite(STEPPER_PIN_2, LOW); | |
digitalWrite(STEPPER_PIN_3, HIGH); | |
digitalWrite(STEPPER_PIN_4, LOW); | |
break; | |
case 2: | |
digitalWrite(STEPPER_PIN_1, LOW); | |
digitalWrite(STEPPER_PIN_2, HIGH); | |
digitalWrite(STEPPER_PIN_3, LOW); | |
digitalWrite(STEPPER_PIN_4, LOW); | |
break; | |
case 3: | |
digitalWrite(STEPPER_PIN_1, HIGH); | |
digitalWrite(STEPPER_PIN_2, LOW); | |
digitalWrite(STEPPER_PIN_3, LOW); | |
digitalWrite(STEPPER_PIN_4, LOW); | |
} | |
} | |
stepNumber++; | |
if(stepNumber > 3) { | |
stepNumber = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment