Go Down

Topic: Delay command for a single line? (Read 601 times) previous topic - next topic


Help a nebie out?
How can I have one line of a sketch have a delay of say 30 seconds but not the entire sketch?
Im using IR to fire my camera and a stepper to rotate it also.They work great alone but not together. I want to be able to shoot more than one frame between rotation steps.

Heres my code:

#include <multiCameraIrControl.h>
#include <AFMotor.h>
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                                     // for your motor
// Connect a stepper motor with 200 steps per revolution (? degree)
// to motor port #2 (M3 and M4)
AF_Stepper motor(200, 2);
const int sensor1Pin = A0;      // Yellow
const int sensor2Pin = A1;      // Red
                                //  White=Ground
Olympus E510(9); //  (camera model E510) Pin 9 Hot IR LED Red Wire
const int ledPin = 13;        // pin that the LED is attached to
int sensor1Value = 0;         // the sensor value
int sensor1Min = 1023;        // minimum sensor value
int sensor1Max = 0;           // maximum sensor value
int sensor2Value = 0;         // the sensor value
int sensor2Min = 1023;        // minimum sensor value
int sensor2Max = 0;           // maximum sensor value

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);   // turn on LED to signal the start of the calibration period:
Serial.begin(9600);         // set up Serial library at 9600 bps
  while (millis() < 30000)  // calibrate sensor during the first 30 seconds
    sensor1Value = analogRead(sensor1Pin);
    sensor2Value = analogRead(sensor2Pin);
    if (sensor1Value > sensor1Max) // record the maximum sensor value
      sensor1Max = sensor1Value;
if (sensor2Value > sensor2Max) {
      sensor2Max = sensor2Value;
    if (sensor1Value < sensor1Min)  // record the minimum sensor value
      sensor1Min = sensor1Value;
if (sensor2Value < sensor2Min) {
      sensor2Min = sensor2Value;
  digitalWrite(13, LOW);// signal the end of the calibration period

void loop() {

  sensor1Value = analogRead(sensor1Pin); // read the sensor:
sensor2Value = analogRead(sensor2Pin);

  sensor1Value = map(sensor1Value, sensor1Min, sensor1Max, 0, 255); // apply the calibration to the sensor reading
sensor2Value = map(sensor2Value, sensor2Min, sensor2Max, 0, 255);
  sensor1Value = constrain(sensor1Value, 0, 255);// in case the sensor value is outside the range seen during calibration
sensor2Value = constrain(sensor2Value, 0, 255);

  Serial.print("sensor1:");  // lables input
  Serial.print(sensor1Value);   // prints the sensors values to the serial
  Serial.print(",");    // makes 2 columns
Serial.print("sensor2:");  // lables input
  Serial.print(sensor2Value);  // prints the sensors values to the serial
if (sensor1Value == 0,sensor2Value == 0)
E510.shutterNow(); // trigger the shutter
  motor.step(1, FORWARD, INTERLEAVE); //CCW



Please forgive my ignorance about Arduino code and the millis function at tutorial
I got the led to blink no problem. My issue is how do I apply this to my stepper?
I already have a "ledPin=13" in my script. Do I change that function to another pin and then change "ledPin=13" to "AF_Stepper motor"?


Try something like this (untested).
At the top of your code just above void setup() add the following 2 variables.
long previousMillis = 0;       
long interval = 1000;
At the beginning of void loop(), put unsigned long currentMillis = millis();

At the end of void loop(), put
if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;   
motor.step(1, FORWARD, INTERLEAVE); //CCW

Change the value of interval to what you need.


OK I think Im understanding now.
However I already have a millis function in this sketch.
So now my question is can I have more than one (ie one for the calibrate sensor function) and another for the stepper?
If so how do I differentiate the two?


Yes, you can have more than 1.
Just creat more variables.

long previousMillis = 0;       
long interval = 1000;
long previous Millis2 =0;
long interval2 = 2000;
long currentMillis = millis();
long currentMillis2 = millis();

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131