Hi all,
I'm trying to build a setup for some lab test; I'm very new to arduino.
The Idea is to have 3 IR pairs run and log passes, and at the same time have 3 magnets cylcing trough a setup every 8 hours. (for testing purpose its coded 20 sec cycles.
I hooked up the leds, relay and a DS1307 for logging. All went fine untill I added the relay code.
The relay does not seem to work when being instructed by an IF statement. Its a 5v relay powered by arduino. It works fine if I code it with delays, but this will mess up my sensor readings. What I want is the relay to act on a certain time given by the DS1307.
I can see the leds blinking but I cant hear the clicks. Sometimes a click comes up but it won't do anything. Also sometimes the leds blink.
Any help of were to look will be appreciated!
#include <DS1307RTC.h>
#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
int pd=2;
int pd2=3;
int pd3=4;
int RELAY1 = 5;
int RELAY2 = 6;
int RELAY3 = 7;
int timePower=8;
int relayPower= 13;
int senRead=A0;
int senRead2=A1;
int senRead3=A2;
int limit=600;
int Left=1;
int Center=1;
int Right=1;
void setup()
{
pinMode(pd,OUTPUT);
pinMode(pd2,OUTPUT);
pinMode(pd3,OUTPUT);
pinMode(relayPower,OUTPUT);
pinMode(timePower,OUTPUT);
digitalWrite(timePower,HIGH);
digitalWrite(pd,HIGH);
digitalWrite(pd2,HIGH);
digitalWrite(pd3,HIGH);
digitalWrite(relayPower,HIGH);
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2,OUTPUT);
pinMode(RELAY3,OUTPUT);
Serial.begin(9600);
while (!Serial);
}
void loop()
{
tmElements_t tm;
if (RTC.read(tm)){
if (tm.Second >= 0 && tm.Second <20) {
digitalWrite (RELAY1,HIGH);
digitalWrite (RELAY2,LOW);
digitalWrite (RELAY3,LOW);
}
else if (tm.Second >=20 && tm.Second < 40) {
digitalWrite (RELAY1,LOW);
digitalWrite (RELAY2,HIGH);
digitalWrite (RELAY3,LOW);
}
else if (tm.Second >= 40 && tm.Second <59) {
digitalWrite (RELAY1,LOW);
digitalWrite (RELAY2,LOW);
digitalWrite (RELAY3,HIGH); }
{
int val=analogRead(senRead);
int val2=analogRead(senRead2);
int val3=analogRead(senRead3);
if (val > limit) {
Serial.print("Left: ");
Serial.print(Left++);
Serial.print(" passes");
Serial.print("\t Center: ");
Serial.print(Center);
Serial.print(" passes");
Serial.print("\t Right: ");
Serial.print(Right);
Serial.print(" passes \t");
Serial.print(tm.Hour);
Serial.print(":");
Serial.print(tm.Minute);
Serial.print(":");
Serial.println(tm.Second);
delay(25);
}
if (val2 > limit) {
Serial.print("Left: ");
Serial.print(Left);
Serial.print(" Passes");
Serial.print("\t Center: ");
Serial.print(Center++);
Serial.print(" passes");
Serial.print("\t Right: ");
Serial.print(Right);
Serial.print(" passes\t");
Serial.print(tm.Hour);
Serial.print(":");
Serial.print(tm.Minute);
Serial.print(":");
Serial.println(tm.Second);
delay(25);
}
if (val3 > limit) {
Serial.print("Left: ");
Serial.print(Left);
Serial.print(" passes");
Serial.print("\t Center: ");
Serial.print(Center);
Serial.print(" passes");
Serial.print("\t Right: ");
Serial.print(Right++);
Serial.println(" passes\t");
Serial.print(tm.Hour);
Serial.print(":");
Serial.print(tm.Minute);
Serial.print(":");
Serial.println(tm.Second);
delay(25);
}}
}
else {
if (RTC.chipPresent()) {
Serial.println("Klok geeft geen tijd");
Serial.println("er lijk nog wel leven in te zitten");
Serial.println();
} else {
Serial.println("DS1307 error, check circuit.");
Serial.println();
}
}}