Pages: [1]   Go Down
Author Topic: Delay command for a single line?  (Read 224 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
  Serial.println(",");   
if (sensor1Value == 0,sensor2Value == 0)
{
 E510.shutterNow(); // trigger the shutter
}
{
  motor.step(1, FORWARD, INTERLEAVE); //CCW
>>>>>  I NEED A DELAY HERE BUT ONLY FOR THE STEPPER<<<<<
  }
}
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 575
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What you want is http://arduino.cc/en/Tutorial/BlinkWithoutDelay.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank You Very Much!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please forgive my ignorance about Arduino code and the millis function at tutorial
http://arduino.cc/en/Tutorial/BlinkWithoutDelay.
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"?
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 575
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 
 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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Boston
Offline Offline
God Member
*****
Karma: 3
Posts: 575
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();
Logged

Pages: [1]   Go Up
Jump to: