Search code examples
carduinoservo

I can't see a mistake in my Arduino code.


Here is my code and when I put it into the Arduino IDE it says:

"expected primary-expression before ',' token"

I think I forgot somewhere a dot or I made a little mistake in the if class.

Thank you very much for your help.

Code:

#include <Servo.h>

#define trigPin 13
#define echoPin 12
#define PinOut1 

Servo myservo; 
int AnalogIn=A0;
int buttonState = 0;
int integer1 = 3;
float floating1 = PI;
String string1 = "words and numbers123";
int array1[5] = {100, 200, 300, 400, 500};
int PinIn1 = 2;
int PinOUt = 11;
int button = 0;

void setup() {
  pinMode(PinOUt, OUTPUT);
  pinMode(PinIn1, INPUT);
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {
  Conditional1();
}

void Conditional1() {
  button = digitalRead(PinIn1);
  if (buttonState == HIGH) {
    digitalwrite(PinOut1, HIGH);
    myservo.write(60);
  } else{
    digitalwrite(PinOut1, LOW);
    myservo.write(0);
  }
}

Solution

  • On line 4 it says #define PinOut1 try adding a space to make it #define PinOut 1

    EDIT: There were also a few other things I noticed after this post so I compiled a fixed version of your code:

    #include <Servo.h>
    #define trigPin 13
    #define echoPin 12
    #define PinOut1 11
    Servo myservo; 
    int AnalogIn=A0;
    int buttonState = 0;
    int integer1 = 3;
    float floating1 = PI;
    String string1 = "words and numbers123";
    int array1[5] = {100, 200, 300, 400, 500};
    
    
    int PinIn1 = 2;
    int PinOut = 11;
    int button = 0;
    
    
    void setup() {
      pinMode(PinOut, OUTPUT);
      pinMode(PinIn1, INPUT);
      Serial.begin(9600);
      myservo.attach(9);
    }
    void loop() {
    Conditional1();
    }
    
    void Conditional1() {
       button = digitalRead(PinIn1);
    
      if (buttonState == HIGH) {
    digitalWrite(PinOut1, HIGH);
        myservo.write(60);
    
      } else{
        digitalWrite(PinOut1, LOW);
        myservo.write(0);
    
      }
    }