Toy elevator
Print Profile(1)

Description
给小朋友做的一个电梯玩具。需要用到
28byj-48 step motor
Arduino board
string
connection wires
2 buttons
code
#include <Stepper.h>
const int buttonPin2 = 2;
const int buttonPin1 = 3;
int buttonState1 = 0;
int buttonState2 = 0;
// Define the number of steps per revolution (for 28BYJ-48, typically 512 steps for one full revolution)
const int stepsPerRevolution = 512; // 512 steps per revolution for the 28BYJ-48 stepper motor
// Initialize the Stepper library with the pins connected to the ULN2003 driver
Stepper stepper(stepsPerRevolution, 8, 10, 9, 11); // IN1, IN2, IN3, IN4
void setup() {
// Set the motor speed (in RPM)
stepper.setSpeed(65); // Adjust the speed, 15 RPM is a good starting point
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
Serial.print(buttonState1);
Serial.print(buttonState2);
if (buttonState1 == LOW) {
// Rotate clockwise
Serial.println("Rotating Clockwise");
stepper.step(stepsPerRevolution); // Rotate 1 full revolution clockwise
}else if (buttonState2 == LOW) {
// Rotate counter-clockwise
Serial.println("Rotating Counter-Clockwise");
stepper.step(-stepsPerRevolution); // Rotate 1 full revolution counter-clockwise
} else {
stepper.step(0);
}
}
Documentation (1)







Comment & Rating (3)