Go Down

Topic: Setting up timer to perform a command after x time (Read 618 times) previous topic - next topic

Dean1305

Hi,

Im looking to incorporate a timer into a project i made which is a solar tracker using 4 LDR's and two servo motors(pitch and rotation of sensor). The program works fully and follows light but i need to add in a timer to mimic an over cast day. Basically i need a timer set up that when all of the 4 LDR's analogue read values reach less than 400, a timer will start and tick up to 15mins. While the timer is on going the program is to still function as normal allowing the sensor to keep looking for light through servo movement. if all LDR's are still less than 400 at 15mins both servos are to go to a 90deg position and remain there. If any one of the LDR's go greater than 400, the timer is to reset and the servos and sensor continue to search for light as normal.

Below is my coding in full which is split into functions (without my attempt at a timer). Iv included every function in case of compiling issues. I think i just need to add this timer to the main page. If anyone can help with this it would get me out of a huge hole!

Thanks,
Dean.

Code: [Select]


//This section is main page

#include <Servo.h>             //Servo library
#include "A_RotationServo1.h" // Function library includes
#include "B_PitchServo2.h"
#include "C_AnalogPrint.h"
#include "D_Ledboard.h"
#include "E_PinMode.h"

Servo S1;          //servo 1
Servo S2;          //servo 2
//int   servo1 =90;  //Servo1 initial position of 90 deg
//int   servo2 =90;  //Servo2 initial position of 90 deg

int ldr1 = A0;     //LDR 1
int ldr2 = A1;     //LDR 2
int ldr3 = A2;     //LDR 3
int ldr4 = A3;     //LDR 4
int lt;
int lr;
int ll;
int rr;

int SP1=0; //servo 1 position
int SP2=0; //servo2 position

//These pins are for a digital signal being sent to idicate a certain light intensity to an LED stack for each LDR.
int ledPinA5 = A5; //Using analoge pins as digital
int ledPinA4 = A4; //Using analoge pins as digital
int ledPin2  = 2;
int ledPin3  = 3;
int ledPin4  = 4;
int ledPin5  = 5;
int ledPin6  = 6;
int ledPin7  = 7;
int ledPin8  = 8;
int ledPin9  = 9;
int ledPin10 = 10;
int ledPin11 = 11;

void setup()
{
  PinModefunction(ledPinA5,ledPinA4,ledPin2,ledPin3,ledPin4,ledPin5,ledPin6,ledPin7,ledPin8,ledPin9,ledPin10,ledPin11);//Function for Pinmode
 
  Serial.begin(9600); //standard baud
  S1.attach(12);      //Attaching pin 12 to servo 1
  S2.attach(13);      //Attaching pin 13 to servo 2
  S1.write(0);
  S2.write(90);
}

void loop()
{
 SP1= S1.read();       //Reads servo1 position for variable SP1
 SP2= S2.read();       //Reads servo2 position for variable SP2
 
 analogRead(ldr1);     //Reads A0 analogue ouput
 lt= analogRead(ldr1); //Reads A0 analogue ouput a second time to get a chance to switch to correct pin
 analogRead(ldr2);     //Reads A1 analogue output
 lr=analogRead(ldr2);  //Reads A1 analogue ouput a second time to get a chance to switch to correct pin

 analogRead(ldr3);     //Reads A2 analogue ouput
 ll= analogRead(ldr3); //Reads A2 analogue ouput a second time to get a chance to switch to correct pin
 analogRead(ldr4);     //Reads A3 analogue output
 rr=analogRead(ldr4);  //Reads A3 analogue ouput a second time to get a chance to switch to correct pin

 //analogRead(SP1); //Reads servo 1 position
 //analogRead(SP2); //Reads servo 2 position

//Functions
Printfunction(lt,lr,ll,rr,SP1,SP2); // Print function for printing values
Servofunction1(S1,lt,lr,ll,rr,SP1); // Servo1 function
Servofunction2(S2,lt,lr,ll,rr,SP2); // Servo2 function
Ledfunction(ledPinA5,ledPinA4,ledPin2,ledPin3,ledPin4,ledPin5,ledPin6,ledPin7,ledPin8,ledPin9,ledPin10,ledPin11,lr,ll,lt,rr); //LED LIB function

}

//This section if for servo function1

void Servofunction1(Servo Sm1,int lt1,int lr1,int ll1,int rr1,int SPm1) // Function layout

if (lt1+lr1+ll1 < ll1+rr1+lt1+50)     // if statement comparitor of LDR sums
{
Sm1.write(SPm1-1);                    // Move servo1 -
delay(10);                            // Delay of 10ms
}
else if (lt1+lr1+ll1+50>ll1+rr1+lt1)  //If lr is less than lt then decrement servo using --servo
{
Sm1.write(SPm1+1);                    // Move servo1 +
delay(10);                            // Delay of 10ms
}
if(lr1>800 && ll1>800 && lt1>800 && rr1>800) // If all LDR's are greater than 800
{
  Sm1.detach();                       // detaches servo 1
  delay(100);                         // Delay of 100ms
}
else if (lt1+lr1+ll1 < ll1+rr1+lt1+50 || lt1+lr1+ll1+50>ll1+rr1+lt1) //If change in LDR sums
{
  Sm1.attach(12);                     //re attaches servo 1
  delay(100);                         // Delay of 100ms
}
}

//This section is for servo function 2

void Servofunction2(Servo Sm2,int lt2,int lr2,int ll2,int rr2,int SPm2) // Function layout
{
if (lr2+ll2+50 > lt2+rr2)      // if statement comparitor of LDR sums
{
  Sm2.write(SPm2+2);           //Servo motor increment by 2 increments
  delay(10);                   // Delay of 10ms
}
else if (lr2+ll2 < lt2+rr2+50) // if statement comparitor of LDR sums
{
  Sm2.write(SPm2-2);           //Servo motor increment by 2 increments
  delay(10);                   // Delay of 10ms
}

if(lr2>800 && ll2>800 && lt2>800 && rr2>800) //If all LDR's are greater than 800
{
  Sm2.detach();                //detactches servo 2
  delay(100);                  // Delay of 100ms
}

else if (lr2+ll2<lt2+rr2+50 || lr2+ll2+50 > lt2+rr2) //If change in LDR sums
{
  Sm2.attach(13);              // re ataches servo 2
  delay(100);                  // Delay of 100ms
}
}

//This section is for print function

 void Printfunction(int lt1,int lr1,int ll1,int rr1,int servo11,int servo22) // Function layout
 {
 Serial.print("LDR0 lt: ");Serial.print(lt1);                      //Prints the value of A0 to serial monitor
 Serial.print(" LDR1 lr: ");Serial.println(lr1);                   //Prints the value of A1 to serial monitor
 Serial.print("LDR2 ll: ");Serial.print(ll1);                      //Prints the value of A2 to serial monitor
 Serial.print(" LDR3 rr: ");Serial.println(rr1);                   //Prints the value of A3 to serial monitor
 //delay(1000);
 Serial.print("       Servo1 deg:");Serial.println(servo11)/180; //Prints servo1 position. Divided position by 180 to display true value
 Serial.print("       Servo2 deg:");Serial.println(servo22)/180; //Prints servo2 position. Divided position by 180 to display true value
 }

//This section is for Led function

void Ledfunction(int A5,int A4,int pin2,int pin3,int pin4,int pin5,int pin6,int pin7,int pin8,int pin9,int pin10,int pin11,int lr1,int ll1,int lt1,int rr1) // Function layout

if (lt1<400)
{
  digitalWrite(A4,LOW);
  digitalWrite(A5,HIGH);
  digitalWrite(pin2,HIGH);
}
if (lt1>600)
{
  digitalWrite(A4,HIGH);
  digitalWrite(A5,LOW);
  digitalWrite(pin2,HIGH);
}
if (lt1>800)
{
  digitalWrite(A4,HIGH);
  digitalWrite(A5,HIGH);
  digitalWrite(pin2,LOW);
}

//This section is for the lt LDR.
if (lr1<400)
{
  digitalWrite(pin3,LOW);
  digitalWrite(pin4,HIGH);
  digitalWrite(pin5,HIGH);
}
if (lr1>600)
{
  digitalWrite(pin3,HIGH);
  digitalWrite(pin4,LOW);
  digitalWrite(pin5,HIGH);
}
if (lr1>800)
{
  digitalWrite(pin3,HIGH);
  digitalWrite(pin4,HIGH);
  digitalWrite(pin5,LOW);
}

// This section is for the ll LDR.
if (ll1<400)
{
  digitalWrite(pin6,LOW);
  digitalWrite(pin7,HIGH);
  digitalWrite(pin8,HIGH);
}
if (ll1>600)
{
  digitalWrite(pin6,HIGH);
  digitalWrite(pin7,LOW);
  digitalWrite(pin8,HIGH);
}
if (ll1>800)
{
  digitalWrite(pin6,HIGH);
  digitalWrite(pin7,HIGH);
  digitalWrite(pin8,LOW);
}

//This section is for the rr LDR.
if (rr1<400)
{
  digitalWrite(pin9,LOW);
  digitalWrite(pin10,HIGH);
  digitalWrite(pin11,HIGH);
}
if (rr1>600)
{
  digitalWrite(pin9,HIGH);
  digitalWrite(pin10,LOW);
  digitalWrite(pin11,HIGH);
}
if (rr1>800)
{
  digitalWrite(pin9,HIGH);
  digitalWrite(pin10,HIGH);
  digitalWrite(pin11,LOW);
}
}

//This section is for pin mode function

void PinModefunction(int ledPinA5p,int ledPinA4p,int ledPin2p,int ledPin3p,int ledPin4p,int ledPin5p,int ledPin6p,int ledPin7p,int ledPin8p,int ledPin9p,int ledPin10p,int ledPin11p)// Function layout
 {
  pinMode(ledPinA5p, OUTPUT); //Analogue port used as digital
  pinMode(ledPinA4p, OUTPUT); //Analogue port used as digital
  pinMode(ledPin2p, OUTPUT);
  pinMode(ledPin3p, OUTPUT);
  pinMode(ledPin4p, OUTPUT);
  pinMode(ledPin5p, OUTPUT);
  pinMode(ledPin6p, OUTPUT);
  pinMode(ledPin7p, OUTPUT);
  pinMode(ledPin8p, OUTPUT);
  pinMode(ledPin9p, OUTPUT);
  pinMode(ledPin10p, OUTPUT);
  pinMode(ledPin11p, OUTPUT);
 }

HermannSW

https://forum.arduino.cc/index.php?topic=462107.msg3236016#msg3236016
http://stamm-wilbrandt.de/en/Raspberry_camera.html

Go Up