Last active
October 7, 2018 22:16
-
-
Save cryptobabel/9817ea601005efae6f47db0696b1167c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo myservo; | |
//Constants | |
const int pResistor = A0; // Force resistor at Arduino analog pin A0 | |
const int ledPin=9; // Led pin at Arduino pin 9 | |
int pos = 0; // variable to store the servo position | |
//Variables | |
int value; // Store value from forceresistor (0-1000) | |
int input; // Serial monitor input | |
void setup(){ | |
Serial.begin(9600); // init serial | |
myservo.attach(9); // attach to our servo | |
pinMode(LED_BUILTIN, OUTPUT); // init led | |
pinMode(pResistor, INPUT); // init resistor | |
} | |
void loop(){ | |
value = analogRead(pResistor); // read resistor value | |
if (Serial.available() > 0) { // If a command was send to the serial monitor | |
input = Serial.parseInt(); // then read the value and parse it to an integer | |
myservo.write(input); // write the the parsed serial input as our servo position | |
Serial.println(input); // print what we parsed from the serial monitor | |
delay (250); // a small delay before exiting if statement | |
} else { | |
// Nothing is being sent to the serial monitor, process movement based on sensors | |
processSensor(value); | |
} | |
delay(200); //Small delay | |
} | |
void processSensor(int fsrReading) { | |
Serial.println("Analog reading = "); | |
Serial.println(fsrReading); // the raw analog reading | |
// We'll have a few threshholds, qualitatively determined | |
if (fsrReading < 500) { // Only base pressure exists | |
Serial.println(" - No pressure"); | |
} else { // Something's on our sensor! | |
Serial.println(" - Big squeeze"); | |
servoDance(); // make the servo's dance | |
} | |
} | |
void servoDance() { | |
// Set various positions to move how we'd like | |
myservo.write(160); | |
delay (100); | |
myservo.write(90); | |
delay (250); | |
myservo.write(170); | |
delay (100); | |
myservo.write(0); | |
delay (250); | |
myservo.write(90); | |
delay (100); | |
myservo.write(120); | |
delay (250); | |
myservo.write(160); | |
delay (100); | |
myservo.write(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment