Pages: 1 2 3 [4]   Go Down
Author Topic: Help on random led coloring?  (Read 4582 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  testRGB();
}

void testRGB(){
  led1.setRGB(255,0,0);
  led1.updateLED();
  delay(500);
  led1.setRGB(0,255,0);
  led1.updateLED();
  delay(500);
  led1.setRGB(0,0,255);
  led1.updateLED();
  delay(2000);
  return;
  }

   led1.setRandomHue();
  led1.setSat(255);
  led1.setVal(255);
  led1.updateLED();
  delay(3000);
  led1.setRandomColor();
  led1.updateLED();
  delay(1000);
 
void loop()
{


}

It says " error: expected constructor, destructor, or type conversion before '.' token" right here:

[glow]led1.setRandomHue();[/glow]



Logged

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

You can't just paste code between function definitions like that.

Code:
void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));

  led1.setRandomHue();
  led1.updateLED();
}

void loop()
{
}
(untested)
« Last Edit: September 24, 2009, 02:09:02 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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yours worked, only every color it showed was white, or at least all the values were at zero the whole time. I got this:

Code:
void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));

  led1.setRandomHue();
  led1.setSat(255);
  led1.setVal(255);
  led1.updateLED();
  delay(3000);
  led1.setRandomColor();
  led1.updateLED();

}

void loop()
{
}
 

but how do I make it so that it doesn't flash, wait for a second or more, and then show the color? I just want it to show the color
« Last Edit: September 24, 2009, 03:26:21 pm by ikestarm » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure I understood what you want but here's my attempt to help you:

Seems like you just need to remove the Delay.

Have fun
Logged

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

Quote
yours worked, only every color it showed was white
It wasn't mine, it was yours, with the code in proper functions.
As I said, untested - I don't have your hardware, I was just working on the code as you posted it.
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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it doesn't matter whose it was I was just referring to what you posted
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are these two the same?:

Code:
void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));

 led1.setRandomHue();
  led1.setSat(255);
  led1.setVal(255);
 
  led1.setRandomColor();
  led1.updateLED();
  
}

void loop()
{
}


Code:
void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));

 
  led1.setRandomColor();
  led1.updateLED();
  
}

void loop()
{
}
 

And I know it's probably inevitable, but why are there occaisionally 3 of the same colors in a row?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int buttonstate;
int val;
int val2;
int switchpin = 5;

int redPin = 3;
int bluePin = 5;
int greenPin = 6;

int redIn = 2;
int greenIn = 3;
int blueIn = 4;

int redVal;
int greenVal;
int blueVal;

#ifndef RGBCommonLED_H
#define RGBCommonLED_H

#include "WProgram.h"


class RGBCommonLED {
public:
  RGBCommonLED(int rpin, int gpin, int bpin);
  ~RGBCommonLED();

  void setRGB(byte r, byte g, byte b);
  void setHSV(byte h, byte s, byte v);
  void setColor(unsigned long v);
  void setHue(byte level);
  void setSat(byte level);
  void setVal(byte level);

  void adjHue(int level);
  void adjSat(int level);
  void adjVal(int level);

  void setRandomColor();
  void setRandomHue();
  void setRandomSat();
  void setRandomVal();

  void setHSVLevel(int section, byte level);
  void adjHSVLevel(int section, int level);
  void setRandomHSVLevel(int section);

  void updateLED();

  void HSVToRGB( unsigned int inHue, unsigned int inSaturation, unsigned int inValue,
    unsigned int *oR, unsigned int *oG, unsigned int *oB );

  void RGBToHSV( unsigned int inRed, unsigned int inGreen, unsigned int inBlue,
    unsigned int *oH, unsigned int *oS, unsigned int *oV );


  void updateHSVFromRGB();


  int pins[3];

  int rgb[3];

  int hsv[3];

private:
 
  boolean hsvSet;
  void setRGBFromHSV();
  void setHSVFromRGB();
};

#endif



RGBCommonLED::RGBCommonLED(int rpin, int gpin, int bpin){
  pins[0] = rpin;
  pins[1] = gpin;
  pins[2] = bpin;
  hsvSet = false;
}


RGBCommonLED::~RGBCommonLED(){
}

void RGBCommonLED::setColor(unsigned long v)
{
  this->setRGB((v & 255),((v >> 8) & 255),(v >> 16) & 255);
}


void RGBCommonLED::setRGB(byte r, byte g, byte b){
  rgb[0] = r;
  rgb[1] = g;
  rgb[2] = b;
  hsvSet = false;
}

void RGBCommonLED::setHSV(byte h, byte s, byte v){
  hsv[0] = h;
  hsv[1] = s;
  hsv[2] = v;
  this->setRGBFromHSV();
  hsvSet = true;
}


void RGBCommonLED::setHSVLevel(int section, byte level){
  updateHSVFromRGB();
  hsv[section] = level;
  this->setRGBFromHSV();
}

void RGBCommonLED::setHue(byte level){
  this->setHSVLevel(0,level);
}

void RGBCommonLED::setSat(byte level){
  this->setHSVLevel(1,level);
}

void RGBCommonLED::setVal(byte level){
  this->setHSVLevel(2,level);
}

void RGBCommonLED::adjHSVLevel(int section, int level){
  updateHSVFromRGB();
  int th = hsv[section] + level;

  if( th < 0 ){
    th = 255 + th;
  }
  else if( th > 255 ) {
    th = 255 - th;
  }

  th = constrain(th,0,255);
  hsv[section] = th;
  this->setRGBFromHSV();
}

void RGBCommonLED::adjHue(int level){
  this->adjHSVLevel(0,level);
}

void RGBCommonLED::adjSat(int level){
  this->adjHSVLevel(1,level);
}

void RGBCommonLED::adjVal(int level){
  this->adjHSVLevel(2,level);
}



void RGBCommonLED::RGBToHSV( unsigned int inRed, unsigned int inGreen, unsigned int inBlue,
unsigned int *oH, unsigned int *oS, unsigned int *oV )
{
  double vals[3];
  unsigned char maxc=0, minc=0;
  double hue, sat, val;

  vals[0]=inRed;
  vals[1]=inGreen;
  vals[2]=inBlue;
  //red is set as maximum and minimum
  if(vals[1]>vals[maxc]) maxc=1;
  if(vals[2]>vals[maxc]) maxc=2;
  if(vals[1]<vals[minc]) minc=1;
  if(vals[2]<vals[minc]) minc=2;
  val = vals[maxc];
  if(vals[maxc]==0)
    sat = hue = 0;
  else
  {
    sat=255*(1-(vals[minc]/vals[maxc]));
    hue = 60 * ((maxc*2) + (vals[(maxc+1)%3] - vals[(maxc+2)%3])/(vals[maxc] - vals[minc]));
  }
  if(hue < 0) hue += 360; //corrector for hues in -60 to 0 range
  *oH = hue; //map(hue,0,360,0,255);
  *oS = sat;
  *oV = val;
}

void RGBCommonLED::HSVToRGB( unsigned int inHue, unsigned int inSaturation, unsigned int inValue,
unsigned int *oR, unsigned int *oG, unsigned int *oB )
{
  if( inSaturation == 0 )
  {
    // achromatic (grey)
    *oR = *oG = *oB = inValue;
  }
  else
  {
    unsigned int scaledHue = (inHue * 6);
    unsigned int sector = scaledHue >> 8; // sector 0 to 5 around the color wheel
    unsigned int offsetInSector = scaledHue - (sector << 8);      // position within the sector
    unsigned int p = (inValue * ( 255 - inSaturation )) >> 8;
    unsigned int q = (inValue * ( 255 - ((inSaturation * offsetInSector) >> 8) )) >> 8;
    unsigned int t = (inValue * ( 255 - ((inSaturation * ( 255 - offsetInSector )) >> 8) )) >> 8;

    switch( sector ) {
    case 0:
      *oR = inValue;
      *oG = t;
      *oB = p;
      break;
    case 1:
      *oR = q;
      *oG = inValue;
      *oB = p;
      break;
    case 2:
      *oR = p;
      *oG = inValue;
      *oB = t;
      break;
    case 3:
      *oR = p;
      *oG = q;
      *oB = inValue;
      break;
    case 4:
      *oR = t;
      *oG = p;
      *oB = inValue;
      break;
    default:            // case 5:
      *oR = inValue;
      *oG = p;
      *oB = q;
      break;
    }
  }
}

void RGBCommonLED::setRandomColor(){
  this->setColor((unsigned long)random(0x01000000));
}

void RGBCommonLED::setRandomHue(){
  this->setRandomHSVLevel(0);
}
void RGBCommonLED::setRandomSat(){
  this->setRandomHSVLevel(1);
}
void RGBCommonLED::setRandomVal(){
  this->setRandomHSVLevel(2);
}

void RGBCommonLED::setRandomHSVLevel(int section){
  this->setHSVLevel(section, (unsigned int)random(0x0100));
}


void RGBCommonLED::updateHSVFromRGB(){
  this->setHSVFromRGB();
  hsvSet = true;
}
[glow]void RGBCommonLED::updateLED(){
  analogWrite(this->pins[0], rgb[0]);
  analogWrite(this->pins[1], rgb[1]);
  analogWrite(this->pins[2], rgb[2]);
  
  Serial.print(" R - ");
Serial.print(rgb[0] , DEC);
Serial.print(" G - ");
Serial.print(rgb[1] , DEC);
Serial.print(" B - ");
Serial.println(rgb[2] , DEC);
}[/glow]

void RGBCommonLED::setRGBFromHSV(){
  unsigned int h = hsv[0];
  unsigned int s = hsv[1];
  unsigned int v = hsv[2];
  unsigned int r = 0;
  unsigned int g = 0;
  unsigned int b = 0;
  HSVToRGB(h,s,v,&r,&g,&b);
  this->setRGB(r,g,b);
}

void RGBCommonLED::setHSVFromRGB(){
  unsigned int r = rgb[0];
  unsigned int g = rgb[1];
  unsigned int b = rgb[2];
  unsigned int h;
  unsigned int s;
  unsigned int v;

  this->RGBToHSV(r,g,b,&h,&s,&v);
  hsv[0] = map(h,0,360,0,255);
  hsv[1] = s;
  hsv[2] = v;
  hsvSet = true;
  
}

RGBCommonLED led1(10,11,9);



void setup()
{
  pinMode(switchpin, INPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));

  led1.setRandomHue();
  led1.setSat(255);
  led1.setVal(255);
  led1.setRandomColor();
  led1.updateLED();
  
  buttonstate = digitalRead(switchpin);
  

}

void loop()
{
   redVal = analogRead(redIn);
  greenVal = analogRead(greenIn);
  blueVal = analogRead(blueIn);
  
  redVal = map(redVal, 0, 1023, 0, 255);
  greenVal = map(greenVal, 0, 1023, 0, 255);
  blueVal = map(blueVal, 0, 1023, 0, 255);

  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin, blueVal);
  
    val = digitalRead(switchpin);
  delay(10);
  val2 = digitalRead(switchpin);
   if(val == val2) {
     if(val != buttonstate) {
       if(val == LOW) {
        [glow] if(rgb[0] >= redPin) {[/glow]
         Serial.print("red is less");

  
}
 

Do I need to make the first highlighted part global so that it can be used for the second part that is highlighted? Because it says rgb[0] was not defined in scope for the second highlighted part. If so, how do you make it global?
Logged

Pages: 1 2 3 [4]   Go Up
Jump to: