Cant find a solution to an error trying to use C# to talk with servo and realy

I’ve been working on this project for a few days and I cant figure out why I’m still getting a error I’m trying to move two servos with my mouse and activate a relay with a click using IDE and C# but I cant find a solution after looking for about 2 hours, this is the message I keep getting.

expected primary-expression before ‘}’ token

#include<Servo.h>
Servo serX;
Servo serY;
const int relay1Pin = 7;
void setup() {

  serX.attach(11);
  serY.attach(10);
  pinMode(relay1Pin, OUTPUT); 
  digitalWrite(relay1Pin, HIGH);
  Serial.begin(115200);
  
}

void loop(){
  int pos;
  if (Serial.available()){ 
    delay(100);}
    while (Serial.available() > 0){
      pos = Serial.read();}
      {if (pos == '0')
        digitalWrite(relay1Pin, LOW);
      else if (pos == 'a')
        digitalWrite(relay1Pin, HIGH);
      else if (pos == '1');}



void serialEvent(){
  serialData = Serial.readString();
  serX.write(parseDataX(serialData));
  serY.write(parseDataY(serialData));
}

int parseDataX(String data)
  data.remove(data.indexOf("Y"));
  data.remove(data.indexOf("X"), 1);
  return data.toInt();
}

int parseDataY(String data)
  data.remove(0,data.indexOf("Y") + 1);
  return data.toInt();
}

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

void loop()
{
  int pos;
  if (Serial.available())
  {
    delay(100);
    while (Serial.available() > 0)
    {
      pos = Serial.read();
      if (pos == '0')
        digitalWrite(relay1Pin, LOW); //
      else if (pos == 'a')
        digitalWrite(relay1Pin, HIGH); //
      else if (pos == '1')
      }
    void serialEvent()
    {
etc,etc

Where does the loop() function end ?

NOTE that the code has been Auto Formatted in the IDE to show the indentation

This code is missing at least two }

void loop(){
  int pos;
  if (Serial.available()){ 
    delay(100);
    while (Serial.available() > 0){
      pos = Serial.read();
      if (pos == '0')
        digitalWrite(relay1Pin, LOW); //
      else if (pos == 'a')
        digitalWrite(relay1Pin, HIGH); //
      else if (pos == '1')

}

I would also recommend always using {} with every IF and ELSE statement

...R

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.