Newb with Syntax understanding question.

I am very new and apologize if my terminology is not correct. I literally just started learning last night. I am using some tutorials on you tube to learn. Thank you to Jeremy Blum :slight_smile:

My Question is regarding this code. (It lets you push a button to PWM control an LED Brightness)

int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;

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

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

}

void loop()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
ledLevel = ledLevel + 51;
}
lastButton = currentButton;

if (ledLevel > 255) ledLevel = 0;
analogWrite(ledPin, ledLevel);
}

I pretty much understand everything that is going on here and it is working fine. There is just a few things that confuse me. Pretty much all regarding the Boolean section.

I get that it is creating a Boolean called debounce and that it watches the switch until the value stabilizes and then returns it. What confuses me is that after it declares a Boolean named debounce it then seems to add another one named last? Is this just a way to name 2 at the same time and nest one inside the other? once inside the Boolean it also names another one named current it looks like. Why are they named in 2 different places instead of both at the beginning where it names debounce and last? Also...it never seems to set an initial value of last to high anywhere in the code but it must be so or the program would not work? Area all Boolean operators automatically high unless you give them an initial value?

Thanks everyone for your help. I hope to come here for all my q's as it seems there are not many Arduino forums.

boolean debounce (boolean last)
{
}

Is declaring a function called debounce. When you call that function you must supply a boolean parameter, that will be called last when it is referred to in the function.

Seems like your saying that the last part is somehow defining how the debounce Boolean will work. I don;t quite understand yet. Could you give an example please? Or point me to a tutorial somewhere that explains it? I looked through the free book but did not see anything.

Thank You

read about functions and how you can move data in/out of them.

In the example you posted I think I understand what is happening.

A function called myMultiplyFunction is being created and the 2 variables x and y both of type int are prerequisites to feed into this function from somewhere else.
Yes?

Inside the function it basicly creates a varibale called result and defines that value as X multiplied by Y then says to return the answer back out.

yes?

If I wanted that answer to use in the program somewhere would I call for the value of myMultiplyfunction? or for the value of result?

Also....In the example I originaly asked about, If it is requiring last as an input to the function there is a problem because that value is not defined anywhere. Yet the program works fine. I do not believe last and lastButton are the same thing are they?

Thank You

jarrod0987:
In the example you posted I think I understand what is happening.

A function called myMultiplyFunction is being created and the 2 variables x and y both of type int are prerequisites to feed into this function from somewhere else.
Yes?

Inside the function it basicly creates a varibale called result and defines that value as X multiplied by Y then says to return the answer back out.

yes?

If I wanted that answer to use in the program somewhere would I call for the value of myMultiplyfunction? or for the value of result?

Also....In the example I originaly asked about, If it is requiring last as an input to the function there is a problem because that value is not defined anywhere. Yet the program works fine. I do not believe last and lastButton are the same thing are they?

Thank You

you are getting it, yes. In your example:

currentButton = debounce(lastButton);

is saying something like "send the value of lastButton to the debounce() function and assign its return to currentButton"

boolean debounce (boolean last)

is saying "in this function, I will return an bool. I will assign the name last to what is sent to me and I expect that last is a bool."

return current;

here it is saying: "I'm done here and I will return the present value of current to the point in the program that called me to do this work"

something like that...

I hope it helps

OK, regarding

boolean debounce (boolean last)

If I understand what you are saying....Your saying that we create this variable named debounce. when we ask for it....what ever value we get we will assign that value as a variable named last?

In the example this makes no sense because there is no purpose. We never use a variable called last anywhere. only lastButton. I am wondering if it is a typo in the code that was supposed to say lastbutton the whole time and for some reason just works anyways?

EDIT: Ok so we do use it inside the Boolean named debounce but we never defined it as a variable we just started referring to it suddenly. Also we never assigned it an initial value so how are we suddenly comparing it to variable current? What is the default value of a undefined variable?

Get yourself an elementary C/C++ textbook, and read it, rather than wasting your own time making random guesses and assumptions.

I've been reading and trying to understand. Most of what I'm seeing doesn't look anything like this.

couple little hints.

in the ide, tools,. autoformat. it will help you with the visualization.

also...

look at item #7

this will help us more than you, but it is how we like it.

jarrod0987:
We never use a variable called last anywhere. only lastButton. I am wondering if it is a typo in the code that was supposed to say lastbutton the whole time and for some reason just works anyways?

EDIT: Ok so we do use it inside the Boolean named debounce but we never defined it as a variable we just started referring to it suddenly.

it is used ONLY in the function, right here:

if (last != current)

and you did declare it right here:

boolean debounce (boolean last)

more specifically:

 (boolean last)

that's it, that's all, nowhere else...

learn about the scope of variables too...

there are other ways to do this too... the programmer who wrote your sketch did it that way... it doesn't mean everyone would have made those same choices.

OK so we basically declared the last function at the same time that we declared the denounce function and basically made it inside the other one.

boolean debounce(boolean last)

Yes?

But yet we waited until the next line to declare current as a variable.

boolean current = digitalRead(switchPin);

Yes?

I guess it doesn't matter which of the 2 places you do it as long as you do it?

The other thing that confuses me is that we say that we are going to get the value of the variable current from the switch pin which of course makes sense. We say that if it is not the same as the value of the variable last then we are going to wait a bit. However, we never said what the initial value of the variable last was going to be? Do all Boolean variables have a default value if you do not set them?

Oh...I read the thing about scope. That makes sense. Makes it only available to the debounce function.

Thank You

  pinMode (onboardled, OUTPUT);

Not sure how it works fine, onboardled is not declared anywhere. It can't compile in that state.

Here is how I would do it. You'll see many similarities except I haven't made extra functions and the debouncing is done by timing rather than delay. You can change the debounce time by changing the constant. I hope the variable names I have chosen make what's happening clear. It also uses internal resistors but could use external with only two minor changes.

const byte SWITCH_PIN = 8;
const byte LED_PIN = 11;
const unsigned long DEBOUNCE_MS = 50UL;

void setup()
{
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
  static int ledLevel = 0;
  static unsigned long debounceStartMS = 0;
  static int switchPinState = !digitalRead(SWITCH_PIN);

  int switchPinSignal = digitalRead(SWITCH_PIN);
  unsigned long nowMS = millis();

  if (switchPinState != switchPinSignal)
  {
    switchPinState = switchPinSignal;
    debounceStartMS = nowMS;
  }

  if (debounceStartMS && nowMS - debounceStartMS >= DEBOUNCE_MS)
  {
    debounceStartMS = 0;
    if (switchPinState == LOW)
    {
      ledLevel += 51;
      if (ledLevel >= 255) ledLevel = 0;
      analogWrite(LED_PIN, ledLevel);
    }
  }
}

A static variable is persistent just like a global except it is scoped only to the function in which it is declared. If you want to learn C++ it is imperative that you understand variable types and scope.

I haven't tried that particular code but it should do the trick.

The answer to the question is in how functions are declared. the first word , boolean is the return type or the type of variable that will be returned. If nothing is returned the function is declared as void see both loop() and setup() as examples. The return type can be almost any variable type, long, char, unsigned int, but they can't be an array or structure. Then you get the function name that you decide on. The part after the name in brackets declares variable types that will be passed to the function. That int last will be created when the function is called, it will be whatever value is passed and will disappear when the function ends. Again, variable types and scope.

Pay no attention to the onboardLED part LOL .I deleted that out of the code and thought I deleted it out of the post too :slight_smile:

jarrod0987:
OK so we basically declared the last function at the same time that we declared the denounce function and basically made it inside the other one.
Yes?

yes

But yet we waited until the next line to declare current as a variable.
Yes?

yes

I guess it doesn't matter which of the 2 places you do it as long as you do it?

oh, it really matters, but in your example remember you are passing a variable that is why one declaration was placed within the initial function declaration.

The other thing that confuses me is that we say that we are going to get the value of the variable current from the switch pin which of course makes sense. We say that if it is not the same as the value of the variable last then we are going to wait a bit. However, we never said what the initial value of the variable last was going to be? Do all Boolean variables have a default value if you do not set them?

yes, by default it will be set to zero, but when a function receives a number, it will be the value of the number it receives...

Oh...I read the thing about scope. That makes sense. Makes it only available to the debounce function.

Thank You

OK I think I have got it. the debounce function wasn't being triggered by the part of the signal I thought it was.

The default value of last is 0 so as soon as the button is pushed and becomes a 1 the first time it triggers the delay and then reads the switchPin again and returns that value.

Yes?

oh, it really matters, but in your example remember you are passing a variable that is why one declaration was placed within the initial function declaration.

Ummm..What?? LOL Sorry. I R NEWB

currentButton = debounce(lastButton);

As far as this part goes it is just saying that the value of current button is the value of last button after it has been run through the debounce function? In this way we could now just debounce any button we wanted. Very convenient :smiley:

Yes?

yes

have fun learning

Thank You.