// FSR output controls speed of stepper motor
int minimum = 1023, maximum = 0; // determine min and max FSR value
int dirPin = 11, stepPin = 12, fsrPin = A0, fsrValue;
int fsrMin = 0, fsrMax = 1023; // replace with measured values
int waitMin = 2, waitMax = 100; // milliseconds
unsigned long timer, timeout;
void setup() {
Serial.begin(115200);
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
digitalWrite(dirPin, HIGH); // CW direction
}
void loop() {
findMinMax(); // find minimum and maximum of Force Sensitive Resistor
// readSensor(); // control Stepper Motor with FSR
}
void readSensor() {
fsrValue = analogRead(fsrPin);
timeout = map(fsrValue, fsrMin, fsrMax, waitMax, waitMin); // no force = max wait, max force = min wait
if (timeout < 100) { // stop motor if FSR does not sense force
if (millis() - timer > timeout) { // if timeout is exceeded
timer = millis(); // reset timer
stepMotor(); // motor one step
}
}
}
void stepMotor() {
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
void findMinMax() {
int fsr = analogRead(A0);
if (fsr < minimum)
minimum = fsr;
if (fsr > maximum)
maximum = fsr;
Serial.print("min ");
Serial.print(minimum);
Serial.print(" (reading ");
Serial.print(analogRead(A0));
Serial.print(") max ");
Serial.println(maximum);
delay(100);
}