<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jacek Spiewla &#187; Arduino</title>
	<atom:link href="http://jacekspiewla.com/blog/category/projects/arduino-projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://jacekspiewla.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 16 Jun 2011 13:22:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Arduino Hacking &#8211; RGB LED and Force Pressure Sensor</title>
		<link>http://jacekspiewla.com/blog/arduino-hacking-rgb-led-and-force-pressure-sensor/</link>
		<comments>http://jacekspiewla.com/blog/arduino-hacking-rgb-led-and-force-pressure-sensor/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 01:24:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://jacekspiewla.com/blog/?p=20</guid>
		<description><![CDATA[Here's a circuit for changing the color of an RGB LED based on the amount of pressure applied to a force pressure sensor. Materials: Arduino Board (I used a Duemilanove) Force Sensor Kit RGB LED 100Ω Resistor Arduino Code: /*  * This script does the following:  * 1. Gets analog sensor input from a touch [...]]]></description>
			<content:encoded><![CDATA[<p>Here's a circuit for changing the color of an RGB LED based on the amount of pressure applied to a force pressure sensor.</p>

<a href='http://jacekspiewla.com/blog/arduino-hacking-rgb-led-and-force-pressure-sensor/img_1900/' title='img_1900'><img width="150" height="150" src="http://jacekspiewla.com/blog/wp-content/uploads/2009/03/img_1900-150x150.jpg" class="attachment-thumbnail" alt="img_1900" title="img_1900" /></a>
<a href='http://jacekspiewla.com/blog/arduino-hacking-rgb-led-and-force-pressure-sensor/img_1901/' title='img_1901'><img width="150" height="150" src="http://jacekspiewla.com/blog/wp-content/uploads/2009/03/img_1901-150x150.jpg" class="attachment-thumbnail" alt="img_1901" title="img_1901" /></a>
<a href='http://jacekspiewla.com/blog/arduino-hacking-rgb-led-and-force-pressure-sensor/img_1903/' title='img_1903'><img width="150" height="150" src="http://jacekspiewla.com/blog/wp-content/uploads/2009/03/img_1903-150x150.jpg" class="attachment-thumbnail" alt="img_1903" title="img_1903" /></a>

<p><span style="text-decoration: underline;"><strong>Materials:</strong></span></p>
<ol>
<li>Arduino Board (I used a Duemilanove)</li>
<li><a href="http://www.trossenrobotics.com/1-5i-Force-Sensing-Resistor-Kit.aspx" target="_blank">Force Sensor Kit</a></li>
<li><a href="http://www.trossenrobotics.com/triple-output-led-rgb.aspx" target="_blank">RGB LED</a></li>
<li>100Ω Resistor</li>
</ol>
<p><span style="text-decoration: underline;"><strong>Arduino Code:</strong></span></p>
<pre>/*
 * This script does the following:
 * 1. Gets analog sensor input from a touch sensor
 * 2. Processes sensor data in a four state machine
 * 3. Adjusts an RGB LED color based on toouch sensor pressure using PWM
 *
 * Written by: Jacek Spiewla (jacekspi@umich.edu)
 * Date Created:   2/23/2009
 * Date Modified:  3/17/2009
 */

//Setup all pins
int sensPin = 2;    // set the touch sensor (analog) input pin on Arduino
int redPin = 11;    // set the PWM (analog) output pin on Arduino controlling the red anode
int grnPin = 9;        // set the PWM (analog) output pin on Arduino controlling the green anode
int bluPin = 10;    // set the PWM (analog) output pin on Arduino controlling the blue anode

//Setup all initial values
int val = 0;            // initial value for touch sensor input
int state = 1;        // initial machine state

//Initialize variables
int redVal;         // pulse width variable for red anode
int grnVal;         // pulse width variable for green anode
int bluVal;        // pulse width variable for blue anode

void setup() {
  pinMode(redPin, OUTPUT);   // set the LED pins as output
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  val = analogRead(sensPin);           // read touch sensor values
  Serial.println(val);

  if (state == 1) {sleep();}        // turn off LED
  else if (state == 2) {redIn();}    // fade in red color
  else if (state == 3) {grnIn();}    // fade in green color
  else if (state == 4) {bluIn();}    // fade in blue color
}

void sleep() {
  alloff();                                  // turn off LED

  if (val &gt; 20 &amp;&amp; val &lt;= 500)  {state = 2;}   // test for low pressure
  if (val &gt; 500 &amp;&amp; val &lt;= 850) {state = 3;}   // test for medium pressure
  if (val &gt; 850) {state = 4;}              // test for high pressure
} 

void redIn() {                  // function to fade in red color and transition to other states
  grnVal = 0;
  analogWrite(grnPin, grnVal);  // turn off green in case the last state was green

  if (redVal == 255) {            // if red is at maximum, continue to light it up
    analogWrite(redPin, redVal);
  } else {                    // else fade in the red color
    redVal ++;
    analogWrite(redPin, redVal);
  }

  if (val &lt; 20) {state = 1;}    // turn off the LED if no pressure detected
  if (val &gt; 500) {state = 3;}   // fade in green if pressure has increased
}

void grnIn() {                  // function to fade in green color and transition to other states
  redVal = 0;
  bluVal = 0;
  analogWrite(redPin, redVal);    // turn off red in case the last state was blue
  analogWrite(bluPin, bluVal);  // turn off blue in case the last state was blue

  if (grnVal == 255) {            // if green is at maximum, continue to light it up
    analogWrite(grnPin, grnVal);
  } else {                    // else fade in the green color
    grnVal ++;
    analogWrite(grnPin, grnVal);
  }

  if (val &lt;= 500) {state = 2;}    // fade in red if pressure has decrease
  if (val &gt;= 850) {state = 4;}    // fade in blue if pressure has increased
}

void bluIn() {                  // function to fade in blue color and transition to other states
  grnVal = 0;
  analogWrite(grnPin, grnVal);    // turn off green in case the last state was green

  if (bluVal == 255) {            // if blue is at maximum, continue to light it up
    analogWrite(bluPin, bluVal);
  } else {                    // else fade in the blue color
    bluVal ++;
    analogWrite(bluPin, bluVal);
  }

  if (val &lt;= 850) {state = 3;}    // fade in green if pressure has decreased
}

void alloff() { // function to turn off the LED
  redVal = 0;
  grnVal = 0;
  bluVal = 0;

  analogWrite(redPin,  redVal);
  analogWrite(grnPin,  grnVal);
  analogWrite(bluPin,  bluVal);
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://jacekspiewla.com/blog/arduino-hacking-rgb-led-and-force-pressure-sensor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

