Arduino Uno help please

OK hello everyone I know this will be really simple for some but I am working on a project of my own and want to add a timer in after the first relay is turned on say pin 4. If that relay doesn’t stay on for say 30 s then it should do nothing. Can anyone help me with this here? I will post my code below.

int sensorPin = A0; // Photocell
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
pinMode(2, OUTPUT); //pin connected to the relay
Serial.begin(9600); //sets serial port for communication
}

void loop() {
// Check value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue); //prints the values coming from the sensor on the monitor

if(sensorValue < 500) //setting a threshold value of light
digitalWrite(2,LOW); //turns relay ON
else digitalWrite(2,HIGH); //turn relay OFF

delay(100);
}

Should I just put in something like
else if (sensorValue < 500 + delay(30000);
digitalWrite(4,LOW); //turns 2nd relay ON

ijoshjoycei:
add a timer in after the first relay is turned on say pin 4. If that relay doesn't stay on for say 30 s then it should do nothing.

Is this intended to be for the relay on pin 2 or are you planning to add a second relay on pin 4?

I don't understand the piece in bold. What would make the relay turn off in less than 30 seconds, and how would the Arduino know that it has turned off?

Maybe have a look at how millis() is used to manage timing in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

ok, I have a relay on pin 2 and want to put another relay on pin 4. I only want pin 4's relay to turn on if pin 2 is active for 30 seconds. If pin 2 is not active for 30 seconds then I want pin 4 to do nothing.

possible pseudocode

  if pin2 not active{
  load pin4millis with millis() // holds timer in reset
  }
  if millis() - pin4millis > 30secMillis{
    pin4On
  }
  else {
    pin4Off
  }

ijoshjoycei:
ok, I have a relay on pin 2 and want to put another relay on pin 4. I only want pin 4’s relay to turn on if pin 2 is active for 30 seconds. If pin 2 is not active for 30 seconds then I want pin 4 to do nothing.

Note how much clearer that description is compared with your Original Post. A detailed (and accurate) statement of the requirement makes it much easier to produce the appropriate code - as in Reply #4

I suspect if you had started with that description you would probably have figured out a solution yourself. Note that this is not intended as a a criticism, just an attempt to make you (and other readers of this Thread) aware of the importance of clearly stating the requirement.

…R

I like this problem! It’s a very simple demonstration of how to do state dependent execution in an Arduino program. I’m gonna co-opt it for my course. Below is similar to the code I will use. Thanks!

(Note, uses digital input instead of analog. input: pin 2, relay 1: pin 12 led, relay2: pin 13 led.)

unsigned long startWaitMillis;
const long waitMillis = 10000L;  // ten seconds

byte state;

const byte inputPin = 2;
const byte relay2LedPin = 12;
const byte relay1LedPin = 13;

void setup() {
  pinMode(inputPin, INPUT_PULLUP);
  pinMode(relay2LedPin, OUTPUT);
  digitalWrite(relay2LedPin, LOW);
  pinMode(relay1LedPin, OUTPUT);
  digitalWrite(relay1LedPin, LOW);

  state = 0;
}

void loop() {

  switch (state) {

    case 0:  // idle waiting for button press
      while (digitalRead(inputPin)) {
        delay(100); // you can sleep here
      }
      state = 1;
      digitalWrite(relay1LedPin, HIGH);  // turn on relay #1
      startWaitMillis = millis();
      break;

    case 1:  // wait 10 seconds and check input again
      if (millis() - startWaitMillis >= waitMillis) {
        if (!digitalRead(inputPin)) {
          digitalWrite(relay2LedPin, HIGH);  // turn on relay #2
          state = 2;
        } else {
          digitalWrite(relay1LedPin, LOW);  // turn off relay #1
          state = 0;
        }
      }
      break;

    case 2:  // all done
      while (true) {  // stay this way forever
        delay(100); // you can sleep here
      }
      break;
  }
}

ChrisTenone:
I'm gonna co-opt it for my course.

For teaching may I suggest you use an ENUM to enumerate the states?

Or at least give the states names rather than magic numbers like 0 and 1

...R

Robin2:
For teaching may I suggest you use an ENUM to enumerate the states?

Or at least give the states names rather than magic numbers like 0 and 1

...R

Thanks Robin.

My 'class' is a 6 hour module in the middle of an Intro to Engineering (102) course taught by someone else. I don't give any final code. Some of my students have never heard of Arduino, while others may have taken C++, analog circuits, or digital circuits courses, and have an Arduino at home.

Typically I run demo code in the first 10-20 minutes, and the rest of the time (3 hours X 2 days) is theirs to work on their project. I "facilitate discovery" (meaning tell those who can benefit to go to the internet - ie 'forums.arduno.cc', fix bad hardware problems, answer any questions, and spoon feed the clueless.) Everybody gets a circuit to continue with by the end.

I'll keep surely keep this suggestion in mind. Students'll get credit for using names that contribute to the readability of their code.

Robin2:
Note how much clearer that description is compared with your Original Post. A detailed (and accurate) statement of the requirement makes it much easier to produce the appropriate code - as in Reply #4

I suspect if you had started with that description you would probably have figured out a solution yourself. Note that this is not intended as a a criticism, just an attempt to make you (and other readers of this Thread) aware of the importance of clearly stating the requirement.

...R

Yes, this was just to be rude or smug. There isn't another way to read what you have written so please keep comments like this to yourself as it was not asked for this type of feedback.

ijoshjoycei:
Yes, this was just to be rude or smug. There isn't another way to read what you have written so please keep comments like this to yourself as it was not asked for this type of feedback.

No it was not. And I specifically said that in the text you quoted.

...R