Pages: [1]   Go Down
Author Topic: Call a method during a delay using an if statement  (Read 858 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys,

I want to create a IF statement to run an event if the action is true, for example I have a motor and it spins for a second then has a delay of 60 secs, during that time I want a servo to move but not wait till the 60 secs are over as with a delay, I know you need to use an IF statement, please see attached code for more details.

Cheers
Code:
#include <Servo.h>
int motorPin = 9;  // define the pin the motor is connected to
                    // (if you use pin 9,10,11 or 3you can also control speed)
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
int onTime = 1000;  //the number of milliseconds for the motor to turn on for
int offTime = 30000;
int pos = 0;
 
/*
  * setup() - this function runs once when you turn your Arduino on
  * We set the motors pin to be an output (turning the pin high (+5v) or low (ground) (-))
  * rather than an input (checking whether a pin is high or low)
  */
 void setup()
 {
  pinMode(motorPin, OUTPUT);
  myservo.attach(6);  // attaches the servo on pin 9 to the servo object
  myservo.write(0);
 }
 

/*
  * loop() - this function will start after setup finishes and then repeat
  * we call a function called motorOnThenOff()
  */
 
void loop()                     // run over and over again
 {
  motorOnThenOff();
  //motorOnThenOffWithSpeed();
  //motorAcceleration();
 }
 
/*
  * motorOnThenOff() - turns motor on then off
  * (notice this code is identical to the code we used for
  * the blinking LED)
  */
 void motorOnThenOff(){
   //int offTime2 = 50000; //the number of milliseconds for the motor to turn off for
   
   digitalWrite(motorPin, HIGH); // turns the motor On
   delay(onTime); // waits for onTime milliseconds
   digitalWrite(motorPin, LOW);  // turns the motor Off
   delay(1000);
   if (motorPin == 30000)
   {runServo();
    delay(offTime-30000);
  }
else{delay(offTime);}
   //delay(offTime2);    // waits for offTime milliseconds
 }
void runServo()
{
  for(pos = 0; pos < 90; pos += 90)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1);                       // waits 15ms for the servo to reach the position
  }
 
  for(pos = 90; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(1);                       // waits 15ms for the servo to reach the position
  }
  delay(30000);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As always, have a look at the blink without delay example for clues of how to avoid using "delay"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Sr. Member
****
Karma: 0
Posts: 351
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As the delay tutorial explain, u cant really do anything else while the delay is active !
Logged

10 LET Loop=Infinite
20 GO TO 10

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are going to have to create a variable (or 2) and use millis() to assign a value and then write your logic that that starts and stops the motor using those values to determine when it is the proper time.  Look at Blink without delay for how to do it.
Logged

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

Hi Guys,

Looking at the blink without delay, its not making sense can some explain to me please  smiley-mr-green
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 351
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

there are already loads of posts explaining it
Logged

10 LET Loop=Infinite
20 GO TO 10

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26350
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The blink without delay example effectively consists of about four lines.
Tell us what you don't understand
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

An update,

Solved this problem by adding the servo method to the loop and reducing the time it takes on the delay, seems to be working fine now, thanks for the input.
Logged

Pages: [1]   Go Up
Jump to: