skizziks_53

reddit_stepperPosition_12_5_2018

Dec 5th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. /*
  2.    Reddit stepperPosition sketch revision #1
  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 = 0;
  16.  
  17. void setup() {
  18.   // Setting pins for startup lights
  19.   Serial.begin(9600);// Starts serial data transfer at 9600bps
  20.   pinMode(8, OUTPUT); //onboard driver LED
  21.   pinMode(9, OUTPUT); //onboard driver LED
  22.   pinMode(10, OUTPUT); //onboard driver LED
  23.   pinMode(11, OUTPUT); //onboard driver LED
  24.   pinMode(3, OUTPUT); //Photosensor
  25. }
  26.  
  27. void loop() {
  28.   Serial.print("\n");
  29.   int sensorValue = analogRead(A5); // read input of analog pin 5
  30.   pwmValue = map(sensorValue, 0, 1023, 0, 255); //mapping sensor values into proportional PWM values, from 0-1023, to 0-255
  31.   // print results from sensor to monitor
  32.   Serial.print("PWMValue: "); Serial.println(pwmValue); //print PWMValue (0 to 255) to serial
  33.   //(analogOutPin, pwmValue); //write PWMValue to digital pins..
  34.   delay(1000);
  35.  
  36.   // int  stepperPos1 = stepperPos;
  37.  
  38.   // I took this line above out, because it serves no purpose?
  39.   // The stepperPos variable is initialized to zero anyway, and you were never changing the value of it.
  40.   // In the code, you seem to be trying to increment and decrement ONE variable,
  41.   //    when you are mixing up references to two different variables,
  42.   //    one of which is left at zero-value. So you don't need the zero-value one at all.
  43.  
  44.   Serial.print("Current stepper position(START): "); Serial.println(stepperPos); // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Changed stepperPos1 to stepperPos
  45.  
  46.   digitalWrite(8, HIGH);
  47.   delay(1000);
  48.   digitalWrite(8, LOW);
  49.   delay(1000);
  50.   digitalWrite(9, HIGH);
  51.   delay(1000);
  52.   digitalWrite(9, LOW);
  53.   delay(1000);
  54.   digitalWrite(10, HIGH);
  55.   delay(1000);
  56.   digitalWrite(10, LOW);
  57.   delay(1000);
  58.   digitalWrite(11, HIGH);
  59.   delay(1000);
  60.   digitalWrite(11, LOW);
  61.   delay(1000);
  62.  
  63.   if (sensorValue < 115)
  64.   {
  65.     Serial.print("Night time value: ");
  66.     Serial.print(sensorValue);//print value read to the debug screen
  67.     stepper.setSpeed(16); // 16 rpm
  68.     stepper.step(-4076); // Complete 4076 steps, 2 RPM in the other direction (-)
  69.     // int stepperPos = stepperPos - 4076; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! this line was wrong; using the 'int' type creates a local variable
  70.     stepperPos = stepperPos - 4076; // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ removed type declaration
  71.   }
  72.  
  73.   if (sensorValue > 116); // else refused to work here..
  74.   /*
  75.    * I think the "else" was working correct, but the error with stepperPos/stepperPos1 was the problem you were having.
  76.    * I left this the same anyway however.
  77.    */
  78.   {
  79.     Serial.print("Day time value: ");
  80.     Serial.print(sensorValue);//print value read to the debug screen
  81.     stepper.setSpeed(16); // 16 rpm
  82.     stepper.step(4076); // Complete 4076 steps, 2 RPM in the other direction
  83.     // int stepperPos = stepperPos + 4076; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! this line was wrong; using the 'int' type creates a local variable
  84.     stepperPos = stepperPos + 4076; // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ removed type declaration
  85.     delay(1000);
  86.   }
  87.  
  88.  
  89.   //if (sensorValue > 116) // else refused to work here..
  90.   //{
  91.   //Serial.print("Day time value: ");
  92.   //Serial.print(sensorValue);//print value read to the debug screen
  93.   //stepper.setSpeed(16); // 16 rpm
  94.   //stepper.step(4076); // Complete 4076 steps, 2 RPM in the other direction
  95.   //delay(1000);
  96.   //}
  97.   Serial.print("\n");
  98.   Serial.print("Current stepper position (END): "); Serial.println(stepperPos); // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Changed stepperPos1 to stepperPos
  99. }
Add Comment
Please, Sign In to add comment