Simple Programs

Ok I know this is an easy one but if anyone could point me where I should look would be great.

I am just getting starting (2nd day messing around) with all of this stuff and all i have is 2 buttons and 2 LEDs and whatnot to mess around with.

So.. I want to make it so one LED will go on with a button push and then stay on until that button is pushed again.

...also any other idea you people can come up with for me to make my two buttons and two LEDs do would be great.

Thank in advance!

I'm not sure what you know about electronics or coding, but with swithces you need to know about contact bounce: Switch - Wikipedia , and methods for supressing that. Otherwise you most likely will get multiple button presses for one.

A look at the Arduino playground under Input - mechancial - switches has some software to look at: Arduino Playground - InterfacingWithHardware Also there is a Debounce example in the Arduino IDE, under "File - Sketchbook - Examples - Digital".

You also need to know about pullup and pulldown resistors: http://www.seattlerobotics.org/encoder/mar97/basics.html

With two LED's and buttons there's not a hole lot you can do, but its a start to a hole lot :slight_smile: You could look at what this guy is trying to do with two LED's (one red and one blue) and a button: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1249624955/14#14

alright I read everything and have even more questions.

I added the 'Buttons' to my library. Now I just need someone to tell me how to use it. Not much of an explanation on that page.

I used the Debounce example to make my set up. If I could edit that it would be great.

also I am trying to make blinkers. so push one butting, LED blinks for a bit. push the other button other LED blinks. You get the idea.

I hope you didn't mix the Buttons library with the Debounce example?

I've never used the Buttons lib, so maybe others can correct me if I'm wrong. I agree its not very well explained. And also it seems he forgot a line in his example (in the somewhat confusing image, which shows the header file with black background to the left, listing the methods and constants quickly, and an example in the Arduino IDE to the right).

But it does say so below the image:

Don't forget to link the Arduino pins to the library with ButtonName.assign...

So I think he forgot to include a "B.assign(pin);" in the seup() function in that example. Where "pin" is a byte value (or simply a constant) of the Arduino pin nr for the button.

I don't know how far/short you've gone, but maybe for a first tiny test, make it so that one of the LEDs turns on when one of the buttons are pressed down, and off when not (very easy, no library, debounce or nothing needed really. In fact no Arduiono needed either, but you could still test it with one :wink: )

Another easy test, turn the LED(s) off again if both buttons are pressed down.

Then move on to what you said in your original post; at the first press of a button, turn the LED on, and then at the next press of the same (or the other?) button, turn it off. Just to get the debounce button-thing going. Whether you use the Buttons library or not. Then worry about your blinker.

This is just a suggestion, of course.

I hope you didn't mix the Buttons library with the Debounce example?

No, I haven't done anything with the Button library. I need someone to explain it to me.

I don't know how far/short you've gone, but maybe for a first tiny test, make it so that one of the LEDs turns on when one of the buttons are pressed down, and off when not

Yeah that is one of the examples. I have done all I can do with that for now.

I some how managed to get it to work with button push > led on , button push > led off. but it is weird. if you don't time it right it doesn't work... i dont know. here is my code

const int buttonPIN = 2;
const int ledPIN     = 13;

int ledState = HIGH;
int buttonState;

void setup () {
  pinMode (buttonPIN, INPUT);
  pinMode (ledPIN, OUTPUT);
  
}


void loop () {
  
  int ledState = digitalRead(ledPIN);
  int buttonState = digitalRead(buttonPIN);
  
  if (buttonState == HIGH) {
    if (ledState == LOW) {
      digitalWrite(ledPIN, HIGH);
    }
  }
  
  if (buttonState == HIGH) {
   if (ledState == HIGH) {
      digitalWrite(ledPIN, LOW);
  }  
 }
}

please tell me everywhere i messed up .. haha remember im just getting started. ;D

ok so I need some more help .... I have a RGB Tricolor LED. I want it hook up so that I push the button and it is one color. push the same button, different color, so on and so on.

i have the code worked out i think ... it works with three different LEDs but i just dont want to have wasted 2.50 on this stupid thing.

the red part seems to always come on with the blue and the green is just find. so please any help would be GREAT!

Hi Smiley,

Can you post the code you have for the tri-color sketch, it is easier to help when we can see the whole picture.

const int buttonPIN = 2;
const int ledPING   = 10;
const int ledPINB   = 12;
const int ledPINR   = 11;

int ledStateG;
int ledStateB;
int ledStateR;
int buttonState;

void setup () {
  pinMode (buttonPIN, INPUT);
  pinMode (ledPING, OUTPUT);
  pinMode (ledPINB, OUTPUT);
  pinMode (ledPINR, OUTPUT);
  digitalWrite(ledPINB, HIGH);
  digitalWrite(ledPING, LOW);
  digitalWrite(ledPINR, LOW);
  
}


void loop () {
  
  int ledStateG = digitalRead(ledPING);
  int ledStateB = digitalRead(ledPINB);
  int ledStateR = digitalRead(ledPINR);
  int buttonState = digitalRead(buttonPIN);
  
  
  if (buttonState == HIGH) {
    if (ledStateG == HIGH){
      if (ledStateB == LOW){
        if (ledStateR == LOW){
          digitalWrite(ledPINB, HIGH);
          delay (100);
          digitalWrite(ledPINB, LOW);
          delay (100);
          digitalWrite(ledPINB, HIGH);
          delay (100);
          digitalWrite(ledPINB, LOW);
          delay (100);
          digitalWrite(ledPINB, HIGH);
          delay (100);
          digitalWrite(ledPINB, LOW);
          delay (100);
          digitalWrite(ledPING, LOW);
          digitalWrite(ledPINB, HIGH);
          digitalWrite(ledPINR, LOW);
       }
      }
    }
  }
  
  if (buttonState == HIGH) {
    if (ledStateG == LOW){
      if (ledStateB == HIGH){
        if (ledStateR == LOW){
          digitalWrite(ledPINR, HIGH);
          delay (100);
          digitalWrite(ledPINR, LOW);
          delay (100);
          digitalWrite(ledPINR, HIGH);
          delay (100);
          digitalWrite(ledPINR, LOW);
          delay (100);
          digitalWrite(ledPINR, HIGH);
          delay (100);
          digitalWrite(ledPINR, LOW);
          delay (100);
          digitalWrite(ledPING, LOW);
          digitalWrite(ledPINB, LOW);
          digitalWrite(ledPINR, HIGH);
       }
      }
    }
  }
  
  if (buttonState == HIGH) {
    if (ledStateG == LOW){
      if (ledStateB == LOW){
        if (ledStateR == HIGH){
          digitalWrite(ledPING, HIGH);
          delay (100);
          digitalWrite(ledPING, LOW);
          delay (100);
          digitalWrite(ledPING, HIGH);
          delay (100);
          digitalWrite(ledPING, LOW);
          delay (100);
          digitalWrite(ledPING, HIGH);
          delay (100);
          digitalWrite(ledPING, LOW);
          delay (100);
          digitalWrite(ledPING, HIGH);
          digitalWrite(ledPINB, LOW);
          digitalWrite(ledPINR, LOW);
       }
      }
    }
  }
}

Well, you should probably assign variables to control states, instead of reading an Output port to determine...

This will do as you asked,"I want it hook up so that I push the button and it is one color. push the same button, different color, so on and so on."

However, if you have a bouncy switch, you may skip colors. See the debouncing libraries and samples in the playground for more... (I couldn't take away all your fun, now, could I?)

#define buttonPIN 2
#define ledPING   10
#define ledPINB   12
#define ledPINR   11

int buttonState;
int lastButtonState;

void setup () 
{
   pinMode (buttonPIN, INPUT);
   pinMode (ledPING, OUTPUT);
   pinMode (ledPINB, OUTPUT);
   pinMode (ledPINR, OUTPUT);

   buttonState='R';
   lastButtonState=LOW;
   adjustLEDs();
 
}

void adjustLEDs()
{
    digitalWrite(ledPINB, buttonState == 'B');
    digitalWrite(ledPING, buttonState == 'G');
    digitalWrite(ledPINR, buttonState == 'R');
}

void loop () 
{
   int iCurrentState = digitalRead(buttonPIN);
   
   // this changes colors on release of button.  Reverse
   // HIGH and LOW in this next line to detect it on the initial press of the button.
   if(lastButtonState==HIGH && iCurrentState==LOW)
  {
     if(buttonState=='B')
         buttonState='G';
     else if(buttonState=='G')
         buttonState='R';
      else if(buttonState=='R')
         buttonState='B';

     adjustLEDs();
   }
   lastButtonState = iCurrentState;
}

I haven't compiled this myself, but it should work...

i dont understand how to program the debounce stuff.

also ... can (or should) the digital pins be used as grounds. the LEDs power pin is 2 and so I cant really have them all ground out to one pin...

i am trying your code now

oh and also my code does work the way i want with 3 different LEDs...
so i am guessing it has something to do with this RGB Tri-color one

yeah your code does the same as mine. (with out blinking of course)

when it should be red, the red and blue side light up.

thats why i am wondering if i should have it wired different

-EDIT-

scratch that.. it seems that when it should be red both the blue and green sides light up. only with i remove the lead to either does the red light up