#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <Servo.h>
Servo servo;
//change to whatever DIO you are using
// D9 = A2 on CPX
int lightPin1 = 9;
//change to whatever DIO you are using
// D10 = A3 on CPX
int lightPin2 = 10;
//change angle as needed
int minAngle = 110;
int maxAngle = 160;
// delay rate variable
int rate;
void setup() {
CircuitPlayground.begin();
//puts servo on DIO 2, which is A5 on the CPX
servo.attach(2);
pinMode(lightPin1, OUTPUT);
pinMode(lightPin2, OUTPUT);
}
void loop() {
if (CircuitPlayground.slideSwitch() == true)
{
//go into demo mode
servo.write(minAngle);
digitalWrite(lightPin1, HIGH);
digitalWrite(lightPin2, HIGH);
delay(500);
servo.write(maxAngle);
digitalWrite(lightPin1, LOW);
digitalWrite(lightPin2, LOW);
delay(500);
}
else
{
//go into sensor mode
//change the sensor if need be
rate = map(CircuitPlayground.soundSensor(), 0, 1023, 0, 2500);
servo.write(minAngle);
digitalWrite(lightPin1, HIGH);
digitalWrite(lightPin2, HIGH);
delay(rate);
servo.write(maxAngle);
digitalWrite(lightPin1, LOW);
digitalWrite(lightPin2, LOW);
delay(rate);
}
}
Comments