Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit stepperPosition sketch revision #3
- 12/5/2018
- Light sensor unknown? What kind of light sensor are you using, and how is it connected?
- */
- #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; // <---------------------------------------------------------------- Are you using the sensor's output on A0, or A5?
- // --------------------------------------------------------------------------------------------- In line #60 you are reading pin A5?
- const int analogOutPin = 8; // <-------------------------------------------------- what is this pin doing?
- 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
- pinMode(3, OUTPUT); //Photosensor
- Serial.println("exiting setup()");
- }
- void loop() {
- check_light_sensor();
- if (sensorValue < 100) {
- rotate_stepper_motor_negative();
- }
- // Note: now there is a dead spot from 100 to 150.
- if (sensorValue > 150) {
- rotate_stepper_motor_positive();
- }
- delay(1000);
- }
- void check_light_sensor() {
- //Serial.print("\n"); <------------------------------------------------ this is incorrect, you need single quotes to print a character, not double quotes
- sensorValue = analogRead(A5); // read input of analog pin 5 ?????????? why are you reading pin A5 here? where is the sensor's output connected to?
- 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.
- Serial.print("rotate negative");
- //show_stepper_start_position();
- //Serial.print("Night time value: ");
- //Serial.println(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.
- Serial.print("rotate positive");
- //show_stepper_start_position();
- //Serial.print("Day time value: ");
- //Serial.println(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"); <------------------------------------------------ this is incorrect, you need single quotes to print a character, not double quotes
- Serial.print("Current stepper position (END): ");
- Serial.println(stepperPos);
- }
Add Comment
Please, Sign In to add comment