val=analog.read (potpin);

It looks like you're using pin 10 to indicate whether sweep your servo through 180-degrees or through 90-degrees. Is that correct? Then you only need the if/else statements to select one of the two 'map()' calls. I see several problems:

  1. Your indentation suggests you tried to select 3 statements with your 'if', but an 'if' only refers to one following statement or block of statements. If you want it to control a block of statements, you need to surround those statements with braces: '{' and '}'.

  2. In this case, you need everything but the 'map' calls to be outside the 'if' statements.

  3. Statements (except 'if' and 'else' by themselves) need to end with a semi-colon.

I think you meant to write:

val=analog.read (potpin);     // read the pot

if(pin10==HIGH)               // pin 10 selects servo range
  val=map(val,0,1023,0,180);  // map the pot value ...
else                          //   into one of
  val=map(0,1023,0,90);       //   the servo ranges

servowrite(val);              // drive the servo


I had try every thing , but there is some shaking and vibration when if condition is satisfied.

Was the shaking near rotation limits? Most servos, especially the inexpensive hobby ones, get unstable near each end of their travel. Some will move smoothly to the limit, others get shaky within a few degrees of the limit. Each one is a little bit different.

Update 2:

Here is a complete, working example based on the Arduino Servo-example and your specifications. There were two bugs above:

  1. I left out the 'val' argument in one of the map commands. This cause a compile error and should have prevented you from testing it. It could not have run, properly or improperly.

  2. In your code example, if(pin10==HIGH) doesn't read any input pin! It just reads some (unseen!) variable named 'pin10' - unless you did read the pin in some code you didn't show us? Which is why we ask for a minimum, complete, & verifiable example.

This code runs and rotates my sg90 servo through 90 degrees, if pin 10 is low, or 180 degrees, if pin 10 is high.

  • You need a (val,... in the second map function: arduino.cc/reference/en/language/functions/math/map Also, maybe a val = map(val,0,1023,0,90+90*pin10) would be sufficient. – Dave X Dec 19 '18 at 3:41
  • "These code r not working" - Please be specific: What were you expecting? What happened instead? What code did you use this time? Edit this into the question. "I use sg90 was there any prblm with this" - Nothing wrong with it. sg90 is an inexpensive, light-duty servo so just know that most of them will 'jitter' or shake near either end of their travel. If you keep them between 10 and 170 degrees, you should have no trouble. Or if you need the most range you can get, test each one to find its useful limits, – JRobert Dec 22 '18 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.