In this tutorial, we will build a stress meter that uses the galvanic response sensor to display the user's stress level on a dial using a mini servo.
BOM
- Arduino Uno R3
- Galvanic Response Sensor
- Mini Servo
- Breadboard
- DuPont connector cables
Wiring
Begin by connecting all of the components following the diagram above. Take extra care to connect the 5V and GND connectors properly.
Code
#include <Servo.h>
#define SERVO_MIN 0
#define SERVO_MAX 179
#define GALVANIC_MAX 240
#define GALVANIC_MIN 0
#define SERVO_PIN 3
#define GALVANIC_PIN A0
#define BUF_SIZE 100
long buf[BUF_SIZE];
int bufIndex;
long avg;
Servo servo;
void setup() {
Serial.begin(9600);
pinMode(SERVO_PIN, OUTPUT);
pinMode(GALVANIC_PIN, INPUT);
servo.attach(SERVO_PIN);
}
void loop() {
int galvanicReading = analogRead(GALVANIC_PIN);
Serial.print("galvanic = ");
Serial.print(galvanicReading);
buf[bufIndex] = galvanicReading;
bufIndex++;
bufIndex %= BUF_SIZE;
avg = 0;
for(int i = 0; i < BUF_SIZE; i++) {
avg += buf[i];
}
avg /= BUF_SIZE;
Serial.print("\t avg = ");
Serial.println(avg);
int servoPos = map(avg, GALVANIC_MIN, GALVANIC_MAX, SERVO_MAX, SERVO_MIN);
servo.write(servoPos);
delay(10);
}