# Arduino Lifetest and Agetest

**URL:** <https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344>\
**Category:** Programming\
**Created:** [October 7, 2022, 7:41am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344 "2022-10-07T07:41:45Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidgimenez17](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@davidgimenez17](https://forum.arduino.cc/u/davidgimenez17)\
**Post date:** [October 7, 2022, 7:41am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/1 "2022-10-07T07:41:46Z")

</div>

Good, I'm trying to do a lifetest programming (permanent ignition) age test (intermittent ignition) to test products with a relay connected to 230V. There are 2 buttons, I press 1st button and the relay turns on permanently, if I press button 1 again and the relay turns off. I press button 2 and the relay works intermittently, if I press it again it turns off.

PROBLEMS TO SOLVE: Button 2 does not work if it does not coincide with the beginning of the code reading  
I need to insert a numerical cycle counter.

I leave the thinkerCad Circuits link: [Login | Tinkercad](https://www.tinkercad.com/things/gHPu7LcfO76-lifetest-agetest/editel?sharecode=VqeP_rJj5Q99A_06j4h69V8AIiZ1i0QzTjjabimwABs)

```cpp
Código:
const int pulsador1=4;
const int pulsador2=7;
int val=0; valor actual del pulsador1
int val1=0;
int old_val=0;
int old_val1=0;
int estado=0;
int estado1=0;

void setup(){
pinMode(LED_BUILTIN,OUTPUT); pinMode(pulsador1,INPUT); pinMode(pulsador2,INPUT);

}

void loop(){
val=digitalRead(pulsador1); val1=digitalRead(pulsador2);

if ((val==HIGH)&& (old_val==LOW)){
state=1-state;
}
old_val = val;
if (state==1){
digitalWrite(LED_BUILTIN,HIGH);
}
else{
digitalWrite(LED_BUILTIN,LOW);
}

if (val1 && old_val1==0){ state1=
1-state1;
}
old_val1 = val1;
if (state1==1){
digitalWrite(LED_BUILTIN,HIGH);

delay(1000);

delay(1000);
}
else{
digitalWrite(LED_BUILTIN,LOW);
}
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [October 7, 2022, 7:44am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/2 "2022-10-07T07:44:47Z")

</div>

Welcome to the forum

> [@davidgimenez17](#):
>
> Code:

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'  
  
Use **code tags** (the \</\> icon above the compose window)&nbsp;to make it easier to read and copy for examination

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 7, 2022, 7:44am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/3 "2022-10-07T07:44:49Z")

</div>

First problem to solve is to find the code tag button in the tool bar and fix the look of your first post…

please read [How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966) and modify your post accordingly (including code tags and necessary documentation for your ask).

---

<div class="post-metadata">

**Author:** ![red\_car](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@red\_car](https://forum.arduino.cc/u/red_car)\
**Post date:** [October 7, 2022, 7:52am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/4 "2022-10-07T07:52:39Z")

</div>

> [@davidgimenez17](#):
>
> val=digitalRead(pulsador1);  
> val1=digitalRead(pulsador2);

Argh !!!

Why not...

```arduino
val1=digitalRead(pulsador1);
val2=digitalRead(pulsador2);

```

?

You are asking for pain ! ... I can't look at the rest of the code.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [October 7, 2022, 7:56am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/5 "2022-10-07T07:56:52Z")

</div>

Why not go the whole hog and use arrays, or even better an array of structs ?

---

<div class="post-metadata">

**Author:** ![red\_car](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@red\_car](https://forum.arduino.cc/u/red_car)\
**Post date:** [October 7, 2022, 7:59am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/6 "2022-10-07T07:59:05Z")

</div>

> [@UKHeliBob](#):
>
> Why not go the whole hog

One step at a time Bob... one step at a time.

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [October 7, 2022, 7:59am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/7 "2022-10-07T07:59:08Z")

</div>

> [@davidgimenez17](#):
>
> ```arduino
> delay(1000);
> 
> delay(1000);
> 
> ```

That's 2 seconds where nothing happens, including:

> [@davidgimenez17](#):
>
> Button 2 does not work if it does not coincide with the beginning of the code reading

As the rest of the code is probably done in less than a millisecond you can assume that it will take anything up to 2 seconds to read a button.

You have defined the buttons as INPUT, do they have pull-up or pull-down resistors?

Some things to help you:

> [@Buttons and other electro-mechanical inputs (introduction)](https://forum.arduino.cc/t/buttons-and-other-electro-mechanical-inputs-introduction/688576):
>
> Intended audience This tutorial is intended for people who have mastered the basics of electronics and C/C++ programming. If you are a beginner and find the concepts difficult, please review the basic examples in the Arduino IDE. In this tutorial BUTTON will be a generic term for any kind of switch or other input device with mechanical contacts. Introduction A common requirement in many projects is to have push buttons, switches or other electro-mechanical devices connected as inputs to an A…

> [@Using millis() for timing. A beginners guide](https://forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/483573):
>
> Part 1 It is not usually long before new Arduino users discover that although the delay() function is easy to use it has side effects, the main one of which is that its stops all activity on the Arduino until the delay is finished (not quite true, I know, but that is usually how the problem presents itself). When this occurs the new user is usually directed to the BlinkWithoutDelay example sketch (BWoD) and/or the excellent thread entitled Several things at the same time. [Several things at the…](http://forum.arduino.cc/index.php?topic=223286.0)

[Demonstration for several things at the same time](https://forum.arduino.cc/index.php?topic=223286.0)

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 7, 2022, 8:23am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/8 "2022-10-07T08:23:27Z")

</div>

consider

```cpp
#undef MyHW
#ifdef MyHW
const byte PinButs [] = { A1, A2 };
#else
const byte PinButs [] = { 4, 7 };
#endif

#define Nbut sizeof(PinButs)
byte butState [Nbut];

byte PinRelay = LED_BUILTIN;
byte stateRelay = 0;

enum { StOff, StOn, StIntermittent };
int mode;

enum { LedOff = HIGH, LedOn = LOW };

unsigned long period;
unsigned long msecLst;;

// -----------------------------------------------------------------------------
void
setRelay (
    int on )
{
    digitalWrite (PinRelay, on);
    stateRelay = on;
}

// -----------------------------------------------------------------------------
bool
butPress (
    int idx )
{
    byte but = digitalRead (PinButs [idx]);

    if (butState [idx] != but) {
        butState [idx] = but;
        return LOW == but;
    }
    
    return false;
}

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();
    // update output

    if (StIntermittent == mode && (msec - msecLst) >= period) {
        msecLst = msec;
        period = random (0, 500);
        setRelay (! stateRelay);
    }

    // check buttons
    if (butPress (1)) {
        mode = StIntermittent;
        period = 0;
    }

    if (butPress (0)) {
        switch (mode) {
        case StOff:
            mode = StOn;
            setRelay (LedOn);
            break;

        case StIntermittent:
        case StOn:
        default:
            mode = StOff;
            setRelay (LedOff);
            break;
        }
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (LED_BUILTIN, OUTPUT);

    for (unsigned n = 0; n < Nbut; n++) {
        pinMode (PinButs [n], INPUT_PULLUP);
        butState [n] = digitalRead (PinButs [n]);
    }
}

```

---

<div class="post-metadata">

**Author:** ![davidgimenez17](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@davidgimenez17](https://forum.arduino.cc/u/davidgimenez17)\
**Post date:** [October 21, 2022, 12:31pm UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/9 "2022-10-21T12:31:12Z")

</div>

That code is very well done, but I prefer to use the structure of my code.  
My idea is to make a language and structure as simple and basic as possible. In Void loop 2 blocks of conditions appear; the first block for 1 button (permanent on and off) and another block for another button (intermittent on and off).

```cpp
const int pulsador1=4;
const int pulsador2=7;
int val1=0; //current value of button1
int val2=0;
int old_val1=0; //previous value button 1
int old_val2=0;
int state1=0; //output state when booting arduino
int state2=0;

void setup(){
pinMode(LED_BUILTIN,OUTPUT);
pinMode(pulsador1,INPUT);
pinMode(pulsador2,INPUT);
(pulsador1 && pulsador2 == LOW); //button 1 and 2 to low
if (digitalRead(pulsador1 && pulsador2 == LOW)) { // Here I would read the button and until button == LOW you don't start
digitalWrite(state1 && state2 == LOW);
}
else{}
// here I would put comparison loop
//serial println

void loop()
{
val1=digitalRead(pulsador1); //write to 0
val2=digitalRead(pulsador2); //read status

if ((val1==HIGH)&& (old_val1==LOW)){
state1=1-state1;

old_val1 = val1;
(state1==1);
digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(pulsador1, HIGH);
}
else{
digitalWrite(LED_BUILTIN,LOW);
digitalWrite(pulsador2, HIGH);
}

if (val2 && old_val2==0){
state2=1-state2;

old_val2 = val2;
(state2==1);
digitalWrite(LED_BUILTIN,HIGH);
delay(1000);
digitalWrite(LED_BUILTIN,LOW);
delay(1000);
}
else{
digitalWrite(LED_BUILTIN,LOW);
}
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [October 21, 2022, 12:57pm UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/11 "2022-10-21T12:57:52Z")

</div>

> [@davidgimenez17](#):
>
> (state1==1);

This statement does nothing. If you meant it as a comment, an assertion of something true at this point in the code, you should use a traditional comment.

If on the other hand, you think it does something, you may want to change it so it actually does. Something.

a7

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [October 21, 2022, 1:06pm UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/12 "2022-10-21T13:06:02Z")

</div>

> [@J-M-L](#):
>
> First problem to solve is to find the code tag button in the tool bar and fix the look of your first post…

Sigh.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 21, 2022, 2:15pm UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/13 "2022-10-21T14:15:36Z")

</div>

> [@davidgimenez17](#):
>
> My idea is to make a language and structure as simple and basic as possible.

redundant code is typically more confusing. separating I/O from processing is a conventional approach

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [October 21, 2022, 2:49pm UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/14 "2022-10-21T14:49:50Z")

</div>

@davidgimenez17

Please do not troll the forum.  
Use code tags, please

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 21, 2022, 3:08pm UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/15 "2022-10-21T15:08:49Z")

</div>

> [@camsysca](#):
>
> Sigh

Sigh for what ? you think it's a lost cause ? 🙂

---

<div class="post-metadata">

**Author:** ![davidgimenez17](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@davidgimenez17](https://forum.arduino.cc/u/davidgimenez17)\
**Post date:** [October 23, 2022, 10:13pm UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/16 "2022-10-23T22:13:18Z")

</div>

Sorry, I'm new to programming and to the forum. I have already solved

---

<div class="post-metadata">

**Author:** ![mrburnette](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mrburnette/32/702556_2.png) [@mrburnette](https://forum.arduino.cc/u/mrburnette)\
**Post date:** [October 23, 2022, 10:26pm UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/17 "2022-10-23T22:26:41Z")

</div>

We understand you are new; but every "help" forum generally has a basic set of general rules to follow to expedite your engagement with other forum members: [here](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966)

For future inquiries/assistance, following the above will eliminate much of the static noise you received this time around.

Additionally since you corrected your own code, posting the working code is considered to be considerate and customary.

---

<div class="post-metadata">

**Author:** ![davidgimenez17](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@davidgimenez17](https://forum.arduino.cc/u/davidgimenez17)\
**Post date:** [October 24, 2022, 10:51am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/19 "2022-10-24T10:51:02Z")

</div>

What does msecLst mean?

> [@gcjr](#):
>
> ```arduino
> if (StIntermittent == mode && (msec - msecLst) >= period) {
> msecLst = msec;
> period = random (0, 500);
> setRelay (! stateRelay);
> 
> ```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 24, 2022, 11:30am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/20 "2022-10-24T11:30:24Z")

</div>

> [@davidgimenez17](#):
>
> What does msecLst mean?

it's the start time of the timer. the timer expires when period msecs have elapsed from msecLst.

resetting msecLst to msec when the timer expires (the condition is true) restart the timer

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2023, 11:31am UTC](https://forum.arduino.cc/t/arduino-lifetest-and-agetest/1039344/21 "2023-04-22T11:31:01Z")

</div>

Closed on April 22, 2023, 11:31am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
