Dec 10, 2020

My version of a DIY bend / flex sensor


I had to help one of my students in a project involving flex sensors. After trying several tutorials I decided to make a custom version with which I'm quite satisfied.





Here you can whatch the build-up video:



If you take advantage of the internal pullup resistor of Arduino's pin you can skip the external voltage divider resistor. Otherwise I suggest you use a 30k Ohm one.




And this is the code:

int sensorPin   = A0;    
int numReadings = 64;
int msDelayFor  = 1;
int msDelayLoop = 100;
int minMedian = 1023; 

/******************************************
 *                  SETUP
 ******************************************/
void setup() {
  Serial.begin(115200);
  pinMode(sensorPin, INPUT_PULLUP);
}
/******************************************
 *                  LOOP
 ******************************************/
void loop() {
  float median = 0;
  for (int i=0; i < numReadings; i++) {
    median += analogRead(sensorPin);
    // for stabilization between analog readings
    delay(msDelayFor); 
  }
  median /= float(numReadings);
  if ( median < minMedian) minMedian = median;

  Serial.println(median-minMedian);
 
  int currentDelay = msDelayLoop - (msDelayFor * numReadings);
  if (currentDelay > 0) delay( currentDelay );

No comments:

Post a Comment