Hi
I have been building a camera slider. There are two modes to the operation of the slider. One is manual using two push switches and the other is automatic using limit switches to change the direction of the stepper motor.
I used the the blink without delay method for timing so that other operations can happen.
The problem is that I have introduced a rotary switch so that I can change the speed of the stepper motor.
The rotary switch, consists of 1k resistors in series, with the different pole positions having different resistor reading, is an analog sensor and I have mapped the incoming variable.
I first adapt the blink with out delay sketch to changing the speed of the flashing led with the rotary switch and it works as I want it to, however when I added it into the sketch for the slider control, the stepper motor freezes.
this is the sketch for the working blink without delay
/* Blink without Delay
Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.
The circuit:
- LED attached from pin 13 to ground.
- Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
This example code is in the public domain.
*/
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long x;
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 930;// interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin (9600);
}
void loop()
{
// read the sensor:
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 1, 10);
// do something different depending on the
// range value:
switch (range) {
case 1: // your hand is on the sensor
Serial.println("200");
x = 200;
break;
case 2: // your hand is close to the sensor
Serial.println("600");
x = 600;
break;
case 3: // your hand is a few inches from the sensor
Serial.println("1000");
x = 1000;
break;
case 4: // your hand is nowhere near the sensor
Serial.println("1600");
x = 1600;
break;
case 5: // your hand is on the sensor
Serial.println("2000");
x = 2000;
break;
case 6: // your hand is close to the sensor
Serial.println("1700");
x = 1700;
break;
case 7: // your hand is a few inches from the sensor
Serial.println("1300");
x = 1300;
break;
case 8: // your hand is nowhere near the sensor
Serial.println("900");
x = 900;
break;
case 9: // your hand is on the sensor
Serial.println("500");
x = 500;
break;
case 10: // your hand is a few inches from the sensor
Serial.println("100");
x = 100;
break;
}
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > x) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
This is the sketch for the slider control
//stepper
#define stepPin 6
#define dirPin 7
// maunal control
#define button1 2
#define button2 3
// automatic control
#define dirChangeL 8
#define dirChangeR 9
//function change
#define functionSwitch 10
// Speed control
const int sensorMin = 0; // refering to the selector switch
const int sensorMax = 930;
int stepState = LOW;
long x;
long previousMillis = 0;
void setup() {
Serial.begin(9600);
//stepper
pinMode(stepPin , OUTPUT);
pinMode(dirPin , OUTPUT);
digitalWrite(stepPin , LOW);
digitalWrite(dirPin , LOW);
//manual contol
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
//automatic control
pinMode(dirChangeL, INPUT_PULLUP);
pinMode(dirChangeR, INPUT_PULLUP);
//function switch
pinMode(functionSwitch, INPUT_PULLUP);
}
void loop() {
if (digitalRead(functionSwitch)==HIGH) {
automatic();
}
else if (digitalRead(functionSwitch)==LOW){
manual();
}
}
// operation code
//automatic
void automatic(){
digitalWrite(dirPin, HIGH);
while (digitalRead(dirChangeL) == HIGH)
{
motion();
if (digitalRead(functionSwitch) ==LOW){
break;
manual();
}
}
digitalWrite(dirPin, LOW);
while (digitalRead(dirChangeR) == HIGH)
{
motion();
if (digitalRead(functionSwitch) ==LOW){
break;
manual();
}
}
}
//manual
void manual(){
if (digitalRead(button1) == LOW && digitalRead(button2) == HIGH) {
digitalWrite(dirPin, LOW);
}
else if (digitalRead(button1) == HIGH && digitalRead(button2) == LOW) {
digitalWrite(dirPin, HIGH);
}
if (digitalRead(button1) == LOW || digitalRead(button2) == LOW) {
digitalWrite(stepPin , HIGH);
motion();
}
}
void motion() {
//read and map the rotary select switch
int sensorReading = analogRead(A0);
int range = map(sensorReading, sensorMin, sensorMax, 1, 10); // this is based on the readings form the selector switch
//setting up the switch options
//due to the way that the rotary switch was wired with the resistors in series
//to get the different values. The differnt case options will follow the pin positions and the various readings
//from these postions
//differnt speed settings
switch (range) {
case 1:
Serial.println("600 milliseconds");
x = 600;
break;
case 2:
Serial.println("2 seconds");
x = 2000;
break;
case 3:
Serial.println("10 seconds");
x = 10000;
break;
case 4:
Serial.println("1 Minute");
x = 60000;
break;
case 5:
Serial.println("10 Minutes");
x = 600000;
break;
case 6:
Serial.println("5 Minutes");
x = 300000;
break;
case 7:
Serial.println("30 seconds");
x = 30000;
break;
case 8:
Serial.println("5 Seconds");
x = 5000;
break;
case 9:
Serial.println("1 second");
x = 1000;
break;
case 10:
Serial.println("200 milliseconds");
x = 200;
break;
}
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > x) {
previousMillis = currentMillis;
if (stepState == LOW)
stepState = HIGH;
else
stepState = LOW;
digitalWrite(stepPin, stepState);
}
}