I am writing an application in C# that will send a serial.write to my COM port. All the examples I've seen of C# controlling the Arduino's pins focus on pulling only one pin. Below is my code. I can only pull pin 9 to HIGH right now, none of the other read commands work. Where's the schoolboy error? :)
Arduino code:
void setup() {
// Define inputs and outputs
pinMode(0, INPUT);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
// Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
}
void loop() {
if (Serial.read() == 'a')
{
digitalWrite(9, HIGH);
}
else if (Serial.read() == 'b')
{
digitalWrite(9, LOW);
}
else if (Serial.read() == 'c')
{
digitalWrite(10, HIGH);
}
else if (Serial.read() == 'd')
{
digitalWrite(10, LOW);
}
else if (Serial.read() == 'e')
{
digitalWrite(11, HIGH);
}
else if (Serial.read() == 'f')
{
digitalWrite(11, LOW);
}
else if (Serial.read() == 'g')
{
digitalWrite(12, HIGH);
}
else if (Serial.read() == 'h')
{
digitalWrite(12, LOW);
}
delay(100);
}
C# code
private void btnOP9on_Click(object sender, EventArgs e)
{
serialPort1.Write("a");
}
private void btnOP9off_Click(object sender, EventArgs e)
{
serialPort1.Write("b");
}