skizziks_53

reddit_stepperPosition_12_7_2018_v3

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