Arduino Lifetest and Agetest

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

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);
}
}

Welcome to the forum

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) to make it easier to read and copy for examination

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 and modify your post accordingly (including code tags and necessary documentation for your ask).

Argh !!!

Why not...

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

?

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

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

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

That's 2 seconds where nothing happens, including:

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:

Demonstration for several things at the same time

consider

#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]);
    }
}

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).

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);
}
}

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

Sigh.

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

@davidgimenez17

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

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

1 Like

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

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

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.

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

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