Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit stepperPosition sketch revision #2
- 12/5/2018
- */
- #include <Stepper.h>
- #define STEPS 2038 // steps in one revolution (RPM) (28BYJ-48)
- Stepper stepper(STEPS, 8, 10, 9, 11); // stepper digital pins on arduino
- const int analogInPin = A0;
- const int analogOutPin = 8;
- int sensorValue = 0;
- int pwmValue; // setting to an integer
- int stepperPos = 4076;
- // This version allows the stepper to turn eight rotations either direction from the initial starting position.
- long minimum_stepper_position = -16304; // This is eight turns 'below' zero.
- long maximum_stepper_position = 16304; // This is eight turns 'above' zero.
- void setup() {
- // Setting pins for startup lights
- Serial.begin(9600);// Starts serial data transfer at 9600bps
- /*
- The regular Stepper library sets the pinMode for the stepper motor pins itself, you don't need to do that.
- pinMode(8, OUTPUT); //onboard driver LED
- pinMode(9, OUTPUT); //onboard driver LED
- pinMode(10, OUTPUT); //onboard driver LED
- pinMode(11, OUTPUT); //onboard driver LED
- */
- pinMode(3, OUTPUT); //Photosensor
- Serial.print("exiting setup()");
- }
- void loop() {
- check_light_sensor();
- if (sensorValue < 115) {
- rotate_stepper_motor_negative();
- }
- // Note: the way that both of these if() statements compare the same value, the motor is always going to turn one way or the other.
- // You would need to use different values for the two statements to leave a 'dead spot' that would let the motor sit idle when the light
- // was at an acceptable level.
- if (sensorValue >= 115) { // Change this value 115 to maybe 165 to leave a 'dead spot'.
- rotate_stepper_motor_positive();
- }
- delay(1000);
- }
- void check_light_sensor() {
- Serial.print("\n");
- sensorValue = analogRead(A5); // read input of analog pin 5
- pwmValue = map(sensorValue, 0, 1023, 0, 255);
- Serial.print("PWMValue: "); Serial.println(pwmValue); //print PWMValue (0 to 255) to serial
- }
- void rotate_stepper_motor_negative() {
- // This function turns the motor up to 2 rotations negative, unless the lower-end limit has been reached.
- // If the motor is already at the lower end, then it won't try to turn any more in that direction.
- show_stepper_start_position();
- Serial.print("Night time value: ");
- Serial.print(sensorValue);//print value read to the debug screen
- stepper.setSpeed(16); // 16 rpm
- int stepCounter = 0;
- int stepTarget = 4076;
- while (stepCounter < stepTarget) { // This condition limits movement to 2 rotations at the most.
- stepCounter = stepCounter + 1;
- if (stepperPos > minimum_stepper_position) { // This condition limits movement if the bottom end has been reached.
- stepperPos = stepperPos - 1;
- stepper.step(-1);
- }
- }
- show_stepper_end_position();
- }
- void rotate_stepper_motor_positive() {
- // This function turns the motor up to 2 rotations positive, unless the upper-end limit has been reached.
- // If the motor is already at the upper end, then it won't try to turn any more in that direction.
- show_stepper_start_position();
- Serial.print("Day time value: ");
- Serial.print(sensorValue);//print value read to the debug screen
- stepper.setSpeed(16); // 16 rpm
- int stepCounter = 0;
- int stepTarget = 4076;
- while (stepCounter < stepTarget) { // This condition limits movement to 2 rotations at the most.
- stepCounter = stepCounter + 1;
- if (stepperPos < maximum_stepper_position) { // This condition limits movement if the top end has been reached.
- stepperPos = stepperPos + 1;
- stepper.step(1);
- }
- }
- show_stepper_end_position();
- }
- void show_stepper_start_position() {
- Serial.print("Current stepper position(START): "); Serial.println(stepperPos);
- }
- void show_stepper_end_position() {
- Serial.print("\n");
- Serial.print("Current stepper position (END): "); Serial.println(stepperPos);
- }
Add Comment
Please, Sign In to add comment