Consistent output with arduino

Hi guys!

I have a little project that i could use some help with. I am quite new to the arduino, and not that steady in the code, but im hoping on getting there!

The project im building is a litte controller that drives a relay output. The controller is connected to a photo-cell that has a two pin output. This output is shorted at various times depending on the movement in front of the photocell.
What i need is for the arduino to close the relay for the same period (say 0,5s) each time the photocell circut is closed. And maybe block a repeating signal from the photocell within 0,5s

Is this possible in some way?

Any help much appreciated!

gmoon4 years ago
If it's really that simple--
1) read the photo cell  2) turn on relay for 0.5 seconds 3) turn off relay 4) wait 0.5 seconds before checking photocell again

--then it's easy. Just use the delay(); function, which takes milliseconds as the argument. Something like this (it's not real code--you'll have to create functions to read the cell and turn the relay on and off):

If (readPCell())
{
relayOn();
delay(500);
relayOff();
delay(500); }

(code placed somewhere inside your loop() function)

If other tasks need to be done during the wait states, it becomes more complicated... There are quite a few ways to do it, of course.
maceide (author)  gmoon4 years ago
Ok, here is what I have come up with so far;

const int cellPin = 12; //
const int outPin = 2; //
const int ledPin = 13;

int passeringState = 0;
unsigned long Timer;

void setup()
{
pinMode(cellPin, INPUT); //
pinMode(outPin, OUTPUT); //
pinMode(ledPin, OUTPUT);
}

void loop()
{
passeringState = digitalRead(cellPin);

if (passeringState == HIGH) {
Timer = millis();
sendSignal();
}
if (millis()-Timer >= 500UL) {
digitalWrite(outPin, LOW);
digitalWrite(ledPin, LOW);
}
}

void sendSignal() {
digitalWrite(outPin, HIGH);
digitalWrite(ledPin, HIGH);

}


This works OK, but i want it to send only 0,5sec pulse even if the input signal lasts more than 0,5sec, say 2sec fx. Is that somehow poosible?
gmoon maceide4 years ago
OK, looks good. You've implemented it without delay(), which would have blocked most of the cycles in loop(). Using delay() is a simpler solution, but the arduino would be stuck in neutral during the delay.

Try the changes below. An additional flag has been added (waitState) that keeps the output pins LOW (after the initial 0.5 sec. of HIGH) until 2 seconds pass. I also rewrote the sendSignal() function to accept an single argument.

const int cellPin = 12; //
const int outPin = 2; //
const int ledPin = 13;

int passeringState = 0;
unsigned long Timer = 0;
boolean waitState = FALSE;

void setup()
{
pinMode(cellPin, INPUT); //
pinMode(outPin, OUTPUT); //
pinMode(ledPin, OUTPUT);

sendSignal(LOW);
}

void loop()
{
passeringState = digitalRead(cellPin);

if (passeringState && waitState == FALSE) {
Timer = millis();
waitState = TRUE;
sendSignal(HIGH);
}

if (millis()-Timer >= 500UL) {
sendSignal(LOW);
}

// wait additional 1.5 seconds
if (millis()-Timer >= 2000UL) {
waitState = FALSE;
}
}

void sendSignal(boolean outpins) {
digitalWrite(outPin, outpins);
digitalWrite(ledPin, outpins);
}

--------------------------------------------------

This should work--if not, you get the general idea (I only took a few coding classes, but NOTHING the instructors put on the board ever worked as-is :-).

If you're running this continously, it could glitch after 50 days or so-- the internal timer (UL) rolls over, and consequently the value returned by millis() does also...
caitlinsdad4 years ago
Look up the tutorials on debouncing a switch. arduino,cc etc. The photocell is your switch.