Code improvement advice.

Hello,

This is a part of my code. Its a simple code which checks if a button is pressed and then triggers a relay.

Because of instabillity of the remote control I want an addition to this code to make the relay "off" only if the button was not pressed the last 1,5sec

Thanks a lot.

while (down==1)
{
  digitalWrite(buttonRelay1, LOW);
  down = digitalRead(buttonPin1);
}

Thanks

The code I wrote need to be corrected (I figured out the need of the whole code)... Please post all of your code.

#include <Wire.h>




int buttonPin1 = 8;     // koumpi down
int buttonRelay1 = 9;    //relay down
int buttonPin2 = 10;     // koumpi up
int down;
int up;
int buttonRelay2 = 11;    //relay down




void  setup()

{
  Serial.begin(9600);



  pinMode(buttonPin1, INPUT);      
  pinMode(buttonPin2, INPUT);   
  pinMode(buttonRelay1, OUTPUT);  
  pinMode(buttonRelay2, OUTPUT);  
  Serial.begin(9600);
  digitalWrite (buttonRelay1, HIGH);
  digitalWrite (buttonRelay2, HIGH);


}

void loop()
{ 
  down = digitalRead(buttonPin1);
  while (down==1)
  {
    digitalWrite(buttonRelay1, LOW);
    down = digitalRead(buttonPin1);
  }
  delay(100);



  digitalWrite(buttonRelay1, HIGH);

  up = digitalRead(buttonPin2);
  while (up==1)
  {
    digitalWrite(buttonRelay2, LOW);
    up = digitalRead(buttonPin2);
    delay(100);
  }
  delay(100);
  digitalWrite(buttonRelay2, HIGH);
}

the delay that I added was to check something (just ignore them)

I declared your pin number as static const as they won't be changing. If this is REALLY your whole code, you don't need the up and down variables.

I changed your code to don't use while() loops.

The addition you asked can be done using millis().

See if it works:

#include <Wire.h>

static const int buttonPin1 = 8;     // koumpi down
static const int buttonRelay1 = 9;    //relay down
static const int buttonPin2 = 10;     // koumpi up
static const int buttonRelay2 = 11;    //relay down

unsigned long buttonPin1_Timer;
unsigned long buttonPin2_Timer;

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

  pinMode(buttonPin1, INPUT);     
  pinMode(buttonPin2, INPUT);   
  pinMode(buttonRelay1, OUTPUT); 
  pinMode(buttonRelay2, OUTPUT); 
  
  digitalWrite (buttonRelay1, HIGH);
  digitalWrite (buttonRelay2, HIGH);
}

void loop()
{
  if(digitalRead(buttonPin1)){
    digitalWrite(buttonRelay1, LOW);
    buttonPin1_Timer = millis();
  }
  else if(millis()-buttonPin1_Timer>1500)
    digitalWrite(buttonRelay1, HIGH);

  if(digitalRead(buttonPin2)){
    digitalWrite(buttonRelay2, LOW);
    buttonPin2_Timer = millis();
  }
  else if(millis()-buttonPin2_Timer>1500)
    digitalWrite(buttonRelay2, HIGH);
}

thanks a lot!!! It works great!!! It just needs one little correction...When it is starting (I plug the arduino) it triggers both relays for a short of time. Is it possible to avoid this??

The digital pins have initial output as LOW, and you relays turn on with LOW. So until your setup() begins, the relays will be ON.
The only way I can think to solve this is to modify your circuit to make them to turn ON with HIGH output from the digital pin.

on my preview code the relays was not triggered as the arduino started... :confused:

I guess it has to do with the register of the variables as the setup code is exactly the same.