Argg... I Forgot So Much

Okay, so I've just gotten an Arduino Uno to add to my collection of gizmos laying around. I took C++ about 6 years ago, then never touched it again so I'm basically starting at zero on my programming. I'm running through various examples online, both on the site here and on J. Blum's YouTube. My current project is just some extra credit for myself so that I gather a better understanding of things again.

My idea is to have one LED come on and off with the push of a switch, and a second LED have 5 varying brightness settings. I'd like the variable LED to increase in brightness with each push of the button, then go off once I've reached the PWM max. I've been able to get it to brighten and dim and go off, but never in my expected 6 clicks of the button. I'm thinking that either A) I don't truly understand the debounce function of the code and which variable to pass to my PWM led, or B) that I need to utilize some sort of counter on the button pushes and use that as a multiplier for my increasing brightness variable. Any help would be great. I'm at a bit of a road block and the misses is getting a bit frustrated with how much time I'm geeked out on my laptop. :slight_smile:

The area I'm stuck with is towards the bottom of my code and is lacking all my overly gratuitous comments.

Here's my code:

/*
/*
Switch debounce via software code. This idea was from Jeremy 
Blums YouTube Channel.

Chris Cronan
Arduino Uno

*/

// Global variables
int switchPin = 8;                                                                                                  // Initializes an integer global variable named 'switchPin' on digital pin 8 of the Uno
int ledPin = 13;                                                                                                    // Initializes an integer global variable named 'ledPin' on digital pin 13 of the Uno

int pwmPin = 3;                                                                                                     // Initializes PWM pin 3 as the variable 'pwmPin'
int brightness = 0;                                                                                                 // Initializes the variable 'brightness' and sets it to equal 0
int brightLED = 51;                                                                                                 // Initializes the variable brightLED and sets it equal to 51                                                                                               

// Global variable used for removing the bounce from the switch push
boolean lastButton = LOW;                                                                                          // Initializes a true/false, high/low, on/off global variable named 'lastButton' and sets it to LOW
boolean ledOn = false;                                                                                             // Initialzes a boolean globabl variable of 'ledOn' and sets it to fasle/off/low
boolean currentButton = LOW;                                                                                       // Initializes a boolean global variable of 'currentButton' in the off state

void setup()
//Only ran once
{
    pinMode (switchPin, INPUT);                                                                                    // Sets pin 8 'switchPin' as an input pin
    
    pinMode (ledPin, OUTPUT);                                                                                      // Sets pin 13 'ledPin' as an output pin
    
    
    pinMode (pwmPin, OUTPUT);
}

boolean debounce (boolean last)                                                                                    // creates a boolean logic program to negate the bouncing of the switch when pressed

{
 boolean current = digitalRead (switchPin);                                                                        // Initializes a variable of 'current' and sets it to equal the read input of the 'switchPin' on pin 8
 
 if (last != current)                                                                                              // if the last state of the button is not equal to the current state of the button, read on switchPin
 
   {
     delay (5);                                                                                                    // wait 5 milliseconds
     
     current = digitalRead (switchPin);                                                                            // then read the current state of the switchPin again and set it to equal variable 'current'
     
   }
  return current;                                                                                                   // now pass the variable current back to the beginning of the function to be set equal to the currently
}
// ends the debounce function

void loop()
{
  // Turns and LED on and off with a software based captive relay
  currentButton = debounce(lastButton);                                                                            // sets 'currentButton' equal to the cleaned up last button push
  
   if (lastButton == LOW && currentButton == HIGH)                                                                 // if the 'lastButton' is equal to LOW, and the 'currentButton' is equal to HIGH
   
     { 
       ledOn = !ledOn;                                                                                            // change the state of the variable 'ledOn' to the opposite of its current state
       
     } 
     lastButton = currentButton;                                                                                  // now set the 'lastButton' push to equal the 'currentButton' state
    
     digitalWrite (ledPin, ledOn);                                                                                // then write the 'ledPin' (13) to the proper state as passed in the variable 'ledOn'
      
      
  // Modulates the brightness of an LED with PWM on PWM pin 3, changed with each button push
  if (currentButton == HIGH)
    {
      brightness = brightness + brightLED;                                                                        // Increases the brightness of the pwm led by the amount defined in the variable 'brightLED'
    }
       
  if (brightness > 255) brightness = 0;
  
  analogWrite (pwmPin, brightness);
  
}
// end of sketch

please modify your post, select the code part and press # button above the smileys so the code get proper tagged.
Makes it more readable.

comments like this one which is obvious from the code

// Initializes an integer global variable named 'switchPin' on digital pin 8 of the Uno

is imho a waste of time and obfuscates the code and will therefor not help you.

Can you explain what you expected, and what the code actually does?

To add to Rob's comment.

Excessive commenting like that is actually bad. If you have something like this:

// Set pinswitchmode to 42
pinswitchmode == 42;

The comment can blind you to the fact that you did not set pinswitchmode to 42. Some experts actually suggest that you write all your comments on the right hand side of the page so that you can cover them during debugging and not be subtly influenced by what they say.

I do recall learning to write comments out to the right of each line. I guess I was mostly just writing all those comments to help me keep everything straight in my head while I was working on this. I have updated the code so it should read properly now.

Code updated with comments out to far right. They are for my reference and understanding until everything comes back to me again and I begin to really remember things as second nature again.

imho the comments should in general only explain the WHY of the code, not the WHAT or HOW (unless optimizing or tricky code)

example

// debounce()
// checks the state of a switch and if value has changed since last time we must read again to suppress bouncing
// parameter: last state of the switch
// returns:      current state of the switch
boolean debounce (boolean last)
{
  boolean current = digitalRead (switchPin);
 
  if ( last != current )
  {
    delay (5);
    current = digitalRead (switchPin);
  }
  return current;
}

google "debounce arduino" for more robust debouncers

robtillaart:
Can you explain what you expected, and what the code actually does?

I expected that one LED would turn on and off (works fine) and that the second coming off of PWM pin 3 would increase in brightness. I was trying to blend in what I learned from the Fading exercise to what I was learning from Jeremy's YouTube. What I've been able to accomplish so far with this code is that the varying brightness LED doesn't really seem to do much. I wish I'd saved other revisions of code but I was just trying different things at will yesterday and not really approaching the issue properly. I

robtillaart:
imho the comments should in general only explain the WHY of the code, not the WHAT or HOW (unless optimizing or tricky code)

example

// debounce()

// checks the state of a switch and if value has changed since last time we must read again to suppress bouncing
// parameter: last state of the switch
// returns:      current state of the switch
boolean debounce (boolean last)
{
 boolean current = digitalRead (switchPin);

if ( last != current )
 {
   delay (5);
   current = digitalRead (switchPin);
 }
 return current;
}




*google "debounce arduino" for more robust debouncers*

Thank you very much. I'll search that in a bit. Sadly now I've got to get back to work. I'll post up this evening (assuming I have time to tinker) what sort of results I have. Oh, and in regards to the hardware setup on the breadboard. That aspect I'm certain is good as I do that sort of stuff everyday at work. I just don't write code anymore and am struggling to get back into the swing of things.