Pages: [1]   Go Down
Author Topic: RGB Code not working.  (Read 919 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, Im wanting to change the colour of an RGB LED. To do this I am using the FSR (Force sensitive resistor), I have the arduino reading the FSR pressure but its not changing the colour of the RGB.
Any help would be appreciated.

Thanks


Code:
/*
 * 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 > 20 && val <= 500)  {state = 2;}   // test for low pressure
  if (val > 500 && val <= 850) {state = 3;}   // test for medium pressure
  if (val > 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 < 20) {state = 1;}    // turn off the LED if no pressure detected
  if (val > 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 <= 500) {state = 2;}    // fade in red if pressure has decrease
  if (val >= 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 <= 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);
}

Moderator edit: Please use [code] [/code] tags when posting code.
« Last Edit: April 16, 2012, 08:34:04 am by AWOL » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your setting of state is inconsistent.

sleep() sets state to 1, 2, or 3.
redIn() might set it to 1 or 3.
grnIn() might set it to 2 or 4.
bluIn() might set it to 3.

Serial.print() or state in each pass through loop will be necessary. Not jerking state around in all the functions will probably be necessary, too.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26285
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

NEED CODE

Thanks
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't you already have a thread for this? http://arduino.cc/forum/index.php/topic,101599.0.html
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26285
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Topics merged:
@OP: Please don't post the same question multiple times - it wastes time.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's not necessary to shout 'NEED HELP'. You probably wouldn't be posting if you didn't need help. It's also not really a good idea to keep posting the same question all over the forums.
« Last Edit: April 17, 2012, 06:50:05 am by dxw00d » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26285
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
t's also not really a good idea to keep posting the same question all over the forums.
It's actually a really bad idea.
@OP: Don't do it - no more warnings.
« Last Edit: April 17, 2012, 07:20:52 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: