// testing switching pinMode when using a contact microphone
// if I indicate pinMode INPUT the values from analogRead stay high.
// actually if I only tap the mic slightly things are okay but if I
// give it a good tap it stays around 893 or something high
int incoming = 0; // for incoming serial data
int previousIncoming;
int reading;
int ledPin = 13;
int knockSensor = A1;
int n0;
int var = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// This code uses serial input from the serial window
// 1 switched pinMode to INPUT_PULLUP
// 2 switches pinMode to INPUT
if (Serial.available() > 0) {
reading = Serial.read();
if (reading > 20){
incoming = reading;
}
}
// default position -- no pinMode specified
if (incoming == 0){
digitalWrite(ledPin, LOW);
n0 = analogRead(knockSensor);
Serial.println(n0);
}
// Serial input 1 -- pinMode INPUT_DISABLE
if (incoming == 49){
if (incoming != previousIncoming){
digitalWrite(ledPin, LOW);
// pinMode(knockSensor, INPUT); // this didn't work
pinMode(knockSensor, INPUT_DISABLE); // this works!
previousIncoming = incoming;
var = 0;
}
var++;
if (var > 1){ // filter out the first reading when the pinMode is changed because I was still getting a high number
n0 = analogRead(knockSensor);
}
}
// Serial input 2 -- pinMode INPUT_PULLUP as though I want to use this pin with a button instead of a contact mic now
if (incoming == 50){
if (incoming != previousIncoming){
digitalWrite(ledPin, HIGH);
previousIncoming = incoming;
}
pinMode(knockSensor, INPUT_PULLUP);
n0 = digitalRead(knockSensor);
}
Serial.println(n0);
delay(100);
}