Pages: [1]   Go Down
Author Topic: RGB fader with up/down buttons  (Read 410 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
:D
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
Im writing some code (attached below) that is supposed to control an RGB led.
Whats supposed to happen is that you put the "device" into programming mode with a switch (pgrmPin) and then using 2 up/down buttons per led color select the color. so far nothing is happening.

The final version is meant to run on an attiny2313 that has a ported Arduino bootloader, thats why the pins are set the way they are. I have tried however to change the pin and run it on my Duemlinanove but still no success.
(yes the leds and buttons work)
if anyone can help it would be really appreciated.

Nadav.

Code:
const int redPin   =   13; //Define red led
const int greenPin =   12;//Define green led
const int bluePin  =   11;//Define blue led
const int pgrmPin  =   10;//Define program switch
const int pgrmRUPin =   7;//Define red up button
const int pgrmGUPin =   6;//Define green up button
const int pgrmBUPin =   5;//Define blue up button
const int pgrmRDPin =   4;//Define red down button
const int pgrmGDPin =   3;//Define green down button
const int pgrmBDPin =   2;//Define blue down button
int red = 0;
int blue = 0;
int green = 0;

void setup() {
  pinMode(redPin, OUTPUT);     //PWM PIN 13 (PB4)
  pinMode(greenPin, OUTPUT);     //PWM PIN 12 (PB3)
  pinMode(bluePin, OUTPUT);     //PWM PIN 11 (PB2)
  pinMode(pgrmPin, INPUT);     //Digital PIN 10 (PB1)
  pinMode(pgrmRUPin, INPUT);     //Digital PIN 7 (PD5)
  pinMode(pgrmGUPin, INPUT);     //Digital PIN 6 (PD4)
  pinMode(pgrmBUPin, INPUT);     //Digital PIN 5 (PD3)
  pinMode(pgrmRDPin, INPUT);     //Digital PIN 4 (PD2)
  pinMode(pgrmGDPin, INPUT);     //Digital PIN 3 (PA0)
  pinMode(pgrmBDPin, INPUT);     //Digital PIN 2 (PA1)
}

void loop() {
  if (pgrmPin == HIGH){//Enter Program mode
    if (pgrmRUPin == HIGH && red >= 0 && red < 255){
      red++;
    }
    if (pgrmRDPin == HIGH && red > 0 && red <= 255){
      red--;
    }

    if (pgrmGUPin == HIGH && green >= 0 && green < 255){
      green++;
    }
    if (pgrmGDPin == HIGH && green > 0 && green <= 255){
      green--;
    }
    if (pgrmBUPin == HIGH && blue >= 0 && blue < 255){
      blue++;
    }
    if (pgrmBDPin == HIGH && blue > 0 && blue <= 255){
      blue--;
    }
    analogWrite(redPin, red);
    analogWrite(greenPin, green);
    analogWrite(bluePin, blue);
}
 analogWrite(redPin, red);
 analogWrite(greenPin, green);
 analogWrite(bluePin, blue);
}

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int pgrmPin  =   10;//Define program switch

void setup() {
  pinMode(pgrmPin, INPUT);     //Digital PIN 10 (PB1)
}

void loop() {
  if (pgrmPin == HIGH)
}

Since prgmPin is set to 10 it will never be equal to HIGH (1).  What you meant to say is:
Code:
  if (digitalRead(prgmPin) == HIGH)

Which is equivalent to:
Code:
  if (digitalRead(prgmPin))
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19365
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int redPin   =   13; //Define red led
const int greenPin =   12;//Define green led
const int bluePin  =   11;//Define blue led

Pins 12 and 13 are not PWM pins on the Duemlinanove.

Do you have pull-ups or something on those pins? How is it wired?

Code:

  if (pgrmPin == HIGH){//Enter Program mode
    if (pgrmRUPin == HIGH && red >= 0 && red < 255){
      red++;

The loop function executes thousands of times a second. It is likely it is immediately going to either full on or full off. You need to check for a button press which is different from last time, eg.

Code:
if (pgrmRUPin == HIGH && oldpgrmRUPin == LOW)
  {
  if (red >= 0 && red < 255)
    red++;
  delay (10);  // debounce
  }

// detect switch changes
oldpgrmRUPin = pgrmRUPin;

(edit) Plus what John said. You need to read the pin, not just use the pin number.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 37
:D
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int redPin   =   13; //Define red led
const int greenPin =   12;//Define green led
const int bluePin  =   11;//Define blue led

Pins 12 and 13 are not PWM pins on the Duemlinanove.

Do you have pull-ups or something on those pins? How is it wired?

Code:

  if (pgrmPin == HIGH){//Enter Program mode
    if (pgrmRUPin == HIGH && red >= 0 && red < 255){
      red++;

The loop function executes thousands of times a second. It is likely it is immediately going to either full on or full off. You need to check for a button press which is different from last time, eg.

Code:
if (pgrmRUPin == HIGH && oldpgrmRUPin == LOW)
  {
  if (red >= 0 && red < 255)
    red++;
  delay (10);  // debounce
  }

// detect switch changes
oldpgrmRUPin = pgrmRUPin;

(edit) Plus what John said. You need to read the pin, not just use the pin number.

Great ill try this tonight! I cant believe if forgot to read the pin! silly me smiley-grin
Also about pins 12 & 13 they are the pwm on the attiny2313..
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19365
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Also about pins 12 & 13 they are the pwm on the attiny2313..

Right. I thought you were talking about testing it on your Duemlinanove, hence my comment:

Quote
I have tried however to change the pin and run it on my Duemlinanove but still no success.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1]   Go Up
Jump to: