0
votes

I have an Arduino Uno connected with 4 LEDs which i want to control with a c# app. The LEDs are working, the wiring is done right but i have no idea why my code is not working. For now i want to ON/OFF the 4 LEDs seperately.

I have tried opening the serial monitor of the arduino ide to monitor whats happening but its not possible to do so because another instance was already busy with that one com port, meaning i can only open either the arduino serial monitor or the c# app.

(I have removed some brackets)

C#:

namespace ArduinoLed
{
public partial class MainWindow : Window
{
    ctrl controller = new ctrl();

    public MainWindow()
    {
        InitializeComponent();
        controller.Serialopen();
    }

    public void Clicked(object sender, RoutedEventArgs e)
    {
        Button clickedButton = (Button)sender;
        controller.Click(clickedButton);
    }
}

class ctrl
{
SerialPort port = new SerialPort("COM3", 9600);

public void Serialopen()
    {
        try
        {
            port.Open();
        }
        catch (Exception)
        {
           MessageBox.Show("Fail to open port (COM3)");
        }
    }

    public void Click(Button btn)
    {
        string name = btn.Name;

        if (btn.Content == "OFF")
        {
            btn.Content = "ON";
            port.WriteLine(btn.Name + "/ON");
        }
        else
        {
            btn.Content = "OFF";
            port.WriteLine(btn.Name + "/OFF");
        }
    }
}

ARDUINO:

void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);

}

void loop() {
  String message;
  if (Serial.available() > 0){
  message = Serial.readString();
  Serial.println(message);

if (message == "btnOne/ON"){
  digitalWrite(3, HIGH);
}  
if (message == "btnOne/OFF"){
  digitalWrite(3, LOW);  
}  
if (message == "btnTwo/ON"){
  digitalWrite(5, HIGH);  
}  
if (message == "btnTwo/OFF"){
  digitalWrite(5, LOW);  
}  
if (message == "btnThree/ON"){
  digitalWrite(6, HIGH); 
}  
if (message == "btnThree/OFF"){
  digitalWrite(6, LOW);
}  
if (message == "btnFour/ON"){
  digitalWrite(9, HIGH); 
}  
if (message == "btnFour/OFF"){
  digitalWrite(9, LOW);  
}
1
I think the problem is simple, this what you get is not what you compare. Maybe there is some additional end line symbol?CrazyBaran
I think this is my problem but how can I compare without being able to open the serial monitor, is there another way to do this ?lucasdm303
if i add messagebox.show(btn.name + "/ON") then I am able to see what should be sent and it is equal to what i have written the arduino should be receivinglucasdm303
Use for example RealTerm to spy what go to arduino. Implement or find how to make in arduino .contains and compare string with it, so you avoid a problem of additional character .CrazyBaran
i tried the .contains method to compare the string and i got it working now. if (message.indexOf("One") > 0 and message.indexOf("ON") > 0){...} thanks !!!lucasdm303

1 Answers

0
votes

Call message.trim(); before you compare the values. This will remove all unreadable chars.