-2

Ok so I have been trying to make a sound controlled servo with an Arduino code and I seem to be doing something wrong. What is happening is i am testing the code with the built in checker to the program and it keeps giving me an error code saying

expected primary-expression before token ','

I have tried fixing it based on the error messages but nothing has helped. I will provide the code below.

int micsensorpin=3;
int micState;
int pos = 0;
#include <Servo.h>
Servo explorer1;

void setup(){
  pinMode(Servo, OUTPUT);
  pinMode(micsensorpin, INPUT);
  digitalWrite( micsensorpin, LOW);
  digitalWrite(pos = 0);
  explorer1.attach(2);
}

void loop(){
  micState=digitalRead(micsensorpin);
  if(micState==HIGH)
  {
    digitalWrite(pos = 0; pos <=70; pos +=8);
    explorer1.write(pos);
    delay(500);
    
  }
    else  {
      digitalWrite(pos=70; pos >=0; pos -= 8);
      explorer1.write(pos);
      delay(500);
    }
  
}

The line of error is pinMode(Servo, OUTPUT);.

gre_gor
  • 6,669
  • 9
  • 47
  • 52

1 Answers1

0

It looks like you need to do a little bit of study on how C++ and Arduino code works.

First off, this line:

pinMode(Servo, OUTPUT);

pinMode expects a pin number as the first argument. Servo is the name of a type, not a pin number. If you will look at the examples for the Servo library you will notice that they never have any pinMode lines for the servo pin. I'm not sure why you thought you needed this line, but you should simply remove the entire line.

Then there's this line:

digitalWrite(pos = 0);

I'm not sure what you're trying to do here either. digitalWrite expects a pin number and a HIGH or LOW. Look at the examples of digitalWrite. Have you ever seen one like this? I think you just want to set the pos variable to 0. It's already set to 0 from where you created it. You can simply remove this entire line as well.

The loop code is equally as confusing. You have this line:

digitalWrite(pos = 0; pos <=70; pos +=8);

It looks like you've somehow confused a digitalWrite for a for loop. Have a look at some of the examples for the Servo library and see if you can't figure out what this is supposed to look like.

The only thing digitalWrite does is set a pin to 5V or 0V. It doesn't set variables. It just sets the digital voltage on a digital pin. IN the parenthesis will always be a pin number, a comma, and a HIGH or LOW. If you are putting anything else in the parenthesis after a digitalWrite command then you are doing things wrong.

Please take a little time to read some of the material on Arduino. There are lots of tutorials that will help you learn how to use these commands. You can't just make up syntax, you have to use them as they are intended.

Delta_G
  • 2,927
  • 2
  • 9
  • 15
  • sorry, im like still learning this stuff. Ive gotten movement from motion on a servo and a light with the sound sensor. Ill do those and see what else needs to be fixed. Ill look at the examples – Kenneth the carnerd Jul 10 '20 at 01:45