Hardware Reference
In-Depth Information
The Code
There are three pieces of code for this project: the PHP
script, the Arduino sketch, and the Processing sketch. The
first two are relatively simple, and having them done and
tested makes the Processing sketch easier to understand.
Tack the ribbon down to the garment's inner lining about
every inch, so it's secure and won't fold over itself when
worn. Don't sew the battery in—you'll need to remove it
for recharging. You shouldn't need to sew the other com-
ponents in either, but Velcro on the back of the LilyPad
Arduino and in the lining of the band will secure it nicely.
The Arduino sketch listens for serial input and when a byte
arrives, it takes a reading, maps it to a voltage range, and
sends it out.
At this point, the circuit should be fully functional. Test it
with a wired connection before going Bluetooth. To test it,
attach a USB-to-Serial connector to the LilyPad Arduino
(which will take some creative cabling) and open the serial
connection in the Serial Monitor or another serial terminal
program at 115200bps. Put your hand on the contacts in
the pocket. Then send any byte, and you should get back
a sensor value. To see a change in the value, either work
up a sweat or lick your hand and put it on the contacts
again. When you know it works, remove the USB-to-Serial
line, and connect the Bluetooth Mate and the battery and
turn it on. Now connect from the Serial Monitor or a serial
terminal program via the Mate's serial port. This will work
like it did in Chapter 2 when you added the Bluetooth Mate
to Monski Pong.
The PHP script accepts a request string and looks for
one variable, called data . It appends everything from that
variable into an existing text file called datalog.txt. It returns
a basic HTML page with the data that the client sent.
The Processing sketch checks if it's connected to the
Arduino's Bluetooth Mate. If it's connected, it asks for a
reading once every 10 seconds. Once every two minutes, it
sends the accumulated readings to the PHP script. There
are also two buttons: one to get a reading and another to
send the reading to the server.
X
The Arduino sketch's
global constants include
the two pins to be used as voltage and
ground for the sensor.
Read It
/*
Galvanic Skin Response reader
Context: Arduino
*/
const int voltagePin = 11; // use pin 11 as voltage
const int groundPin = A1; // use pin A1 as ground
setup() initializes serial at 115200bps
(the default rate for the Bluetooth
Mate) and sets the voltage and ground
pins for the sensor appropriately.
void setup() {
// initialize serial:
Serial.begin(115200);
// set powerPin and groundPin as digital outputs:
pinMode(voltagePin, OUTPUT);
pinMode(groundPin, OUTPUT);
// set them high and low respectively:
digitalWrite(voltagePin, HIGH);
digitalWrite(groundPin, LOW);
}
loop() checks to see whether there's
been any serial input. When there is, it
reads the byte just to clear the serial
buffer, then takes a sensor reading,
maps it, and returns it.
Notice that the mapping is to a range
from 0 to 3.7V. That's because the
Lithium Polymer battery supplies the
microcontroller at 3.7V when fully
charged.
void loop() {
// if serial available, send average
if (Serial.available() > 0) {
int inByte = Serial.read();
int sensorReading = analogRead(A0);
float voltage = map(sensorReading, 0, 1023, 0,3.7);
Serial.println(voltage);
}
}
 
Search WWH ::




Custom Search