Problem with adjustable relay timer.

Hello there. I just joined to the forum. I want to make a timer for a friends garden lights. I want to on/off the relay with 3 different timing options which is selectable with three momentary push buttons and there will be 3 leds to indicate which mode is on. (18h/6h, 12h/12h, 20h/4h). Buttons are connected to ground and 2,3,4 numbered pins with internal pullup resistors of atmega8a. Leds are connected to ground and 5,6,7 numbered pins of atmega8a with 100ohm current limiting resistors. I also connected a 16.000mhz crystal to XTAL1 and XTAL2 pins of atmega8a with 2 30pf capacitors to ground. I burn the bootloader of arduino ng into the chip. However when i power the board only the first led lightens up and buttons don't change a thing. I am quite new into this things and english is not my native language so sorry in advance. This is my code.

int buttpin0 = 2;
int buttpin1 = 3;
int buttpin2 = 4;
int ledpin0 = 5;
int ledpin1 = 6;
int ledpin2 = 7;
int role0 = 8;
int role1 = 9;
int vaziyet;

void setup() {
  pinMode(buttpin0, INPUT_PULLUP);
  pinMode(buttpin1, INPUT_PULLUP);
  pinMode(buttpin2, INPUT_PULLUP);
  pinMode(role0, OUTPUT);
  pinMode(role1, OUTPUT);
  pinMode(ledpin0, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
}

void loop() {
  // your loop code here

  switch (vaziyet) {
    case 0:    // 18-6
      digitalWrite(role0, LOW);
      digitalWrite(role1, LOW);
      digitalWrite(ledpin0, HIGH);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      myDelay(64800000);
      digitalWrite(role0, HIGH);
      digitalWrite(role1, HIGH);
      myDelay(21600000);
      break;
    case 1:    // 12-12
      digitalWrite(role0, LOW);
      digitalWrite(role1, LOW);
      digitalWrite(ledpin0, LOW);
      digitalWrite(ledpin1, HIGH);
      digitalWrite(ledpin2, LOW);
      myDelay(43200000);
      digitalWrite(role0, HIGH);
      digitalWrite(role1, HIGH);
      myDelay(43200000);
      break;
    case 2:    // 20-4
      digitalWrite(role0, LOW);
      digitalWrite(role1, LOW);
      digitalWrite(ledpin0, LOW);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, HIGH);
      myDelay(72000000);
      digitalWrite(role0, HIGH);
      digitalWrite(role1, HIGH);
      myDelay(14400000);
      break;
  }
}


void myDelay(unsigned long duration)
{
  unsigned long start = millis();

  while (millis() - start <= duration) {
    checkButtons();  // check the buttons
  }
}


void checkButtons()
{
  int  buttonstate0 = digitalRead(buttpin0);
  int  buttonstate1 = digitalRead(buttpin1);
  int  buttonstate2 = digitalRead(buttpin2);


  if (buttonstate0 == LOW)
  {
    vaziyet = 0;
  }

  if (buttonstate1 == LOW)
  {
    vaziyet = 1;
  }

  if (buttonstate2 == LOW)
  {
    vaziyet = 2;
  }
}

Thank you very much. Love you all.

Try the example Blink sketch with the different LEDs to make sure the controller is working.

BTW
Let us assume you are in State 0.
You are locked into the 'while' loop until the timing goes false.

while (millis() - start <= duration) 
  {
    checkButtons();  // check the buttons
  }

Thanks for fast and helpful response. I tried blink sketch and i can confirm all three leds are able to blink. Do you have any advise to check the buttons instead of while loop?

I think i found out. Just deleted while loop all togehter and put a checkButtons (); at the beginning of the void loop. Thank you very much.

int buttpin0 = 2;
int buttpin1 = 3;
int buttpin2 = 4;
int ledpin0 = 5;
int ledpin1 = 6;
int ledpin2 = 7;
int role0 = 8;
int role1 = 9;
int vaziyet;

void setup() {
  pinMode(buttpin0, INPUT_PULLUP);
  pinMode(buttpin1, INPUT_PULLUP);
  pinMode(buttpin2, INPUT_PULLUP);
  pinMode(role0, OUTPUT);
  pinMode(role1, OUTPUT);
  pinMode(ledpin0, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
}

void loop() {
  // your loop code here
    checkButtons();  // check the buttons

  switch (vaziyet) {
    case 0:    // 18-6
      digitalWrite(role0, LOW);
      digitalWrite(role1, LOW);
      digitalWrite(ledpin0, HIGH);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      myDelay(64800000);
      digitalWrite(role0, HIGH);
      digitalWrite(role1, HIGH);
      myDelay(21600000);
      break;
    case 1:    // 12-12
      digitalWrite(role0, LOW);
      digitalWrite(role1, LOW);
      digitalWrite(ledpin0, LOW);
      digitalWrite(ledpin1, HIGH);
      digitalWrite(ledpin2, LOW);
      myDelay(43200000);
      digitalWrite(role0, HIGH);
      digitalWrite(role1, HIGH);
      myDelay(43200000);
      break;
    case 2:    // 20-4
      digitalWrite(role0, LOW);
      digitalWrite(role1, LOW);
      digitalWrite(ledpin0, LOW);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, HIGH);
      myDelay(72000000);
      digitalWrite(role0, HIGH);
      digitalWrite(role1, HIGH);
      myDelay(14400000);
      break;
  }
}


void myDelay(unsigned long duration)
{
  unsigned long start = millis();

}


void checkButtons()
{
  int  buttonstate0 = digitalRead(buttpin0);
  int  buttonstate1 = digitalRead(buttpin1);
  int  buttonstate2 = digitalRead(buttpin2);


  if (buttonstate0 == LOW)
  {
    vaziyet = 0;
  }

  if (buttonstate1 == LOW)
  {
    vaziyet = 1;
  }

  if (buttonstate2 == LOW)
  {
    vaziyet = 2;
  }
}

I am so stupid. When i delete while loop i deleted all the timing management. Now i changed while with if. Now buttons are responsive but not sure about if relays will work. I will try tomorrow and write here.

What voltage is the Mega8 running on? What color LEDs? Remember the AVR's max pin current (40 mA), continuous should be < 25 mA.

Atmega8 running at 5V. Leds are slow blinking 3.2v 20ma. Here is the new code which looks like it is working.

int buttpin0 = 2;
int buttpin1 = 3;
int buttpin2 = 4;
int ledpin0 = 5;
int ledpin1 = 6;
int ledpin2 = 7;
int role0 = 8;
int role1 = 9;
int vaziyet;
int interval;
unsigned long previousMillis = 0;
const unsigned long on18 = 64800000;
const unsigned long off6 = 21600000;
const unsigned long onoff12 = 43200000;
const unsigned long on20 = 72000000;
const unsigned long off4 = 14400000;
boolean rolestate = true;

void setup() {
  pinMode(buttpin0, INPUT_PULLUP);
  pinMode(buttpin1, INPUT_PULLUP);
  pinMode(buttpin2, INPUT_PULLUP);
  pinMode(role0, OUTPUT);
  pinMode(role1, OUTPUT);
  pinMode(ledpin0, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  pinMode(ledpin2, OUTPUT);
}

void loop() {
  // your loop code here
  checkButtons();
  switch (vaziyet) {
    case 0:    // 18-6
      digitalWrite(role0, rolestate);
      digitalWrite(role1, rolestate);
      digitalWrite(ledpin0, HIGH);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, LOW);
      myDelay186();
      break;
    case 1:    // 12-12
      digitalWrite(role0, rolestate);
      digitalWrite(role1, rolestate);
      digitalWrite(ledpin0, LOW);
      digitalWrite(ledpin1, HIGH);
      digitalWrite(ledpin2, LOW);
      myDelay1212();
      break;
    case 2:    // 20-4
      digitalWrite(role0, rolestate);
      digitalWrite(role1, rolestate);
      digitalWrite(ledpin0, LOW);
      digitalWrite(ledpin1, LOW);
      digitalWrite(ledpin2, HIGH);
      myDelay204();
      break;
  }
}


void myDelay186 ()
{
  unsigned long currentMillis = millis();

  if ((unsigned long)(currentMillis - previousMillis) >= interval) {
    if (rolestate) {
      interval = on18;
    } else {
      interval = off6;
    }
    rolestate = !(rolestate);
    previousMillis = currentMillis;
  }
}

void myDelay1212 ()
{
  unsigned long currentMillis = millis();

  if ((unsigned long)(currentMillis - previousMillis) >= interval) {
    if (rolestate) {
      interval = onoff12;
    } else {
      interval = onoff12;
    }
    rolestate = !(rolestate);
    previousMillis = currentMillis;
  }
}

void myDelay204 ()
{
  unsigned long currentMillis = millis();

  if ((unsigned long)(currentMillis - previousMillis) >= interval) {
    if (rolestate) {
      interval = on20;
    } else {
      interval = off4;
    }
    rolestate = !(rolestate);
    previousMillis = currentMillis;
  }
}

void checkButtons()
{
  int  buttonstate0 = digitalRead(buttpin0);
  int  buttonstate1 = digitalRead(buttpin1);
  int  buttonstate2 = digitalRead(buttpin2);


  if (buttonstate0 == LOW)
  {
    vaziyet = 0;
  }

  if (buttonstate1 == LOW)
  {
    vaziyet = 1;
  }

  if (buttonstate2 == LOW)
  {
    vaziyet = 2;
  }
}