2

I have system connected to an Arduino Uno where I want to read whether the switch is open or closed.

Based on if the switch is open/closed, I want an LED light to turn on/off. When the switch is open, the LED turns on and pushing the button turns it off and stays off for 24 hours then turns back on if the switch is still open and I can again turn off the light with one push of the button. However, if the switch is closed within the 24 hours, the light doesn't turn on and pushing the button does not do anything.

I don't know how to change the state of the LED back on after the 24 hours. Should I use a button counter, if statements, or while statements? I'm really new to coding and took on a large project I'm not sure how to complete. This is obviously a very small portion of the project. Any help is much appreciated.

This is as far as I've gotten. When the switch is open the LED turns on, but when I push the button, the light just dims. I also have to hold the button down to keep the light "off" and it turns right back on when I release the button.

if (digitalRead(switch) == LOW) {
digitalWrite(Light, HIGH );


if (digitalRead(Button) == LOW) {
    digitalWrite(Light, LOW);
}      

else if (digitalRead(switch) == HIGH) {
  digitalWrite(Light, LOW );

}
}
  • Please edit your question (click "edit") and make clear whether you are describing what you want to happen or what does happen. For example, you say “However, if the switch is closed within the 24 hours, the light doesn't turn on and pushing the button does not do anything”; is that what your code does now, and it is erroneous or undesired behavior? Try to write a clear specification of what should happen, couched in sentences that begin with phrases like “The system shall ...” – James Waldby - jwpat7 May 31 '17 at 1:38
  • Please edit your question to include a Minimal, Complete, and Verifiable example of code, not just snippets. Minimal means you've stripped away irrelevant stuff, just leaving what's needed to show the problem. Complete means all the library names are shown, all the variable declarations, and all the function definitions – so people don't have to waste time guessing what you did or what you meant. Verifiable means it can be compiled and tested, allowing other people to test their theories about the problem. – James Waldby - jwpat7 May 31 '17 at 1:40
  • Do not name a constant switch. – Edgar Bonet Jun 1 '17 at 9:12
  • You need to go back and learn to program. You are trying the climb the mountain by the hardest route possible. Start with small hills. Learn the basics, develop your skills. Enjoy each little step. – user31481 Oct 29 '17 at 21:08
1

I have to make an answer, since I cannot use items/links easily in a comment.

Instead of creating the application for you, I suggest you do the following:

  • Read/try and experiment with the default Arduino examples for a switch (see Switch
  • In the above example also is shown how to debounce the signal, this is needed since you want to use it as a 'toggle'.
  • Use a timer for millis() and check for 24 hrs (which is 1,440,000) millis, so it should fit in a long.
  • Small thing: Always align brackets to make it easier for yourself and others.
0

A few things will need to be done.

  1. You have to figure out to detect a button press. This needs to be transition based. Ie. Detect a button press when it was not pressed the previous time.

Once a button is pressed, you take a snap shot of the time. And then turn on the led.

  1. Then you test current time at each pass and see if the desired time has passed.

This can be done by configuring one of the timers to record sufficiently long period.

If you have to use mill is(), you have to figure out how long it overflows and how to detect its overflow.

Both can be done, fairly easily.

0

Your problem is, that you do not keep the status and acts only on actual state of switch+button each pass of loop() (that is many thosand each second).

So you Light On is switch is turned, then Light off if button is pressed and next split of second you repaet it again - led looks dim, as your eye cannot see how fast is blinking.

You need to keep the status (OFF,ON,TEMP_OFF) and act upon that. Also if it is temporary off, you need know, when it happend, to undo it after 24 hours. (google for state automat arduino)

the code would be like this (not tested, I wrote it from head, so try to understand the principle and fix all small errors and play with the idea on your own)

// statuses - just some different values
#define ST_OFF 1
#define ST_ON 2
#define ST_TEMP_OFF 3

// pin assignment - change by tour projects need
#define pin_switch 2
#define pin_button 3
#define pin_led 13


unsigned long temp_time; // temporarry off from this time
byte status=ST_OFF; // start with dark

loop() {
  switch (status) {

    case ST_OFF: 
      if ( digitalRead(pin_switch) == LOW ) {
          status = ST_ON;
          // light it on if switch turns/is LOW
          };
       break;

    case ST_ON:
      if ( digitalRead(pin_button) == LOW ) {
         status = ST_TEMP_OFF; 
         temp_time=millis();
         // light it off temporary if button pressed, remeber when
         };
       if ( digitalRead(pin_switch) == HIGH ) {
          status = ST_OFF;
          // light it off permanently if switch released - override TEMP, if both happens
          };
       break;

    case ST_OFF: 
       if ( millis()-temp_time > 24L*60L*60L ) {
          status = ST_ON;
          // light it on, if timeouted
          };
       if ( digitalRead(pin_switch) == HIGH ) {
          status = ST_OFF;
          // light it off permanently if switch released - overrides TEMP/ON
          };
       break;
    };

  digitalWrite(pin_led,(status == ST_ON) ); 
  // light the led if ST_ON, turn it off otherwise
  // we can keep old value or look for actual and write only if it differs, but it is the simplest way

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.