Pages: 1 [2]   Go Down
Author Topic: RGB Led, School-project, need help!  (Read 1824 times)
0 Members and 1 Guest are viewing this topic.
Forum Administrator
MMX
Offline Offline
Edison Member
*****
Karma: 37
Posts: 1139
hallo kompis
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The other pin has to go to ground. You don't need any resistors, because the code is activating an internal resistor in the processor. Actually this reminds me  that you need to compare the buttons to LOW and not HIGH to check whether they are pressed or not, since by default they will be HIGH. Here the modified software:

Code:
int redLed = 11;
int greenLed = 10;
int blueLed = 9;

int btn1 = 12;
int btn2 = 8;

boolean rnd = true;
boolean turnOn = true;

long randNumb;
long randTime;

void setup()
{
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
 
  pinMode(btn1, INPUT);
  digitalWrite(btn1, HIGH);
  pinMode(btn2, INPUT);
  digitalWrite(btn2, HIGH);
}

void red()
{
  analogWrite(redLed, 255);
  analogWrite(greenLed, 0);
  analogWrite(blueLed, 0);
}

void green()
{
  analogWrite(redLed, 0);
  analogWrite(greenLed, 255);
  analogWrite(blueLed, 0);
}

void blue()
{
  analogWrite(redLed, 0);
  analogWrite(greenLed, 0);
  analogWrite(blueLed, 255);
}

void yellow()
{
  analogWrite(redLed, 255);
  analogWrite(greenLed, 255);
  analogWrite(blueLed, 0);
}

void orange()
{
  analogWrite(redLed, 255);
  analogWrite(greenLed, 35);
  analogWrite(blueLed, 255);
}

void pink()
{
  analogWrite(redLed, 255);
  analogWrite(greenLed, 0);
  analogWrite(blueLed, 162);
}

void purple()
{
  analogWrite(redLed, 88);
  analogWrite(greenLed, 0);
  analogWrite(blueLed, 112);
}

void bluegreen()
{
  analogWrite(redLed, 0);
  analogWrite(greenLed, 144);
  analogWrite(blueLed, 255);
}

void white()
{
  analogWrite(redLed, 255);
  analogWrite(greenLed, 255);
  analogWrite(blueLed, 255);
}

void loop()
{
  if (rnd && turnOn) {
    randNumb = random(1, 10); // Generate a random number between 1 and 10
    randTime = random(250, 1001);  // Generate a random number between 250 and 1000

    if(randNumb == 1){
      red();
    }
    if(randNumb == 2){
      green();
    }
    if(randNumb == 3){
      blue();
    }
    if(randNumb == 4){
      yellow();
    }
    if(randNumb == 5){
      orange();
    }
    if(randNumb == 6){
      pink();
    }
    if(randNumb == 7){
      purple();
    }
    if(randNumb == 8){
      bluegreen();
    }
    if(randNumb == 9){
      white();
    }

    delay(randTime);
  }
  else if (not turnOn) {
    analogWrite(redLed, 0);
    analogWrite(greenLed, 0);
    analogWrite(blueLed, 0);
  }
  else if (not rnd) {
    analogWrite(redLed, 50);
    analogWrite(greenLed, 50);
    analogWrite(blueLed, 50);
  }

  if (digitalRead(btn1) == LOW) {
    rnd = not rnd;
  }

  if (digitalRead(btn2) == LOW) {
    turnOn = not turnOn;
  }

}
Logged

Sweden
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Extremely rookie
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

more precisely should i wire?
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the code, right up top, which pin to which button.

button 1: pin 12 --- button --- ground
button 2: pin 8 --- button --- ground

Your led is on pins 9, 10, 11.

I could suggest some debounce in the code? Use it if the circuit acts up, some buttons are better than others, some stink. I tested this code by plugging 1 jumper in a digital pin hole and 1 jumper in ground. Then I touched the other ends of the jumpers together, and I wiped them across each other, etc. And then I did the same but using digitalRead() without the debounce and really what I learned is either touching jumper pins is pretty clean or I can see the output change fast enough to catch it all, because at times it looked like it. So don't bother unless your buttons seem 'unsteady'.

getButton() would replace your call(s) to digitalRead() -- only in case of button trouble

Code:
byte getButton( byte pin )
{
  byte A, B, C = 0;
  A = digitalRead( pin );
  while ( C < 4 ) // need 3 matching reads in a row, 5 ms apart, to determine button state
  {
    delay( 5 );    // this can be adjusted but more than 10 would be for a really baaaad button
    B = digitalRead( pin );
    if ( B == A )   // match, increment C
    {
      C++;
    }
    else {           // not match, start over with the last read state
      C = 0;
      A = B;
    }
  }
}


Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

looks good, but where do you returns your 'byte' ? since you declare it as returning one..

and why is a button high in standard state? i always thought it was low..?
Logged


Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4808
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now that's really weird because, it works and you're right, it shouldn't!

I am running 0022. Here's the q&d test code that I just recompiled and it runs and does what I expect!

I ground the jumper in 7 to the jumper in GND and the monitor prints 0. I break the connect and it prints 1. Just before the closing bracket there it should say: return A;
But .. it .. WORKS!

Code:
void setup( void )
{
  Serial.begin(9600);
  pinMode( 7, INPUT );
  digitalWrite( 7, HIGH );
  pinMode( 3, INPUT );
  digitalWrite( 3, HIGH );
}

byte wasPS3 = 1;
byte pinState3 = 1;

byte wasPS7 = 1;
byte pinState7 = 1;

byte getButton( byte pin )
{
  byte A, B, C = 0;
  A = digitalRead( pin );
  while ( C < 4 ) // need 3 reads in a row, 5 ms apart, that match
  {
    delay( 5 );
    B = digitalRead( pin );
    if ( B == A )
    {
      C++;
    }
    else {
      C = 0;
      A = B;
    }
  }
}

void loop( void )
{
  pinState3 = digitalRead( 3 );
  if ( pinState3 != wasPS3 )
  {
    wasPS3 = pinState3;
    Serial.println( pinState3 + 100, DEC );
  }

  pinState7 = getButton( 7 );
  if ( pinState7 != wasPS7 )
  {
    wasPS7 = pinState7;
    Serial.println( pinState7, DEC );
  }
}

Must be something the compiler did. Maybe the first time ever a compiler fix my F-U.

I made the change and on test it still works:

Code:
byte getButton( byte pin )
{
  byte A, B, C = 0;
  A = digitalRead( pin );
  while ( C < 4 ) // need 3 reads in a row, 5 ms apart, that match
  {
    delay( 5 );
    B = digitalRead( pin );
    if ( B == A )
    {
      C++;
    }
    else {
      C = 0;
      A = B;
    }
  }
  return  A;
}

And as far as should the button read HIGH or LOW when not pressed, the answer is that it depends on how it's wired up and what you do with the pins.

In this case the power is fed through about a 20k internal resistor to the pin the 'button' is connected to. There's almost not enough current there to light up a led (a red one, it was pathetic little glow and didn't bring pin 7 low... try it!) much less burn out a pin. It's safe and it doesn't require me extra parts beyond 3 jumpers and the UNO to run my test.

And my test says it takes dirty switches to need debouncing! Dirtier than touching jumper ends at least.

And LOL, 5V through 220R to a red led makes one very bright red led. Through 20k and it's Mick Jagger, one of the Glimmer Twins! Because that's -all- I got was a near dead cig-butt glimmer!

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: 1 [2]   Go Up
Jump to: