Step 5Writing the Code
Once you have all your sensors in the jacket you must write the code to get the LEDs to fade according to the temperature of the body.
1. Download the Lilypad program from
http://www.arduino.cc/en/Main/ArduinoBoardLilyPad
Then you will be able to program your Lilypad.
2. Plug in your Lilypad to the computer and make sure the settings in the Arduino program are selected for the right board. It should be the Lilypad Aurdino with ATmega328.
3. Copy and paste this code into the program:
int ledPin = 13; // LED is connected to digital pin 13
int sensorPin0 = 0; // temperature sensor is connected to analog pin 0
int sensorValue0; // variable to store the value coming from sensor a0
int sensorPin1 = 1; // temperature sensor is connected to analog pin 1
int sensorValue1; // variable to store the value coming from sensor a1
int sensorPin2 = 2; // temperature sensor is connected to analog pin a2
int sensorValue2; // variable to store the value coming from sensor a2
int sensorPin3 = 3; // temperature sensor is connected to analog pin 3
int sensorValue3; // variable to store the value coming from sensor a3
int sensorPin4 = 4; // temperature sensor is connected to analog pin 4
int sensorValue4; // variable to store the value coming from sensor a4
int sensorPin5 = 5; // temperature sensor is connected to analog pin 5
int sensorValue5; // variable to store the value coming from sensor a5
// Output
int bluePin = 10; // Blue LED, connected to digital pin 10
int redPin = 11; // Red LED, connected to digital pin 11
// Program variables
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 0; // Initial values are Red full, Green and Blue off
int blueVal = 0;
int wait = 10; // 50ms (.05 second) delay; shorten for faster fades
void setup()
{
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); // initialize the serial port
digitalWrite(ledPin, HIGH); // turn the LED on
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(bluePin, OUTPUT);
}
void loop() // run over and over again
{
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println("Values");
sensorValue0 = analogRead(sensorPin0); // read the value from the sensor
Serial.println(sensorValue0); // send that value to the computer
sensorValue1 = analogRead(sensorPin1); // read the value from the sensor
Serial.println(sensorValue1); // send that value to the computer
sensorValue2 = analogRead(sensorPin2); // read the value from the sensor
Serial.println(sensorValue2); // send that value to the computer
sensorValue3 = analogRead(sensorPin3); // read the value from the sensor
Serial.println(sensorValue3); // send that value to the computer
sensorValue4 = analogRead(sensorPin4); // read the value from the sensor
Serial.println(sensorValue4); // send that value to the computer
sensorValue5 = analogRead(sensorPin5); // read the value from the sensor
Serial.println(sensorValue5); // send that value to the computer
int sensorAverage = (sensorValue0+sensorValue1+sensorValue2+sensorValue3+sensorValue4+sensorValue5)/6; // average the sensor values
Serial.println("Sensor Average:"); // send that value to the computer
Serial.println(sensorAverage); // send that value to the computer
if (sensorAverage < 175) // Cold phase of fades
{
if (redVal < 175) redVal +=5; // Red down
if (blueVal > 0) blueVal -=5; // Blue up
}
else if (sensorAverage > 175) // Warm phase of fades
{
if (redVal > 0) redVal -=5; // Red up
if (blueVal < 175) blueVal +=5; // Blue down
}
Serial.println("check color value:");
Serial.println(redVal);
Serial.println(blueVal);
analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(bluePin, blueVal);
delay(1000); // delay for 1 second
}
4. Once you have the code, things get tricky... You will need to import the code to the Lilypad. Notice the UPLOAD button on the top of the program and the little button on the Lilypad. You will need to press down the button on the Lilypad. It should start blinking red and green. Soon after press the upload button and see if it successfully uploads.
*This may take a few tries: it is quite touchy.
5. Once it successfully uploads, you can click on the serial monitor and be able to read the temperature sensor readings.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|


























































