Random servos...Sketch qustions

Hello-

Here I have a sketch, which is working fine, but I have some questions.

Two servos, 1 & 2, run at random when pin 7 is LOW.

I have 2 potentiometer wipers on pins A0 and A1. When pin 7 is HIGH, the two potentiometers control servo position.

Does my sketch look right?

I am not clear on when I need "}" these brackets…

Why do I need 3 }}} at the end of the sketch to make it work?

Is this called syntax or format? Is anything out of the ordinary?

thanks, steve

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Servo.h>      // include the servo library

Servo servo1;       
Servo servo2;       

unsigned long prevMillis = 0;
long interval = 500;

int switchPin = 7;

int pos;
int pos2;

void setup() {

  servo1.attach(10);  
  servo2.attach(9);  

  pinMode(switchPin, INPUT);
}

void loop()

{
  if (digitalRead(switchPin) == HIGH)
  {
    int analogValue = analogRead(A0);
    Serial.println(analogValue);

    // if your sensor's range is less than 0 to 1023, you'll need to
    // modify the map() function to use the values you discovered:
    int servoAngle = map(analogValue, 0, 1023, 0, 179);

    // move the servo using the angle from the sensor:
    servo1.write(servoAngle);


    int analogValue2 = analogRead(A1);
    Serial.println(analogValue2);

    // if your sensor's range is less than 0 to 1023, you'll need to
    // modify the map() function to use the values you discovered:
    int servoAngle2 = map(analogValue2, 0, 1023, 0, 179);

    // move the servo using the angle from the sensor:
    servo2.write(servoAngle2);  
  }
  else
  {
    unsigned long newMillis = millis();
    if (newMillis - prevMillis > interval){
      prevMillis = newMillis;

      int pos = random(0, 180);
      int pos2 = random(0, 180);

      servo1.write(pos);
      servo2.write(pos2);

    }
  }
}

stevex:
I am not clear on when I need "}" these brackets…

Why do I need 3 }}} at the end of the sketch to make it work?

Is this called syntax or format? Is anything out of the ordinary?

A pair of curly braces encloses a block of code. It is used to signify to the compiler that everything in the block is related in a very important way..

   x = 5;
   if (x == 5)
      Serial.print("x = ");
      Serial.println(x);

The above code will print
x = 5
but if you change the assignment to x = 6, the code will print
6

Why? Because the if statement only affects the line directly after it. So when x contains 6, the "x =" will not print (because x does not contain 5), but the next line has no conditional associated with it, so it will alwys execute regardless of the value in x.

Changing the code to:

   x = 5;
   if (x == 5) {
      Serial.print("x = ");
      Serial.println(x);
   }

Causes the two Serial.print lines to be treated as a block, so that they will both be executed if x contains 5, and neither will be executed of x does not contain 5.

As for the }}} at the end, well, EVERY open curly brace REQUIRES a closing curly brace, so something like this...

void loop() {
    if (whatever) {
       dostuff;
        if (stuff) {
          doOtherStuff;
          andSoOn;
       }
   }
}

If you put the cursor on a curly brace (or a parenthesis) in the IDE, the editor will draw a little rectangle around the matching character of the pair.