Soft Potentiometer Sketch

// SR will be .25 seconds

const int sr = 250; // sample rate in ms??
// const int setdist = 256; // Distance to travel per sample
const int le = 40; // Low end of travel
const int he = 100; // High end of travel
int dir = 1; // 1 is clockwise -1 is counterclockwise
const int SENSOR_PIN = 0; // Pin for ribbon
const int LED_PIN = 12; // LED pin location
// const int forgiveness = 2; // 2 samples forgiveness
bool flowing = false;
bool prevFlow = false;
int prevRead = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode( LED_PIN, OUTPUT );
}

void loop() {
// put your main code here, to run repeatedly:
int sensorVal = analogRead( SENSOR_PIN );
int sensDelta = sensorVal – prevRead;

prevRead = sensorVal;

if ( sensDelta != 0 ) {
//Serial.print(“Val: “);
//Serial.println( sensorVal );
Serial.print(“Delta: “);
Serial.println( sensDelta );

if ( ( sensDelta > le ) && ( sensDelta < he ) ) {
flowing = true;
Serial.println( “Flow detected” );
}

if ( !( ( prevFlow == true ) || ( flowing == true ) ) ) {
Serial.println( “oops” );
digitalWrite( LED_PIN, LOW );
}
else {
digitalWrite( LED_PIN, HIGH );
}

prevFlow = flowing;
flowing = false;
}
else {
digitalWrite( LED_PIN, LOW );
}

delay( sr );
}

Leave a Reply

Your email address will not be published. Required fields are marked *