Author Topic: IDE Sketch question: RGB LED  (Read 6340 times)

XtremeIN

  • Newbie
  • *
  • Posts: 21
IDE Sketch question: RGB LED
« on: March 20, 2013, 01:13:24 pm »
Hello all,
I have been working on an RGB LED controller. I thought I had it figured out, but I do not. I have a basic sketch written. The problem I am having is when P5, colorSlectPB, goes HIGH the LEDS are not changing color.  Pretty sure its my code.  Also is there a way to have the DigiSpark retain the value of a valaiable, colorSelectcount, after power loss?
 
Code: [Select]

 
int colorSelectPB = 5;
int redPin = 0;
int bluePin = 4;
int greenPin = 1;
boolean lastButton = LOW;
boolean currentButton = LOW;
int colorSelectcount = 0;
void setup()
{
  pinMode(colorSelectPB, INPUT);
  //Serial.begin(9600);
}
boolean debounce(boolean last)
{
  boolean current = digitalRead(colorSelectPB);
  if (last != current)
  {
    delay(5);
    current = digitalRead(colorSelectPB);
  }
  return current;
}
void loop()
{
  //Serial.println (colorSelectcount);
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    colorSelectcount++;
    lastButton = currentButton;
  }
  if (colorSelectcount == 1)  //RED
  {
    analogWrite (redPin, 255);
    analogWrite (bluePin, 0);
    analogWrite (greenPin, 0);
  }
  if (colorSelectcount == 2)  //BLUE
  {
    analogWrite (redPin, 0);
    analogWrite (bluePin, 255);
    analogWrite (greenPin, 0);
  }
  if (colorSelectcount == 3)  //GREEN
  {
    analogWrite (redPin, 0);
    analogWrite (bluePin, 0);
    analogWrite (greenPin, 255);
  }
  if (colorSelectcount == 4)  //ORANGE
  {
    analogWrite (redPin, 255);
    analogWrite (bluePin, 20);
    analogWrite (greenPin, 255);
  }
  if (colorSelectcount == 5)  //PURPLE
  {
    analogWrite (redPin, 102);
    analogWrite (bluePin, 0);
    analogWrite (greenPin, 102);
  }
  if (colorSelectcount == 6)  //AQUA
  {
    analogWrite (redPin, 0);
    analogWrite (bluePin, 255);
    analogWrite (greenPin, 255);
  }
  if (colorSelectcount == 7)  //YELLOW
  {
    analogWrite (redPin, 255);
    analogWrite (bluePin, 255);
    analogWrite (greenPin, 0);
  }
  if (colorSelectcount == 8)  //WHITE
  {
    analogWrite (redPin, 255);
    analogWrite (bluePin, 255);
    analogWrite (greenPin, 255);
  }
  if (colorSelectcount > 8) colorSelectcount = 1; 
  else
  {
    analogWrite (redPin, 0);
    analogWrite (bluePin, 0);
    analogWrite (greenPin, 0);
  }
}

Thank you,
Micheal

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: IDE Sketch question: RGB LED
« Reply #1 on: March 20, 2013, 04:58:03 pm »
You can store things through power loss using eeprom. #include <EEPROM.h> at the start of your program, then EEPROM.write(address, byte) and EEPROM.read(address) to get that byte back. The address can be anything between 0 and 511, and it works identically to on regular full sized Arduino.

Sergio

  • Newbie
  • *
  • Posts: 9
Re: IDE Sketch question: RGB LED
« Reply #2 on: March 20, 2013, 05:53:40 pm »
Hi,
Your last if() statement should be:
Code: [Select]

if (colorSelectcount > 8) {  //OFF
   colorSelectcount = 0;
   analogWrite (redPin, 0);
   analogWrite (bluePin, 0);
   analogWrite (greenPin, 0);
}
« Last Edit: March 20, 2013, 05:56:47 pm by Sergio »

XtremeIN

  • Newbie
  • *
  • Posts: 21
IDE Sketch question: RGB LED: Pin 3 issue
« Reply #3 on: March 25, 2013, 11:50:29 pm »
hello all,
I have been using the Arduino Example, Button to test my hardware. I have figured out that some how I fried Pin 5.  After replacing the board I now have Pin 5 and Pin 2 working correctly. Now my issue is I need Pin 3 for an Input also. I thought that since Pin 3 uses a pull up resistor all I needed to do was have the input go to ground via a switch. It stays HIGH. Please advise on what I need to do to get Pin 3 to change states.

Thanks,
Micheal

Code: [Select]

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  1;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Mark

  • Full Member
  • ***
  • Posts: 196
Re: IDE Sketch question: RGB LED
« Reply #4 on: March 26, 2013, 12:48:06 am »
A couple of thoughts ...
 
 
  • Is there any way to prove that the pin on the DS board is going low.?
  • Can you add a led with resistor across it.?
  • Does it work if you use a wire without the shield plugged in.? (assuming you're using a shield)
 
While not necessary, I 'd be adding the digitalwrite statement.
In theory it doesn't offer anything but enabling the pullup resistor, but who knows.
Code: [Select]
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);     
}

These may seem strange, but I've seen stranger things.
 
Mark

XtremeIN

  • Newbie
  • *
  • Posts: 21
Re: IDE Sketch question: RGB LED
« Reply #5 on: March 26, 2013, 01:28:27 am »
Thanks for the info Mark. I will give it a try in a few.  I have another question/issue.  Shocker, I know :).
If I run this
Code: [Select]
#define REDPIN 0
#define GREENPIN 1
#define BLUEPIN 4

void setup()
{
  pinMode(REDPIN, OUTPUT); 
  pinMode(GREENPIN, OUTPUT); 
  pinMode(BLUEPIN, OUTPUT);
}
void loop()
{
  analogWrite(REDPIN, 255);
  analogWrite(GREENPIN, 20);
  analogWrite(BLUEPIN, 0);   
}

My LEDS are a nice funky orange.
But, if I run this
Code: [Select]

#define redPin 0
#define greenPin 1
#define bluePin 4 
int colorSelectPB = 5;
boolean lastButton = LOW;
boolean currentButton = LOW;
int colorSelectcount = 0;
void setup()
{
  pinMode(colorSelectPB, INPUT);
}
boolean debounce(boolean last)
{
  boolean current = digitalRead(colorSelectPB);
  if (last != current)
  {
    delay(5);
    current = digitalRead(colorSelectPB);
  }
  return current;
}
void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    colorSelectcount = colorSelectcount + 1;
  }
  if (colorSelectcount == 1)  //RED
  {
    analogWrite (redPin, 255);
    analogWrite (greenPin, 0);
    analogWrite (bluePin, 0);
  }
  if (colorSelectcount == 2)  //GREEN
  {
    analogWrite (redPin, 0);
    analogWrite (greenPin, 255);
    analogWrite (bluePin, 0);
  }
  if (colorSelectcount == 3)  //BLUE
  {
    analogWrite (redPin, 0);
    analogWrite (greenPin, 0);
    analogWrite (bluePin, 255);
  }
  if (colorSelectcount == 4)  //ORANGE
  {
    analogWrite (redPin, 255);
    analogWrite (greenPin, 20);
    analogWrite (bluePin, 0);
  }
  if (colorSelectcount == 5)  //PURPLE
  {
    analogWrite (redPin, 255);
    analogWrite (greenPin, 0);
    analogWrite (bluePin, 255);
  }
  if (colorSelectcount == 6)  //AQUA
  {
    analogWrite (redPin, 0);
    analogWrite (greenPin, 255);
    analogWrite (bluePin, 255);
  }
  if (colorSelectcount == 7)  //YELLOW
  {
    analogWrite (redPin, 255);
    analogWrite (greenPin, 255);
    analogWrite (bluePin, 0);
  }
  if (colorSelectcount == 8)  //WHITE
  {
    analogWrite (redPin, 255);
    analogWrite (greenPin, 255);
    analogWrite (bluePin, 255);
  }
  lastButton = currentButton;
  if (colorSelectcount > 8) colorSelectcount = 1; 
  else
  {
    analogWrite (redPin, 0);
    analogWrite (bluePin, 0);
    analogWrite (greenPin, 0);
  }
}

when I get to the orange color the LEDS flicker.....
Any thought?

Thanks,
Micheal

Sergio

  • Newbie
  • *
  • Posts: 9
Re: IDE Sketch question: RGB LED
« Reply #6 on: March 26, 2013, 03:29:03 am »
That is because the logic in your last statement is not right.
When colorSelectcount ==1 the first statement turns it "orange".
The last statement then tests if colorSelectcount > 8, which is not, so it turns them off.
The loop repeats again and the first statement turns them on.
If you do not want the last button press to turn all leds off, as I posted it originally, just remove the else clause.

Mark

  • Full Member
  • ***
  • Posts: 196
Re: IDE Sketch question: RGB LED
« Reply #7 on: March 26, 2013, 03:42:36 am »
Michael

Why aren't you setting the Pinmode for the outputs as per your first code.?
Only other comment was rather than a bunch of 'if' statements you could use a 'switch case' statement.

I've never called a subroutine that way, so I've learned something today.
You could always drop a delay in somewhere to see where its going to, to cause the flickering.
Does the flickering also occur when its past the orange.?

I looked at the last statement and thought that every colour should flicker, hence the question for the rest of the colours.

can we presume you have fixed the earlier problem of not being able to detect Pin5 changing state.?

Mark

Sergio

  • Newbie
  • *
  • Posts: 9
Re: IDE Sketch question: RGB LED
« Reply #8 on: March 26, 2013, 04:47:45 am »
Please note that analogwrite 0 or 255 are special cases that translate to digitalwrite low and high by the analogwrite function. The value 20 in the orange case sets up a pwm on that pin.
The other values  blink in all cases, but it does so fast that you cannot see it. I think the addtional delay in setting up of timers to generate the pwm of 20 allows you to see the pins are actually flickering. Also, analogwrite generates a pinmode output automatically for the pin.

XtremeIN

  • Newbie
  • *
  • Posts: 21
getting closer!!!!!
« Reply #9 on: March 27, 2013, 12:01:12 am »
Thank you! I just got home and still have some issues to work through.  I will get to the suggestions as soon as I can. 
Sergio, thank you. I removed the ELSE statement and the Orange color no longer flickers.

Thanks,
Micheal

XtremeIN

  • Newbie
  • *
  • Posts: 21
Re: IDE Sketch question: RGB LED
« Reply #10 on: March 27, 2013, 02:22:45 am »
After a few beers, several hours, and some choice words I have worked through all of my issues.
Thank you!!
Next is figuring out the EEPROM code to remember two different values.....

Micheal

XtremeIN

  • Newbie
  • *
  • Posts: 21
Re: IDE Sketch question: RGB LED
« Reply #11 on: March 27, 2013, 10:57:02 pm »
Is there a way to use a potentiometer on P2 to change the value of FADESPEED?

Code: [Select]
    // color swirl! connect an RGB LED to the PWM pins as indicated
    // in the #defines
    // public domain, enjoy!
    #define REDPIN 0
    #define GREENPIN 1
    #define BLUEPIN 4
    #define FADESPEED 5 // make this higher to slow down
   
    void setup() {
    pinMode(REDPIN, OUTPUT);
    pinMode(GREENPIN, OUTPUT);
    pinMode(BLUEPIN, OUTPUT);
    }
    void loop() {
    int r, g, b;
    // fade from blue to violet
    for (r = 0; r < 256; r++) {
    analogWrite(REDPIN, r);
    delay(FADESPEED);
    }
    // fade from violet to red
    for (b = 255; b > 0; b--) {
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
    }
    // fade from red to yellow
    for (g = 0; g < 256; g++) {
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
    }
    // fade from yellow to green
    for (r = 255; r > 0; r--) {
    analogWrite(REDPIN, r);
    delay(FADESPEED);
    }
    // fade from green to teal
    for (b = 0; b < 256; b++) {
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
    }
    // fade from teal to blue
    for (g = 255; g > 0; g--) {
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
    }
    }

Thanks,
Micheal