Pages: [1]   Go Down
Author Topic: It WONT STOOOP!  (Read 1299 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

Im trying to get the RGB LED strip to light up to a certain colour when a press button 1.

Then it should fade out

And the it should wait for me to push the button again

But what happens is ... I push the button it comes on! And doesnt turn off and keeps cycling though the colours!




Code:
const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;

int buttonState1 = 0;

byte redPwr = 0;
byte greenPwr = 0;
byte bluePwr = 0;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);


  pinMode(buttonPin1, INPUT);
  buttonState1 = digitalRead(buttonPin1);

  // serial for debugging purposes only
  Serial.begin(9600);
}
void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW){
    redPwr = 255;
    bluePwr = 125;
    analogWrite(ledPinR, redPwr);
    greenPwr = 100;
    analogWrite(ledPinG, greenPwr);
    analogWrite(ledPinB, bluePwr);

    lightAndFade3();
  }
  else {
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinG, LOW);
digitalWrite(ledPinB, LOW);
  }

}


void lightAndFade3()
{
  // fade in from min to max in increments of 5 points:
  for(int fadeValueR = 255 ; fadeValueR <= 255; fadeValueR +=5)
    for(int fadeValueG = 125 ; fadeValueG <= 125; fadeValueG +=5)
      for(int fadeValueB = 100 ; fadeValueB <= 100; fadeValueB +=5) {
        // sets the value (range from 0 to 255):
        analogWrite(ledPinR, fadeValueR);
        analogWrite(ledPinG, fadeValueG);
        analogWrite(ledPinB, fadeValueB);
        delay(30);
      }



  // fade out from max to min in increments of 5 points:
  for(int fadeValueR = 255  ; fadeValueR >= 0; fadeValueR -=5)
    for(int fadeValueG = 125  ; fadeValueG >= 0; fadeValueG -=5)
      for(int fadeValueB = 100  ; fadeValueB >= 0; fadeValueB -=5){

        // sets the value (range from 0 to 255):
        analogWrite(ledPinR, fadeValueR);
        analogWrite(ledPinG, fadeValueG);
        analogWrite(ledPinB, fadeValueB);
        // wait for 30 milliseconds to see the dimming effect
        delay(30);
      }
}


Any suggestions!
smiley
Please!
« Last Edit: January 24, 2010, 09:28:08 pm by Victor_Q » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any suggestions!

You've got "Serial" initialised - why don't you add some debug prints?

Is your pushbutton maybe latching?

Maybe check some of those "for" loops?
« Last Edit: January 24, 2010, 05:12:10 pm by GrooveFlotilla » Logged

Per Arduino ad Astra

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

Ok i re wrote the code a bit
and now it is just a solid colour when i push the button.


It give me the colour i want, and lights up which is good...
But im just wanting it to fade out !

 smiley-sad
Code:
const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;

int buttonState1 = 0;

byte redPwr = 0;
byte greenPwr = 0;
byte bluePwr = 0;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);


  pinMode(buttonPin1, INPUT);
  buttonState1 = digitalRead(buttonPin1);


}
void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW){
    redPwr = 246;
    greenPwr = 235;
    analogWrite(ledPinR, redPwr);
    bluePwr = 0;
    analogWrite(ledPinG, greenPwr);
    analogWrite(ledPinB, bluePwr);

    light();
    delay(10);
    fade();
  
  }
  else {
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinG, LOW);
digitalWrite(ledPinB, LOW);
  }

}


void light()
{
  // fade in from min to max in increments of 5 points:
   for(int fadeValueR = 246 ; fadeValueR <= 246; fadeValueR +=0)
  for(int fadeValueG = 235 ; fadeValueR <= 235; fadeValueR +=0)
    for(int fadeValueB = 0; fadeValueB <= 0; fadeValueB +=0)
      {
        // sets the value (range from 0 to 255):
        analogWrite(ledPinR, fadeValueR);
      
        analogWrite(ledPinG, fadeValueG);
      
        analogWrite(ledPinB, fadeValueB);
      
      }
}



void fade()
{
  // fade out from max to min in increments of 5 points:
  for(int fadeValueR = 246  ; fadeValueR >= 0; fadeValueR -=5)
    for(int fadeValueG = 235  ; fadeValueG >= 0; fadeValueG -=0.5)
      for(int fadeValueB = 0  ; fadeValueB >= 0; fadeValueB -=10){

        // sets the value (range from 0 to 255):
        analogWrite(ledPinR, fadeValueR);
       delay(30);
       analogWrite(ledPinG, fadeValueG);
       delay(30);
       analogWrite(ledPinB, fadeValueB);
     delay(30);
      
      }
}
Logged

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

First, there's no reason to read buttonState in setup. You don't use the value, so don't make the call.

In the light function, you are fading the red pin from 246 to 246, the green pin from 235 to 235, and the blue pin from 0 to 0, with no time at each (non-existent step). What is the purpose of the for loops?

In the fade function, the green loop's integer value is decremented by 1/2 each time. How much of a change does that make to an integer? Hint: NONE.

The blue loop fades from 0 to 0. What is the purpose of that?

Logged

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

Quote
First, there's no reason to read buttonState in setup. You don't use the value, so don't make the call.
fair enough...
Quote
In the light function,
......

i have the values because they will reflect the amount of each indipendant colour.

if i put the red pin value at 255 then the colour would come our different.
otherwise its seems to freak out.

fade function: its fixes


I just am so confused with this whole set up.

All i want is a button to create a colour and have that colour fade out to off.

atm i am failing miserably


Logged

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

This is the code without fading which works fine.
Code:
const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;

int buttonState1 = 0;

byte redPwr = 0;
byte greenPwr = 0;
byte bluePwr = 0;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);


  pinMode(buttonPin1, INPUT);



}
void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW){
    redPwr = 246;
    greenPwr = 235;
    analogWrite(ledPinR, redPwr);
    bluePwr = 0;
    analogWrite(ledPinG, greenPwr);
    analogWrite(ledPinB, bluePwr);
    delay(2000);
  }
  else {
    analogWrite(ledPinR, LOW);
    analogWrite(ledPinG, LOW);
    analogWrite(ledPinB, LOW);
  }
}

Logged

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

In this last code, there is no call to light function from the previous code, which did nothing.

If you want the fade function to fade from some value to 0, for each color, pass those colors to the function:

Code:
void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
   for(int r=redFrom; r>=0; r-=5)
   {
      digitalWrite(redPin, r);
      for(int g=grnFrom; g>=0; g -=5)
      {
          digitalWrite(grnPin, g);
          for(int b=bluFrom; b>=0; b -=5)
          {
              digitalWrite(bluPin, b);
              delay(delayTime);
          }
      }
   }
}

This will fade blue to 0, then green to 0, then red to 0. You can change the order, if you like.

If you want the colors to fade to 0 at the same time:
Code:
void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
   for(int r=redFrom, g=grnFrom, b=bluuFrom; r>=0; r-=5, g-=5, b-=5)
   {
      digitalWrite(redPin, r);
        digitalWrite(grnPin, g);
        digitalWrite(bluPin, b);
        delay(delayTime);
   }
}
« Last Edit: January 24, 2010, 07:00:27 pm by PaulS » Logged

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

It says "  In function 'void loop()':
error: too few arguments to function 'void fade(int, int, int, int)"
Code:
void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
   for(int r=redFrom, g=grnFrom, b=bluuFrom; r>=0; r-=5, g-=5, b-=5)
   {
      digitalWrite(ledPinR, r);
        digitalWrite(ledPinG, g);
        digitalWrite(ledPinB, b);
        delay(1000);
   }
}

what figure do i put where?

say red = 200
      green = 100
      blue = 50

Thanks you so much for the code though!!!

Just need to get this done..

also will this work with randomized colours?

smiley


Logged

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

The error is in your call to the fade function, not in the fade function. You didn't post that code.

The function needs 4 argument - the red color to fade down from, the green color to fade down from, the blue color to fade down from, and the time to spend at each step.

You could call it like this:

Code:
fade(200, 100, 50, 25); // Stay at each level for 25 milliseconds.

You could call this with random values.
« Last Edit: January 24, 2010, 07:26:16 pm by PaulS » Logged

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

Im sorry .. its 12:38 here :/ im tired and i have to get this done by tomorrow smiley-sad

Still giving me errors.


Code:
void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW){
    redPwr = 200;
    greenPwr = 100;
    analogWrite(ledPinR, redPwr);
    bluePwr = 50;
    analogWrite(ledPinG, greenPwr);
    analogWrite(ledPinB, bluePwr);
    delay(1000);
    fade();
  
  }
    else {
    analogWrite(ledPinR, LOW);
    analogWrite(ledPinG, LOW);
    analogWrite(ledPinB, LOW);
  }
}

void fade(int 200, int 100, int 50, int 25)
{
   for(int r=200, g=100, b=50, r>=0; r-=5, g-=5, b-=5)
   {
      digitalWrite(ledPinR, r);
        digitalWrite(ledPinG, g);
        digitalWrite(ledPinB, b);
        delay(1000);
   }
}
 

*sigh* ! smiley-sad
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there. Since I'm awake I'm gonna take a shot at this.
It seems you have some basics mixed up. No fence or anything (I'm no expert myself).
Later on, maybe you should take a look at variables and functions:
http://www.cplusplus.com/doc/tutorial/variables/
http://www.cplusplus.com/doc/tutorial/functions/
There's also some info in the Arduino reference (about variable scope etc down the page)
http://arduino.cc/en/Reference/Extended

For now, in your main loop, what happened to:
Code:
fade();
vs
Quote
fade(200, 100, 50, 25); // Stay at each level for 25 milliseconds.
?

Change the constants (200,100... etc) with your variables, of course.. Which btw, in the same manner, why in your fade() function do you use constants in the argument list?
Code:
...
void fade(int 200, int 100, int 50, int 25)
{
   for(int r=200, g=100, b=50, r>=0; r-=5, g-=5, b-=5)
   {
...


I'm gonna hint that you need to change it to something like this:
Code:
...
void fade(int fromRed, int fromGreen, int fromBlue, int someDelay)
{
   for(int r=fromRed, g=fromGreen, b=fromBlue, r>=0; r-=5, g-=5, b-=5)
   {
...
Logged

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

is there any way you could just wack it in for me?

this is the code
Code:
const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;

int buttonState1 = 0;

byte redPwr = 0;
byte greenPwr = 0;
byte bluePwr = 0;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);


  pinMode(buttonPin1, INPUT);



}
void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW){
    delay(200);
    redPwr = 246;
    greenPwr = 235;
    analogWrite(ledPinR, redPwr);
    bluePwr = 0;
    analogWrite(ledPinG, greenPwr);
    analogWrite(ledPinB, bluePwr);
    fade(); // this is what is giving me problems! ********
    delay(500);
  
  }
    else {
    analogWrite(ledPinR, LOW);
    analogWrite(ledPinG, LOW);
    analogWrite(ledPinB, LOW);
  }
}
void fade(int redFrom=246, int grnFrom=235, int bluFrom=0, int delayTime=25)
{
   for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; r-=5, g-=5, b-=5)
   {
      digitalWrite(ledPinR, r);
        digitalWrite(ledPinG, g);
        digitalWrite(ledPinB, b);
        delay(delayTime);
   }
}



pretty pleaaase!
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah well alrighty then. If you promise to read my links in the previous post  smiley

No promises though, but it compiles here without errors.

Code:
const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;

int buttonState1 = 0;

byte redPwr = 0;
byte greenPwr = 0;
byte bluePwr = 0;

void fade(int, int, int, int); // forgot to mention you must declare a function prototype first (I think), or simply put the hole function here.

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);


  pinMode(buttonPin1, INPUT);



}
void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW){
    delay(200);
    redPwr = 246;
    greenPwr = 235;
    analogWrite(ledPinR, redPwr);
    bluePwr = 0;
    analogWrite(ledPinG, greenPwr);
    analogWrite(ledPinB, bluePwr);
    fade(redPwr,greenPwr,bluePwr,25);
    delay(500);

  }
    else {
    analogWrite(ledPinR, LOW);
    analogWrite(ledPinG, LOW);
    analogWrite(ledPinB, LOW);
  }
}
void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
   for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; r-=5, g-=5, b-=5)
   {
      digitalWrite(ledPinR, r);
        digitalWrite(ledPinG, g);
        digitalWrite(ledPinB, b);
        delay(delayTime);
   }
}


Note: Regarding the function declaration (the line "fade(int, int, int, int);" at the top, I'm not sure if it is a must after all - it compiles without it.
« Last Edit: January 24, 2010, 09:36:40 pm by raron » Logged

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

Ha! Well at least works... but...

still doesnt fade!

Oh well..
I will try to sort this out tomorrow

Thanks for you help and i promise to learn about C++

Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wops, I didn't notice, you should use analogWrite (and on PWM output pins of course, which you have).. digitalWrite is just that - digital on or off. In reality so are PWM pins, but they oscillate to sort of approximate an analog out.

Also, but less important, you have a delay(200) and delay(500) at the start and end of your button function. I don't think you need the one at the beginning (what's it for?), and the one at the end probably should be before the call to fade(). Or else it should begin to fade at once.

Code:
const int ledPinR = 9;
const int ledPinG= 11;
const int ledPinB = 10;

const int buttonPin1 = 3;

int buttonState1 = 0;

byte redPwr = 0;
byte greenPwr = 0;
byte bluePwr = 0;

void setup()
{
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(buttonPin1, INPUT);
}

void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW){
    redPwr = 246;
    greenPwr = 235;
    bluePwr = 0;
    analogWrite(ledPinR, redPwr);
    analogWrite(ledPinG, greenPwr);
    analogWrite(ledPinB, bluePwr);
    delay(500);
    fade(redPwr,greenPwr,bluePwr,25);
  } else {
    analogWrite(ledPinR, LOW);
    analogWrite(ledPinG, LOW);
    analogWrite(ledPinB, LOW);
  }
}

void fade(int redFrom, int grnFrom, int bluFrom, int delayTime)
{
   for (int r=redFrom, g=grnFrom, b=bluFrom; r>=0; r-=5, g-=5, b-=5)
   {
     analogWrite(ledPinR, r);
     analogWrite(ledPinG, g);
     analogWrite(ledPinB, b);
     delay(delayTime);
   }
}

Logged

Pages: [1]   Go Up
Jump to: