Back to Parent

int pinServo = A3;
Servo servoMotor;
int positionServo = 0;

int pinLightSensor = A0;
int lightSensorValue = 0;

void setup() {
    servoMotor.attach(pinServo);
    Particle.function("servoAdjust", adjustServo);
    Particle.variable("servoPosition", &positionServo, INT);
}

void loop() {
    lightSensorValue = analogRead(pinLightSensor);
    positionServo = calculateServoPosition(lightSensorValue);
    servoMotor.write(positionServo);
}

int adjustServo(String command) {
    int newPosition = command.toInt();
    if (newPosition == -1) {
        positionServo = calculateServoPosition(lightSensorValue);
        servoMotor.write(positionServo);
    } else {
        positionServo = constrain(newPosition, 0, 180);
        servoMotor.write(positionServo);
    }
    return 1;
}

int calculateServoPosition(int sensorValue) {
    return map(sensorValue, 2000, 4095, 0, 180);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0