//--------------------------------------------------------------------------------
// Define a pin that we'll place the FSR on
// Remember to add a 10K Ohm pull-down resistor too.
int fsrPin = A0;
// Create a variable to hold the FSR reading
int fsrReading = 0;
// Define a pin we'll place an LED on
int ledPin = D2;
// Create a variable to store the LED brightness.
int ledBrightness = 0;
//--------------------------------------------------------------------------------
int servoPin = A3;
Servo myServo;
int servoPos = 0;
void setup() {
// Set up the LED for output
pinMode(ledPin, OUTPUT);
// Create a cloud variable of type integer
// called 'light' mapped to photoCellReading
Particle.variable("force", &fsrReading, INT);
//--------------------------------------------------------------------------------
// attaches the servo on the A3 pin to the servo object
myServo.attach( A3 );
//Register our Particle to control the servo
Particle.function("servo", servoControl);
// Keep a cloud variable for the current position
Particle.variable( "servoPos" , &servoPos , INT );
Particle.subscribe( "diot/2019/paired/yoolqc7001" , handleSharedEvent );
}
bool angry = false;
void loop() {
// Use analogRead to read the photo cell reading
// This gives us a value from 0 to 4095
fsrReading = analogRead(fsrPin);
if(fsrReading >= 3000 ){
Particle.publish( "diot/2019/paired/yoolqc7000", "data goes here" );
if(angry == false){
angry = true;
myServo.write( 0 );
}else{
angry = false;
myServo.write( 179 );
}
}
// Map this value into the PWM range (0-255)
// and store as the led brightness
ledBrightness = map(fsrReading, 0, 4095, 0, 255);
// fade the LED to the desired brightness
analogWrite(ledPin, ledBrightness);
// wait 1/10th of a second and then loop
delay(100);
}
int servoControl(String command){
// Convert
int newPos = command.toInt();
// Make sure it is in the right range
// And set the position
servoPos = constrain( newPos, 0 , 180);
// Set the servo
myServo.write( servoPos );
// done
return 1;
}
void handleSharedEvent(const char *event, const char *data){
if(angry == false){
angry = true;
myServo.write( 0 );
}else{
angry = false;
myServo.write( 179 );
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .