Hardware Reference
In-Depth Information
Once you've got the
sensor panel together
and connected to the microcontroller,
run this code on the Arduino board to
test the sensors.
Test It
/*
Analog sensor reader
Context: Arduino
Reads an analog input on Analog in 0, prints the result
as an ASCII-formatted decimal value.
To see the results, open the Serial
Monitor at 9600 bits per second. Now,
position the cat on the panel and note
the number change. This can be tricky,
as cats are difficult to command. You
may want to put some cat treats or
catnip on the pad to encourage the cat
to stay there. When you're satisfied
that the system works and that you can
see a significant change in the value
when the cat sits on the panel, you're
ready to move on to the next step.
Connections:
FSR analog sensors on Analog in 0
*/
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read analog input:
int sensorValue = analogRead(A0);
// send analog value out in ASCII decimal format:
Serial.println(sensorValue, DEC);
}
Next, send the
sensor readings
serially to Processing, which will send
an email and trigger the camera to take
a picture when the cat is on the mat.
This sketch will look familiar to you
because it's similar to the one you used
to read the sensor values for Monski
Pong in Chapter 2.
Connect It
/*
Serial String Reader
Context: Processing
Reads in a string of characters until it gets a linefeed (ASCII 10).
Then converts the string into a number.
*/
import processing.serial.*;
Serial myPort; // the serial port
float sensorValue = 0; // the value from the sensor
float xPos = 0; // horizontal position of the graph
void setup() {
size(400,300);
// list all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my Mac is always my
// Arduino, so I open Serial.list()[0]. Open whatever port you're using
// (the output of Serial.list() can help; they are listed in order
// starting with the one that corresponds to [0]).
myPort = new Serial(this, Serial.list()[0], 9600);
ยป
// read bytes into a buffer until you get a newline (ASCII 10):
 
Search WWH ::




Custom Search