Created
May 14, 2020 16:37
-
-
Save TheNathanSpace/d13b8320df4a378ae53442f63e4230b9 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
// Program: Sample Arduino code for a flying fader | |
// Purpose: Demonstrating how to not use blocking code when moving a motor back and forth based on a analog sensor input | |
// Programmer: LRTNZ | |
// Date: 13/05/2020 | |
// |------------| | |
// | Pin Values | | |
// |------------| | |
// Motor Pins | |
const int ENABLE_PIN = 2; | |
const int DIRECTION_ONE = 3; | |
const int DIRECTION_TWO = 4; | |
// Sensor Pins | |
const int FADER_POT = A15; | |
// |--------------| | |
// | Motor Values | | |
// |--------------| | |
// Motor direction enum - This allows us to store multiple different states in a variable, in a more usable way. | |
enum motorDirection { | |
forwards, | |
stationary, | |
reverse | |
}; | |
// Motor state | |
motorDirection direction = stationary; | |
bool running = false; | |
// |--------------| | |
// | Fader Values | | |
// |--------------| | |
// Limits for the travel, based on the potentiometer. Set to be slightly less/greater than the max/min, to allow for momentum in the moving fader. | |
const int MIN_FADER_VALUE = 1000; // Must be above 0, to be effective | |
const int MAX_FADER_VALUE = 31000; // Must be less than the maximum value, to be effective | |
// Fader Value | |
int faderValue = 0; | |
// If the fader is at a limit or not | |
bool faderAtLimit = false; | |
// |-----------| | |
// | Main Code | | |
// |-----------| | |
void setup() { | |
// put your setup code here, to run once: | |
// Start the serial stream | |
Serial.begin(9600); | |
Serial.println("Fader Demo with Limits Started"); | |
// Tells the arduino that these pins are output pins | |
pinMode(DIRECTION_TWO, OUTPUT); | |
pinMode(DIRECTION_ONE, OUTPUT); | |
pinMode(ENABLE_PIN, OUTPUT); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
// Checks whether the fader is at it's limit or not | |
faderAtLimit = checkSliderLimit(); | |
// If the fader is at a limit, stop the motor | |
if(faderAtLimit){ | |
setDirection(stationary); | |
} | |
// If the motor is not running - The fader has reached a limit | |
if(running == false){ | |
// If the current direction the motor was travelling was going forwards, set it to reverse | |
if(direction == forwards){ | |
Serial.println("Started the motor going backwards"); | |
// Set direction to be the direction enum reverse value | |
direction = reverse; | |
// Call to the method to set the direction pins of the H bridge. | |
setDirection(direction); | |
// Start the motor at max speed | |
startMotor(128); | |
// If the motor was going in reverse (Or is stationary as at the start of the program), change it to run forwards | |
} else { | |
Serial.println("Started the motor going forwards"); | |
// Set direction to be the direction enum forwards value | |
direction = forwards; | |
// Call to the method to set the direction pins of the H bridge. | |
setDirection(direction); | |
// Start the motor at max speed | |
startMotor(128); | |
} | |
} | |
} | |
// Returns true if the fader is at, or passed a limit in either direction | |
bool checkSliderLimit(){ | |
// Get the current value of the fader position | |
int deadzonethrottle = 25; | |
faderValue = constrain(map(analogRead(FADER_POT),1023-deadzonethrottle,0+deadzonethrottle,0,32767),0,32767); | |
Serial.println(faderValue); | |
// Checks for max travel limit, and if the motor is travelling in the direction where the max limit will apply | |
if(faderValue >= MAX_FADER_VALUE && direction == forwards){ | |
Serial.println("Fader Reached Max Travel Limit"); | |
return true; | |
} | |
// Checks for min travel limit, and if the motor is travelling in the direction where the min limit will apply | |
if(faderValue <= MIN_FADER_VALUE && direction == reverse){ | |
Serial.println("Fader Reached Min Travel Limit"); | |
return true; | |
} | |
// If neither of above are true, the fader is not at a limit, so return false | |
return false; | |
} | |
// Starts the motor at the passed in speed | |
void startMotor(int speed){ | |
// Starts the PWM on the enable pin | |
analogWrite(ENABLE_PIN, speed); | |
running = true; | |
} | |
// Method to set the direction pins of the H bridge, from the provided input | |
void setDirection(motorDirection directionVal){ | |
// Switch based on the direction value. Think of this as a list of "if else" statements. | |
// By using the enum we have been able to use this, while still also maintaining human readability | |
switch (directionVal){ | |
// If the direction is forwards | |
case forwards: | |
Serial.println("Changed motor to run forwards"); | |
// Set the pins for the motor to run forwards | |
digitalWrite(DIRECTION_ONE, true); | |
digitalWrite(DIRECTION_TWO, false); | |
// Escape from the switch statement | |
break; | |
// If the direction is reverse | |
case reverse: | |
Serial.println("Changed motor to run in reverse"); | |
// Set the pins for the motor to run in reverse | |
digitalWrite(DIRECTION_ONE, false); | |
digitalWrite(DIRECTION_TWO, true); | |
// Escape | |
break; | |
// If the direction is stationary | |
case stationary: | |
Serial.println("Changed motor to stop"); | |
// Set the enable pin to 0, to stop the motor moving | |
analogWrite(ENABLE_PIN, 0); | |
// No longer running the motor, so need to update the running variable. | |
running = false; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment