do-while problem

Hi all,

I am trying to set DS3231 clock with a 4x4 keypad using Arduino Mega.

What I am trying to achieve is, if I press the '’ key from the 4x4 keypad than the Mega should ask for me to enter 2 key strokes, if either key equals to ‘#’ it should break the do-while. Than it should convert these strokes to a 2 digit number by a simple calculation (x110 + x2). After that it should do a comparison. If this 2 digit number is equal to ‘0’ than it shouldn’t set the year.
If it’s 17 or greater it would set the year with this number. And if it’s smaller than 17 then this micro loop should repeat itself until desirable number comes out (0 or a number bigger than 17).

Below is my code, the do-while loop doesn’t work as I expect and I am not sure what is wrong and also I am a newbie.

Thanks in advance.

#include <Wire.h>
#include <DS3231.h>
#include <Keypad.h>

const byte Satir = 4; //dört satır
const byte Sutun = 4; //dört sütun
char Tuslar[Satir][Sutun] = {
	{ '1','2','3','A' },
	{ '4','5','6','B' },
	{ '7','8','9','C' },
	{ '*','0','#','D' }
};
byte rowPins[Satir] = { 9, 8, 7, 6 }; //tuş takımının satır çıkışlarına bağla
byte colPins[Sutun] = { 5, 4, 3, 2 }; //tuş takımının sütun çıkışlarına bağla 
Keypad TusTakimi = Keypad(makeKeymap(Tuslar), rowPins, colPins, Satir, Sutun);

int Yil;
int Ay;
int Gun;
int Sayi;
int SayiSonuc;
char Tus;

DS3231 clock;
RTCDateTime TarihZaman;

void setup()
{
	clock.begin();
	Serial.begin(115200);
}

void loop()
{
	TarihZaman = clock.getDateTime();
	Yil = TarihZaman.year;
	Ay = TarihZaman.month;
	Gun = TarihZaman.day;
	char Tus = TusTakimi.getKey();
	if (Tus == '*') {
		Serial.println("Tarih ayarlama modu, 2 basamak 'Yil' degerini girin. Vazgeçmek için '#' tuşuna basın."); [b]//year setting mode instructions[/b]
		do {
			SayiSonuc = TusGir();
		} while (SayiSonuc == 0 || SayiSonuc > 16); [b]//this comparison does not work[/b]
		if (SayiSonuc == 0) {
			Yil = TarihZaman.year;
		}
		else {
			Yil = SayiSonuc + 2000;
		}

	}
// serial print the year every second
	if (MevcutZaman - EskiSaniye > 1000) {
		Serial.print(TarihZaman.year);
Serial.print(" ");
EskiSaniye = MevcutZaman;
	}
	MevcutZaman = millis();
}

int TusGir() {
	// 1st digit
		int Tus1 = TusTakimi.getKey();
		while (Tus1 == NO_KEY) {
			Tus1 = TusTakimi.getKey();
			switch (Tus1) {
			case '*': case 'A': case 'B': case 'C': case 'D':
				Serial.print("Hatalı giriş yaptınız. ");
			case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
				Tus1 = (Tus1 - '0');
			case '#':
				break;
			}
		}
		// 2nd digit
		int Tus2 = TusTakimi.getKey();
		while (Tus2 == NO_KEY) {
			Tus2 = TusTakimi.getKey();
			switch (Tus2) {
			case '*': case 'A': case 'B': case 'C': case 'D':
				Serial.print("Hatalı giriş yaptınız. ");
			case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
				Tus2 = (Tus2 - '0');
			case '#':
				break;
			}
		}
		SayiSonuc = (Tus1 * 10) + Tus2; //number calculation
	return SayiSonuc;

You forgot break statements on your first two cases in the switch in the TisGir method so those cases will fall through. The break in the last case breaks the switch statement and not the while loop.

You should have a look at the switch case stuff in any good c++ tutorial. And you need to set some variable that you can use after the switch case to break the while since any break inside the switch is just going to break the switch and not the while.

Thanks, I solved the problem.