Button Control C# .NET

Hello;
i have some problem from my arduino or c# codes.

if i push button1 arduino send to c# value on serial comminication, but if i press button2, button3 or button4 arduino can not send anything. i cant find the problem is where :confused:
best regards.

Arduino codes:

char x;
char b;
int button1 = digitalRead(2);
int button2 = digitalRead(3);
int button3 = digitalRead(4);
int button4 = digitalRead(5);
void setup()
{

  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

}

void loop()
{
  if(Serial.available()>0)
    x=Serial.read();
  if(x=='a')
  {
    if (button1 == HIGH)
    {
      button1 = digitalRead(2);
      Serial.println(1);
      delay(100);
    }

  }
 else if(x=='b')
  {
    if (button2 == HIGH)
    {
      button2 = digitalRead(3);
      Serial.println(2);
      delay(100);
    }

  }
  else if(x=='c')
  {
    if (button3 == HIGH)
    {
      button3 = digitalRead(4);
      Serial.println(3);
      delay(100);
    }

  }
  else if(x=='d')
  {
    if (button4 == HIGH)
    {
      button4 = digitalRead(5);
      Serial.println(4);
      delay(100);
    }

  }

}

C# codes

 private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                seriPort.Write("a");
                int button1 = Convert.ToInt32(seriPort.ReadExisting());


                if (button1 == 1)
                {

                    lstButtonlar.Items.Add("Button 1 basıldı! " + DateTime.Now);
                }


                seriPort.Write("b");
                int button2 = Convert.ToInt16(seriPort.ReadExisting());
                if (button2 == 2)
                {

                    lstButtonlar.Items.Add("Button 2 basıldı! " + DateTime.Now);
                }

                seriPort.Write("c");
                int button3 = Convert.ToInt16(seriPort.ReadExisting());

                if (button3 == 3)
                {

                    lstButtonlar.Items.Add("Button 3 basıldı! " + DateTime.Now);
                }

                seriPort.Write("d");
                int button4 = Convert.ToInt16(seriPort.ReadExisting());

                if (button4 == 4)
                {

                    lstButtonlar.Items.Add("Button 4 basıldı! " + DateTime.Now);
                }

                System.Threading.Thread.Sleep(100);
            }
            catch (Exception ex)
            {


            }
        }

What do you see when you just look at the values that the Arduino sends to the serial monitor?

if i press button 1 arduino send "1" and i read "1" on c#
if c# read "1" it add an item in list box like "button 1 pressed"

You asked how to find where the problem is. It makes sense to find out first whether it is on the Arduino or PC side. So leave the C# code out of it and poll the Arduino manually from the serial monitor.

Hint: you are testing your input variables before inputting them. :slight_smile:

I suspect this

Serial.println(1);

should be

Serial.println('1');

Also, I notice that the C# code you posted writes values rather than trying to read anything from the Arduino.

...R

thank you aarg and Robin2 its work!