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);
}
}
}