I'm using the map function to transform the potentiometer value from analog pin 0 to between 0 and 30000 (milliseconds). The problem is that when the potentiometer is turned all the way down, the lowest output value I get is just above 1023 (it's currently at 1037). Which seems suspiciously close to the 1023 highest analog pin value. When I serial print pin 0 (Serial.println(analogRead(CAMERA_DELAY_PIN));
), the lowest value I get is around 300, not 0. I thought maybe my pot was just not working as expected, and so I tried just mapping from 300 to 1023 (e.g., map(analogRead(CAMERA_DELAY_PIN), 300, 1023, 0, 30000);
), but that does the opposite of what I would expect. Instead of getting an output value close to 0, it's coming closer to 2328.
I'd love to know if I'm doing something wrong. But maybe I just need to change the potentiometer.
Here is the mapping code: cameraPause = map(analogRead(CAMERA_DELAY_PIN), 0, 1023, 0, 30000);
/*
Author: GulJanjua
Date : December 30, 2018
Software Requirements:
----------------------
-Arduino IDE
-AccelStepper
Hardware Requirements:
----------------------
-Arduino
-Stepper motor driver
-
Project Requirents:
-------------------
1. Stop
2. Forward once for a given distance, then trigger the camera.
3. Reverse once for a given distance, then trigger the camera.
4. Forward infinitely: move a given distance, then pause for x milliseconds while the camera is triggered, then repeat.
5. Reverse infinitely: move a given distance, then pause for x milliseconds while the camera is triggered, then repeat.
*/
#include <AccelStepper.h>
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);
int period = 500; // delay time for serial printing pot value to monitor. Increasing this will slow down when button changes are responded to and thus they won't act as expected.
unsigned long time_now = 0; // start millis time at 0
#define ledPin 10
#define STOP_PIN 2
#define LEFT_ONE_PIN 5
#define RIGHT_ONE_PIN 6
#define LEFT_PIN 3
#define RIGHT_PIN 4
// #define SPEED_PIN 0
// #define MAX_SPEED 1000
// #define MIN_SPEED 0.01
#define CAMERA_DELAY_PIN 0
#define MAX_DEALY 30000
#define MIN_DELAY 250
#define STEPS1 10 //STEPS in one time rotation only
#define STEPS2 10 //STEPS in infinite rotation
#define preShutterPause 10 // Not really needed, but may give objects time to stop shaking.
#define shutterPause 250 // Allows shutter trigger to turn on only for this instance and not remain on
// #define cameraPause 3000 // pause while camera takes photo
int dir = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode(RIGHT_ONE_PIN, INPUT_PULLUP);
pinMode(LEFT_ONE_PIN, INPUT_PULLUP);
stepper1.setMaxSpeed(200.0);
stepper1.setAcceleration(500.0);
stepper1.setSpeed(200);
}
void loop() {
time_now = millis(); // used for timing of things that aren't using delay()
// SETTING CAMERA DELAY TIME VIA POT
static float cameraPause = 0.0; // Defines the camera pause time as numbers
cameraPause = map(analogRead(CAMERA_DELAY_PIN), 0, 1023, 0, 30000); // Scale the pot's value 0-1023 to what I want min to max delay time
while (millis() < time_now + period) { // Simply writing the current potentiomiter camera pause time on the serial monitor
Serial.println(analogRead(CAMERA_DELAY_PIN));
Serial.println(cameraPause);
}
// ONE MOVE ONLY
if (digitalRead(LEFT_ONE_PIN) == LOW) {
stepper1.runToNewPosition(STEPS1);
stepper1.setCurrentPosition(0);
delay(preShutterPause);
digitalWrite(ledPin , HIGH);
delay(shutterPause);
digitalWrite(ledPin , LOW);
delay(cameraPause);
dir = 0;
}
if (digitalRead(RIGHT_ONE_PIN) == LOW) {
stepper1.runToNewPosition(-STEPS1);
stepper1.setCurrentPosition(0);
delay(preShutterPause);
digitalWrite(ledPin , HIGH);
delay(shutterPause);
digitalWrite(ledPin , LOW);
delay(cameraPause);
dir = 0;
}
// INFINITE MOVES
if (digitalRead(LEFT_PIN) == LOW) {
dir = 1;
}
if (digitalRead(RIGHT_PIN) == LOW) {
dir = 2;
}
if (digitalRead(STOP_PIN) == LOW) {
dir = 0;
}
if (dir == 1) {
stepper1.runToNewPosition(STEPS2);
stepper1.setCurrentPosition(0);
delay(preShutterPause);
digitalWrite(ledPin , HIGH);
delay(shutterPause);
digitalWrite(ledPin , LOW);
delay(cameraPause);
}
if (dir == 2) {
stepper1.runToNewPosition(-STEPS2);
stepper1.setCurrentPosition(0);
delay(preShutterPause);
digitalWrite(ledPin , HIGH);
delay(shutterPause);
digitalWrite(ledPin , LOW);
delay(cameraPause);
}
}