Blum Tutorial 2, PWM Question

Hi,

I've been working through various tutorials and books for the last month or so, coming from zero Arduino or C++ experience (took a VB course in college back in '99.. LOL).

Anyway, in Blum's second tutorial, the second half talks about how to use PWM to control the brightness of an LED, basically:

int switchPin = 8;
int whiteLed = 3;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledLevel = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(switchPin, INPUT);
  pinMode(whiteLed, OUTPUT);

}

boolean debounce(boolean last) //recieves lastButton (default of LOW) from loop and renames it "last"
  {
  boolean current = digitalRead(switchPin); //creates 'current' variable and assigns the current button state to it
  if (last != current)                      //checks to see if lastButton is equal to current. if it is not, this means the button just changed state
  {
   delay(5);                                //only done if the button was just pressed.  Delays 5ms and takes a second reading to avoid bounce
   current = digitalRead(switchPin);
   
  }
  return current;                           //returns the current state of the button back to the loop
}
void loop()
{
  currentButton = debounce(lastButton); //send lastButton (default of LOW) to debounce and wait for return value, then set currentButton to returned value
  
  if (lastButton == LOW && currentButton == HIGH) //if the previous button value was low and the returned current button value is high, the button was just pressed
  {
                                
    ledLevel = ledLevel + 51;
    
    
   }
 
    lastButton = currentButton;    //set curretButton to lastButton otherwise lastButton is always LOW
    
  if (ledLevel > 255) ledLevel = 0;
   
    analogWrite(whiteLed, ledLevel);
    Serial.println(ledLevel);
    
}

Serial output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
51
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
102
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
153
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
204
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
50
101
101
101
101
101
101
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
152
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
203
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
254
49
49
49
49
49
49
49
49
49
49
49
49
49
49

So, when I set this all up as he describes, I noticed that, after the first cycle, my LED was not shutting all the way off. I then set it up to output the ledLevel value over serial (output listed above) and what I found was that the arduino is apparently wrapping around any values over 255. After the first cycle, instead of returning to zero per the code ( if ledLevel > 255, ledLevel = 0) it was set to 50, the next cycle would start at 49, then 48, etc. Was there a change made to cause this behavior after the Blum tutorials were made? I was able to work around it by preventing it from adding +51 if the value was already 255 and having the reset based on a button press counter instead. I'm just curious why this didn't behave the way it was supposed to according to the video.

Thanks in advance.

Don’t share a code that is irrelevant to what you explain.
Post the code you really used and show us the output of the Serial console

Thanks J-M-L, I updated my post to include the full code as well as the Serial output.

Which board do you have selected?

At least on Arduino AVR Boards core of the Uno, Mega, Leonardo, etc., the boolean data type of the ledlevel variable can only have the values of 0 and 1 so I was surprised that you're getting any other value. Surely the boolean type was not a good choice. I don't know whether the error was yours, or the author's, but I'm sure something like int would be a better type to use for a variable that's expected to have values > 255.

Apparently on the platform you're compiling for, boolean is an unsigned 8 bit integer. Since the largest number this type can hold is 255, the variable is never reset to 0. Instead, what happens is it overflows from 255 to 50, from 254 to 49, and so on. Try it with the correct data type and I think you'll find the program works as you expect it to.

Use an int as you had in the original sample you gave us but now removed so my first comment does not make sense... (don’t totally change posts, the idea was just to add a new answer)

Anyway, in Blum's second tutorial, the second half talks about how to use PWM to control the brightness of an LED, basically:

The code you posted includes the statement
boolean ledLevel = 0;This declaration was used when the program in the video was turning an LED on and off but at 17:15 in the video it is noted that when used to control the PWM output it needs to be changed to be an int and the change is made. I suspect that you forgot to do that

Thanks folks, that's what it was. I'm surprised boolean worked at all. Oh well, at least I had fun figuring out a work-around.

pert:
Which board do you have selected?

At least on Arduino AVR Boards core of the Uno, Mega, Leonardo, etc., the boolean data type of the ledlevel variable can only have the values of 0 and 1 so I was surprised that you're getting any other value. Surely the boolean type was not a good choice. I don't know whether the error was yours, or the author's, but I'm sure something like int would be a better type to use for a variable that's expected to have values > 255.

Apparently on the platform you're compiling for, boolean is an unsigned 8 bit integer. Since the largest number this type can hold is 255, the variable is never reset to 0. Instead, what happens is it overflows from 255 to 50, from 254 to 49, and so on. Try it with the correct data type and I think you'll find the program works as you expect it to.

Pert, to answer your questions, this is on a genuine Arduino Uno.

I guess you must be using some old version of Arduino AVR Boards because that's definitely not how boolean works with the current version. Anyway, it's not really a big deal. With boolean, we should only be using true and false. 0 is false, while all non-zero values are true. So as long as people use the type correctly, it doesn't matter whether the numerical values of the boolean type are restricted only to 0 and 1 or not.