Push button & muti coloured LED

Hi all,

This should be something that’s really simple to do but I’m not sure how to do it.

On my breadboard I have a multi coloured LED which displays in red and green. This LED is plugged in to two of the digital pins and ground.

I’m using an RFID reader which operates a couple of motors so when the RFID tag is placed over the reader the motors run and the LED glows red or green depending on which direction the motors move.

What I want to do is the same process as above but instead of using the RFID reader the push button is used. So there would be two ways of operating the motors, the push button and the RFID reader.

I’ve read a number of push button tutorials but so far I’ve not found anything that’s helping me with this.

Anyone got any ideas how to connect up the push button? Or any links to a suitable tutorial would be good.

I’ve attached an image of what I’ve got so far.

This is the code I’m using to test it

int redPin = 2;
int greenPin = 8;
//int bluePin = 8;
int button = 1; // button pin


void setup()
{
  Serial.begin(115200); 
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
   
   pinMode(button, INPUT); // button on pin 1
   
}

void loop()
{
  
   int state = digitalRead(button);
    Serial.print(state);
    /* check if button is pressed */
    if (state == HIGH) {
        Serial.print("Green");
        setColor(0, 255, 0);     // Green
        delay(1000);
        
        Serial.print("Red");
        setColor(255, 0, 0);     // Red
        delay(1000);
    } else {
       Serial.print("state is LOW"); 
    }
}

void setColor(int red, int green, int blue)
{
    analogWrite(redPin, 255-red);
    analogWrite(greenPin, 255-green);
   // analogWrite(bluePin, 255-blue);
}

Cheers,

CaptainChainsaw

Change things a little:

int redPin = 2;
int greenPin = 8;
//int bluePin = 8;
int button = 1; // button pin   <<< Use a different pin, 0 & 1 are used by Serial


void setup()
{
  Serial.begin(115200); 
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
   
   pinMode(button, INPUT); // button on pin X << Change to INPUT_PULLUP for internal resistor
   
}

void loop()
{
  
   int state = digitalRead(button);
    Serial.print(state);
    /* check if button is pressed */
    if (state == LOW) {                     // Change to LOW - connect pin to GND when button is pushed
        Serial.print("Green");
        setColor(0, 255, 0);     // Green
        delay(1000);
        
        Serial.print("Red");
        setColor(255, 0, 0);     // Red
        delay(1000);
    } else {
       Serial.print("state is LOW"); 
    }
}

void setColor(int red, int green, int blue)
{
    analogWrite(redPin, 255-red);
    analogWrite(greenPin, 255-green);
   // analogWrite(bluePin, 255-blue);
}

Hey,

Thank you very much, that certainly helped! I wasn't quite sure how to wire up the button but I've got it going now and the updated code did the trick.

This diagram helped me: http://www.nudatech.com/blog/controlling-an-rgb-led-with-arduino/

As all the other tutorials | looked at just used a single colour LED that didn't help.

Thanks again

CaptainChainsaw :)

lol, same project as me.. same example i saw..

Cool :)

I got my circuit and code working last night. When I get it totally finished I should really document it so if you get stuck that might help you (if you need it of course). Although you're likely to be using different components.

Good luck!

One thing I was thinking of adding was a feature to automatically lock the door if it has unlocked for a few mins. Not sure how to do that yet. I have some ideas but they're not too elegant.

Cheers,

CaptainChainsaw