Sensor tester
I made a very simple circuit + program, basically a general purpose sensor testor. What it is made of:
a) 6 LEDs organized like a scale, connected to digital-out pins on the Arduino
b) a toggle switch for calibration mode, connected to digital-in
c) a sensor, connected to analog-in
What it does:
1) If the toggle switch is closed, then the program watches the sensor and records the minimum and maximum values.
2) If the toggle switch is open, then the program reads the sensor and uses the value to light a scale of 6 LEDs.
3) If the sensor has not been calibrated, then when the switch is open all the LEDs are flashed, as a warning.
I wanted to make a general purpose circuit that allows me to not worry about specific sensor values, and instead work with a min & max that are read from the sensor.
This turned out to be unexpectedly useful - I had been using a voltage regulator (unnecessary with Arduino) so all my voltages were only reaching 2.3v, not 5v. However, the calibration made this irrelevant.
I spent a bit of time wiring up two variable resistors with nice dial-like controls, that came out of an answering machine. One had range in the magnitude of 10K ohms, the other 1K ohms. I think they were meant to work together, one to set a gross value and the other to fine tune, with the resistances simply added together. In any case, I fit the appropriate resistors, now both of them return comparable ranges which are easy to scale in software.
Keep reading to see the source code for the sensor tester. Something's not quite right with the scale function - see if you can find it.
// Simple program to read a variable pot, and use it to control a scale of LEDs
// Physical Computing
// Gian Pablo Villamil
// September 24, 2006
//
// Program has a calibration mode when switch is closed - watches for min & max values
// When working, uses sensor reading to light up a scale of LEDs
// define the output LED start and end pins
#define startLEDPin 2
#define endLEDPin 7
// define calibration switch pin
#define calibrationPin 8
// define potentiometer input pin
#define potPin 0
// define output scale
// kludgy but works
#define outScaleMin 7
#define outScaleMax 0
// define interval for blinking
#define blinkTime 500
// variables
int potMin ; // calibrated minimum
int potMax ; // calibrated maximum
int potValue ; // pot value as read
int scaledPotValue ; // pot value as scaled
char calibrated = 0 ; // flag for calibration
int scale(int inputValue, int inputMin, int inputMax, int outputMin, int outputMax);
void calibrate () ;
void warning () ;
void echoReading () ;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Serial.println("Initialize input pin");
pinMode(calibrationPin, INPUT);
Serial.println("Initialize output pins");
for (int i=startLEDPin; i<= endLEDPin; i++) {
pinMode(i, OUTPUT);
}
Serial.println("Begin reading");
potMax = 0 ;
potMin = 1023 ;
calibrated = 0 ;
}
void loop() {
if (digitalRead(calibrationPin)) {
calibrate() ;
}
else
{
if (calibrated) {
echoReading();
}
else {
warning ();
}
}
}
int scale(int inputValue, int inputMin, int inputMax, int outputMin, int outputMax) {
return ((outputMax-outputMin)*(inputValue-inputMin)/(inputMax-inputMin)+outputMin);
}
void calibrate() {
Serial.println("Begin calibration");
Serial.println("Turn pot to maximum and minimum value");
potMax = 0 ;
potMin = 1023 ;
calibrated = 0;
while (digitalRead(calibrationPin)){
potValue = analogRead(potPin);
if (potValue > potMax) {
potMax = potValue;
};
if (potValue < potMin) {
potMin = potValue;
};
}
if (potMax > potMin) {
calibrated = 1;
Serial.print("Max = ");
Serial.print(potMax);
Serial.print(" Min =");
Serial.println(potMin);
}
else {
Serial.println("Not calibrated!");
}
}
void warning () {
if (millis() % blinkTime > (blinkTime / 2)) {
for (int i = startLEDPin; i<= endLEDPin; i++) {
digitalWrite(i,HIGH);
}
}
else {
for (int i = startLEDPin; i<= endLEDPin; i++) {
digitalWrite(i,LOW);
}
}
}
void echoReading () {
potValue = analogRead(potPin); // read the pot value
scaledPotValue = scale(potValue, potMin, potMax, outScaleMin, outScaleMax);
// Serial.print("Read = ");
// Serial.print(potValue); // print the pot value back to the debugger pane
// Serial.print(" Scaled = ");
// Serial.println(scaledPotValue);
delay(10); // wait 10 milliseconds before the next loop
for (int i= startLEDPin; i<=endLEDPin; i++) {
if (scaledPotValue >= i) {
digitalWrite(i,HIGH);
}
else {
digitalWrite(i,LOW);
}
}
}