/**********************************************************************
Filename : Drive Stepper Motor
Description : Use ULN2003 to drive the stepper motor.
Auther : www.freenove.com
Modification: 2020/07/11
**********************************************************************/
// Conncet the port of the stepper motor driver
int outPorts[] = {14, 27, 26, 25};
void setup() {
// set pins to output
for (int i = 0; i < 4; i++) {
pinMode(outPorts[i], OUTPUT);
}
}
void loop()
{
// Rotate a full turn
moveSteps(true, 32 * 64, 3);
delay(1000);
// Rotate a full turn towards another direction
moveSteps(false, 32 * 64, 3);
delay(1000);
}
//Suggestion: the motor turns precisely when the ms range is between 3 and 20
void moveSteps(bool dir, int steps, byte ms) {
for (unsigned long i = 0; i < steps; i++) {
moveOneStep(dir); // Rotate a step
delay(constrain(ms,3,20)); // Control the speed
}
}
void moveOneStep(bool dir) {
// Define a variable, use four low bit to indicate the state of port
static byte out = 0x01;
// Decide the shift direction according to the rotation direction
if (dir) { // ring shift left
out != 0x08 ? out = out << 1 : out = 0x01;
}
else { // ring shift right
out != 0x01 ? out = out >> 1 : out = 0x08;
}
// Output singal to each port
for (int i = 0; i < 4; i++) {
digitalWrite(outPorts[i], (out & (0x01 << i)) ? HIGH : LOW);
}
}
void moveAround(bool dir, int turns, byte ms){
for(int i=0;i<turns;i++)
moveSteps(dir,32*64,ms);
}
void moveAngle(bool dir, int angle, byte ms){
moveSteps(dir,(angle*32*64/360),ms);
}
/**********************************************************************
Filename : BreathingLight
Description : Make led light fade in and out, just like breathing.
Auther : www.freenove.com
Modification: 2020/07/11
**********************************************************************/
#define PIN_LED 2 //define the led pin
#define CHN 0 //define the pwm channel
#define FRQ 1000 //define the pwm frequency
#define PWM_BIT 8 //define the pwm precision
void setup() {
ledcSetup(CHN, FRQ, PWM_BIT); //setup pwm channel
ledcAttachPin(PIN_LED, CHN); //attach the led pin to pwm channel
}
void loop() {
for (int i = 0; i < 255; i++) { //make light fade in
ledcWrite(CHN, i);
delay(10);
}
for (int i = 255; i > -1; i--) { //make light fade out
ledcWrite(CHN, i);
delay(10);
}
}
0 件のコメント:
コメントを投稿