This week we do some demonstrations on how to make a motorized fader move to where you want it to go.
Downloads
Motorized Fader Test 1
/*######################### MOTORIZED FADER TEST 1 ############################
This fader test program will set the fader to one of 2 positions based on
the button that is pressed.
For more examples of stepper motor control refer to the show notes at
http://tech-zen.tv.
From: Mike Myers (http://mikemyers.me) @netnutmike
Let's Make It Episode 38 at http://tech-zen.tv
http://tech-zen.tv
For the sample code, show notes, contact information and many more
videos, visit the show page at http://tech-zen.tv/letsmakeit
Please subscribe to our YouTube channel or our netcasts at any of
your favorite netcast / podcast outlets.
We normally record Let's Make It live on Monday evenings around
9pm eastern. You can watch it live by going to tech-zen.tv and clicking
the live link at the top of the page.
We also have a community setup for our viewers and listeners. You can
join the community by going to community.tech-zen.tv.
We love input on what you would like to know or if you have an idea for
a new Let's Make it episode, you can contact us via email or phone at
the show information page.
################################################################################*/
#include <AccelStepper.h>
#include <AFMotor.h>
#define forward 36
#define backward 38
int sensorPin = A8;
int sensorValue = 0;
int motorspeed = 250;
AF_DCMotor motor(3);
void setup()
{
pinMode(forward, INPUT_PULLUP);
pinMode(backward, INPUT_PULLUP);
motor.setSpeed(motorspeed);
motor.run(RELEASE);
Serial.begin(9600);
}
void loop()
{
if (!digitalRead(forward)) {
Serial.println("F");
setFaderPos(75);
} else if (!digitalRead(backward)) {
Serial.println("B");
setFaderPos(20);
} else {
motor.run(RELEASE);
}
}
void setFaderPos(int pos)
{
int curpos;
curpos = map(analogRead(sensorPin), 480, 1024, 0, 100);
while (abs(curpos - pos) > 1) {
if (curpos > pos)
motor.run(FORWARD);
else if (curpos < pos)
motor.run(BACKWARD);
curpos = map(analogRead(sensorPin), 480, 1024, 0, 100);
}
motor.run(RELEASE);
}
Motorized Fader Test2
/*######################### MOTORIZED FADER TEST 2 ############################
This sketch takes the encoder code from Episode 35 and uses it to control
a stepper motor. We turn the encoder clockwise and the stepper motor turns
clockwise as well. Same thing for turning the encoder counter clockwise.
From: Mike Myers (http://mikemyers.me) @netnutmike
Let's Make It Episode 38 at http://tech-zen.tv
http://tech-zen.tv
For the sample code, show notes, contact information and many more
videos, visit the show page at http://tech-zen.tv/letsmakeit
Please subscribe to our YouTube channel or our netcasts at any of
your favorite netcast / podcast outlets.
We normally record Let's Make It live on Monday evenings around
9pm eastern. You can watch it live by going to tech-zen.tv and clicking
the live link at the top of the page.
We also have a community setup for our viewers and listeners. You can
join the community by going to community.tech-zen.tv.
We love input on what you would like to know or if you have an idea for
a new Let's Make it episode, you can contact us via email or phone at
the show information page.
################################################################################*/
#include <AccelStepper.h>
#include <AFMotor.h>
#define forward 36
#define backward 38
int sensorPin = A8;
int sensorValue = 0;
int motorspeed = 250;
AF_DCMotor motor(3);
void setup()
{
pinMode(forward, INPUT_PULLUP);
pinMode(backward, INPUT_PULLUP);
motor.setSpeed(motorspeed);
motor.run(RELEASE);
Serial.begin(9600);
}
void loop()
{
setFaderPos(0);
Serial.println("0");
delay(2000);
setFaderPos(33);
Serial.println("33");
delay(2000);
setFaderPos(75);
Serial.println("75");
delay(2000);
setFaderPos(100);
Serial.println("100");
delay(2000);
setFaderPos(25);
Serial.println("25");
delay(2000);
setFaderPos(60);
Serial.println("60");
delay(2000);
if (!digitalRead(forward)) {
Serial.println("F");
setFaderPos(0);
} else if (!digitalRead(backward)) {
Serial.println("B")
setFaderPos(100);
} else {
motor.run(RELEASE);
//Serial.println(analogRead(sensorPin));
}
}
void setFaderPos(int pos)
{
int curpos;
curpos = map(analogRead(sensorPin), 480, 1024, 0, 100);
while (abs(curpos - pos) > 1) {
if (curpos > pos)
motor.run(FORWARD);
else if (curpos < pos)
motor.run(BACKWARD);
curpos = map(analogRead(sensorPin), 480, 1024, 0, 100);
}
motor.run(RELEASE);
}