skizziks_53

reddit_stepperPosition_12_5_2018_v2

Dec 5th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. /*
  2.    Reddit stepperPosition sketch revision #2
  3.    12/5/2018
  4. */
  5.  
  6. #include <Stepper.h>
  7. #define STEPS 2038 // steps in one revolution (RPM) (28BYJ-48)
  8. Stepper stepper(STEPS, 8, 10, 9, 11); // stepper digital pins on arduino
  9.  
  10. const int analogInPin = A0;
  11. const int analogOutPin = 8;
  12.  
  13. int sensorValue = 0;
  14. int pwmValue; // setting to an integer
  15. int stepperPos = 4076;
  16.  
  17. // This version allows the stepper to turn eight rotations either direction from the initial starting position.
  18. long minimum_stepper_position = -16304; // This is eight turns 'below' zero.
  19. long maximum_stepper_position = 16304; // This is eight turns 'above' zero.
  20.  
  21. void setup() {
  22.   // Setting pins for startup lights
  23.   Serial.begin(9600);// Starts serial data transfer at 9600bps
  24.  
  25.   /*
  26.     The regular Stepper library sets the pinMode for the stepper motor pins itself, you don't need to do that.
  27.     pinMode(8, OUTPUT); //onboard driver LED
  28.     pinMode(9, OUTPUT); //onboard driver LED
  29.     pinMode(10, OUTPUT); //onboard driver LED
  30.     pinMode(11, OUTPUT); //onboard driver LED
  31.   */
  32.   pinMode(3, OUTPUT); //Photosensor
  33.  
  34.   Serial.print("exiting setup()");
  35. }
  36.  
  37.  
  38.  
  39. void loop() {
  40.  
  41.   check_light_sensor();
  42.  
  43.   if (sensorValue < 115) {
  44.     rotate_stepper_motor_negative();
  45.   }
  46.  
  47.   // 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.
  48.   // 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
  49.   //    was at an acceptable level.
  50.  
  51.   if (sensorValue >= 115) { // Change this value 115 to maybe 165 to leave a 'dead spot'.
  52.     rotate_stepper_motor_positive();
  53.   }
  54.  
  55.   delay(1000);
  56.  
  57. }
  58.  
  59.  
  60.  
  61. void check_light_sensor() {
  62.   Serial.print("\n");
  63.   sensorValue = analogRead(A5); // read input of analog pin 5
  64.   pwmValue = map(sensorValue, 0, 1023, 0, 255);
  65.   Serial.print("PWMValue: "); Serial.println(pwmValue); //print PWMValue (0 to 255) to serial
  66. }
  67.  
  68.  
  69.  
  70. void rotate_stepper_motor_negative() {
  71.   // This function turns the motor up to 2 rotations negative, unless the lower-end limit has been reached.
  72.   // If the motor is already at the lower end, then it won't try to turn any more in that direction.
  73.   show_stepper_start_position();
  74.   Serial.print("Night time value: ");
  75.   Serial.print(sensorValue);//print value read to the debug screen
  76.   stepper.setSpeed(16); // 16 rpm
  77.   int stepCounter = 0;
  78.   int stepTarget = 4076;
  79.   while (stepCounter < stepTarget) { // This condition limits movement to 2 rotations at the most.
  80.     stepCounter = stepCounter + 1;
  81.     if (stepperPos > minimum_stepper_position) { // This condition limits movement if the bottom end has been reached.
  82.       stepperPos = stepperPos - 1;
  83.       stepper.step(-1);
  84.     }
  85.   }
  86.   show_stepper_end_position();
  87. }
  88.  
  89.  
  90.  
  91. void rotate_stepper_motor_positive() {
  92.   // This function turns the motor up to 2 rotations positive, unless the upper-end limit has been reached.
  93.   // If the motor is already at the upper end, then it won't try to turn any more in that direction.
  94.   show_stepper_start_position();
  95.   Serial.print("Day time value: ");
  96.   Serial.print(sensorValue);//print value read to the debug screen
  97.   stepper.setSpeed(16); // 16 rpm
  98.   int stepCounter = 0;
  99.   int stepTarget = 4076;
  100.   while (stepCounter < stepTarget) { // This condition limits movement to 2 rotations at the most.
  101.     stepCounter = stepCounter + 1;
  102.     if (stepperPos < maximum_stepper_position) { // This condition limits movement if the top end has been reached.
  103.       stepperPos = stepperPos + 1;
  104.       stepper.step(1);
  105.     }
  106.   }
  107.   show_stepper_end_position();
  108. }
  109.  
  110.  
  111.  
  112.  
  113. void show_stepper_start_position() {
  114.   Serial.print("Current stepper position(START): "); Serial.println(stepperPos);
  115. }
  116.  
  117.  
  118.  
  119.  
  120. void show_stepper_end_position() {
  121.   Serial.print("\n");
  122.   Serial.print("Current stepper position (END): "); Serial.println(stepperPos);
  123. }
Add Comment
Please, Sign In to add comment