Toggle A LED With a Switch

After playing around for a bit, Ive come up with some code that toggles a LED based on the press of a button. Im sure a lot of the code is redundant and unnecessary, but I learn through trial and error, and this worked for me. I also included the debounce library, because Im using jumper wires in a breadboard and not an actual switch.

Ive commented the code as best I could. If you have any suggestions or ways I could have done this better, please feel free to tell me!

#include <Debounce.h>

bool wasHigh = false;
bool beenLow = true;
int ledPin = 13;
int switchPin = 9;

Debounce debouncer = Debounce(20, switchPin); 

void setup()
{
  pinMode(ledPin,OUTPUT);
  pinMode(switchPin,INPUT);
}

void loop()
{
  debouncer.update();                        // update the debouncer
  if ((debouncer.read() == HIGH) && beenLow)      // if the switch has been pressed AND it has been released (beenLow == true)
  {
    if (wasHigh)                         // and if the light was high last time
    { 
      digitalWrite(ledPin,LOW);             // toggle the light low
      wasHigh = false;                   // define that the light was not high
    }
    else                               // otherwise the light was low last time
    {
      digitalWrite(ledPin, HIGH);             // so toggle the light high
      wasHigh = true;                   // and define that the light was high
    }
    beenLow = false;                         // define that the switch hasnt been released, to prevent the light from flashing as the loop runs
  }

  if (debouncer.read() == LOW)                   // if the switch has been released (and is pulled low by the pulldown resistor)
  {
    if (wasHigh)                         // and if the light was on
      digitalWrite(ledPin,HIGH);             // keep it on
    else                               // otherwise the light was off
      digitalWrite(ledPin,LOW);             // so keep it off
    beenLow = true;                         // define that the switch has been released
  }
}

This is another way of doing it: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247178706/3#1

I think your code looks just fine :slight_smile:

yes looks good, but i don't really understand why you add a library for something like that.

anyway ladyada has a good tutorial about buttons Arduino Tutorial - Lesson 5 in this tutorial is also a way how to avoid bouncing.

yes looks good, but i don't really understand why you add a library for something like that.

It is because it looks good, and is easy to understand for a complete beginner / non-programmer.
http://www.arduino.cc/playground/Code/HardwareAbstraction#Why

:slight_smile:

It is because it looks good, and is easy to understand for a complete beginner / non-programmer.

They do make the code look simpler, but I wonder if they're healthy for a complete beginner as they (by design) hide what's actually going on. In this case someone might use the debounce library because you should debounce switches, and never actually find out what debouncing is and why contact bounce is a problem.

On the other hand, I program in Java nowadays rather than machine code so I'm not averse to a few layers of abstraction myself :slight_smile:

Andrew

Hi Andrew, many Arduino users don't care about underlying causes of things like contact bounce, they just don't want it getting in the way of completing their project. The high level of abstraction available for Arduino is major reason why its so popular.

I would agree that beginners in computer science studies would benefit from understanding the low level issues, but I think that computer scientists are not the primary target for this forum. :wink:

Exactly, a lot of people that use Arduino's, artists for example, just want it to work and not worry about how it works too much.

I'm guilty of cargo cult programming myself when I encounter something new, like a new language or API. However it all falls apart when something stops working and you don't know why because you don't know why it worked in the first place.

I guess everybody finds the level of abstraction that suits their needs and moves up a level when they just need to get something done, and moves down a level when they need to satisfy their inner geek.

Andrew

Solving a problem by using a suitable existing abstraction is not cargo cult programming, its good software engineering. I do agree that there is a great deal of satisfaction in opening the hood and getting your hands dirty. But I don't think we should knock people that don't need or want to go there.

You might like to try my DigitalToggle library for this sketch.

Since the requirement for your sketch was to toggle the output rather than keep track of the current state of the output you can simplify your code by using that library.

For additional practice you might consider adding a 'long press' of the button that uses analogWrite to set the LED to half-brightness. This would give you a number of different states that you could manage using a state machine.

Also, it's great to see good comments in code!

I like to use very verbose comments that describe what I'm trying to do and why, but not so much about how. That way when I go back and find stuff that doesn't work I can see what I was trying to do. It isn't unusual for me to have many times more comments than code.

I apologise if I came across as knocking anyone. That wasn't my intention at all (in this discussion, anyway).

And I second the comment about comments :slight_smile: Those are some of the best I've seen in Arduino code, and not a

x = x + 1; // Add one to x

type comment in sight.

Andrew