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);
}