Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit stepperPosition sketch revision #1
- 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 = 0;
- void setup() {
- // Setting pins for startup lights
- Serial.begin(9600);// Starts serial data transfer at 9600bps
- 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
- }
- void loop() {
- Serial.print("\n");
- int sensorValue = analogRead(A5); // read input of analog pin 5
- pwmValue = map(sensorValue, 0, 1023, 0, 255); //mapping sensor values into proportional PWM values, from 0-1023, to 0-255
- // print results from sensor to monitor
- Serial.print("PWMValue: "); Serial.println(pwmValue); //print PWMValue (0 to 255) to serial
- //(analogOutPin, pwmValue); //write PWMValue to digital pins..
- delay(1000);
- // int stepperPos1 = stepperPos;
- // I took this line above out, because it serves no purpose?
- // The stepperPos variable is initialized to zero anyway, and you were never changing the value of it.
- // In the code, you seem to be trying to increment and decrement ONE variable,
- // when you are mixing up references to two different variables,
- // one of which is left at zero-value. So you don't need the zero-value one at all.
- Serial.print("Current stepper position(START): "); Serial.println(stepperPos); // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Changed stepperPos1 to stepperPos
- digitalWrite(8, HIGH);
- delay(1000);
- digitalWrite(8, LOW);
- delay(1000);
- digitalWrite(9, HIGH);
- delay(1000);
- digitalWrite(9, LOW);
- delay(1000);
- digitalWrite(10, HIGH);
- delay(1000);
- digitalWrite(10, LOW);
- delay(1000);
- digitalWrite(11, HIGH);
- delay(1000);
- digitalWrite(11, LOW);
- delay(1000);
- if (sensorValue < 115)
- {
- Serial.print("Night time value: ");
- Serial.print(sensorValue);//print value read to the debug screen
- stepper.setSpeed(16); // 16 rpm
- stepper.step(-4076); // Complete 4076 steps, 2 RPM in the other direction (-)
- // int stepperPos = stepperPos - 4076; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! this line was wrong; using the 'int' type creates a local variable
- stepperPos = stepperPos - 4076; // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ removed type declaration
- }
- if (sensorValue > 116); // else refused to work here..
- /*
- * I think the "else" was working correct, but the error with stepperPos/stepperPos1 was the problem you were having.
- * I left this the same anyway however.
- */
- {
- Serial.print("Day time value: ");
- Serial.print(sensorValue);//print value read to the debug screen
- stepper.setSpeed(16); // 16 rpm
- stepper.step(4076); // Complete 4076 steps, 2 RPM in the other direction
- // int stepperPos = stepperPos + 4076; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! this line was wrong; using the 'int' type creates a local variable
- stepperPos = stepperPos + 4076; // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ removed type declaration
- delay(1000);
- }
- //if (sensorValue > 116) // else refused to work here..
- //{
- //Serial.print("Day time value: ");
- //Serial.print(sensorValue);//print value read to the debug screen
- //stepper.setSpeed(16); // 16 rpm
- //stepper.step(4076); // Complete 4076 steps, 2 RPM in the other direction
- //delay(1000);
- //}
- Serial.print("\n");
- Serial.print("Current stepper position (END): "); Serial.println(stepperPos); // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Changed stepperPos1 to stepperPos
- }
Add Comment
Please, Sign In to add comment