13. Arduino for Production! AVR Atmega32 - Understanding Button Debouncing

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ม.ค. 2025

ความคิดเห็น • 72

  • @maisonheidi
    @maisonheidi 6 ปีที่แล้ว +8

    The best Videos on the www i`v ever see for how to learn programming a AVR . Respect and verry thanks how to getting started with all the diverence of Programms like WINAvr or Atmel Studio too. I learn alot thank you for doing videos like this !

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  12 ปีที่แล้ว

    Thanks. It may be set to zero by default, but this is really just a good practice to make sure that it is really set at zero. When the program becomes larger and more complex, this practice may save you time. For example, if the DDRB commands get used in its own function, there may be other functions or places that set this bit to 1, and finding that error would be time consuming.

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  12 ปีที่แล้ว

    Thanks. You can't set a pin high or low if it is set as input. The state of an input pin in changed by some device outside of the chip.

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  12 ปีที่แล้ว

    Just ordered a sample. I will make a tutorial when the sample arrives.

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  13 ปีที่แล้ว

    @somebody301 the PC versions are actually less expensive than what I bought and have better features. The Owon that I purchased was around $400.

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  13 ปีที่แล้ว

    @somebody301 I'm using a cheap Owon oscilloscope. I hear that PC scopes are a great alternative.

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  13 ปีที่แล้ว

    @somebody301 I don't use a wave generator... yet!

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  13 ปีที่แล้ว

    @somebody301 Yeah, I wouldn't invest in anything until you see a need for it. Even an oscilloscope is handy but not an absolute necessity.

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  13 ปีที่แล้ว

    @somebody301 Good tip.
    Another good investment is a logic analyzer. I actually have one of those and it's great to see if serial communication is actually happening. They usually come with many leads to probe multiple legs. I may get into that in the uart tutorial. A good project may even to create one from scratch and show the result on the LCD. Hmm!!

  • @Anonymous-to3jj
    @Anonymous-to3jj 10 ปีที่แล้ว +1

    Ok.... I'm lost I can't find the project where you created the starting code file. So I'm lost on the following lines of code.
    DDRB |= 1

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  12 ปีที่แล้ว

    Thanks.

  • @MrAlexthemachine
    @MrAlexthemachine 13 ปีที่แล้ว

    Okay I have a quick question. Wouldn't a rectifier diode work better for dealing with the reverse voltage caused by a switch?

  • @beepsntones
    @beepsntones 11 ปีที่แล้ว

    I have thought of the NI national instrument that might required me to buy their extra boards for inputs and output for data transfer..

  • @pravineeshkrishna9709
    @pravineeshkrishna9709 3 ปีที่แล้ว

    while (1)
    {
    if (bit_is_clear(PINB,1))
    {
    PORTB ^= 0x05; // pin0 and pin2 toggle
    }
    }
    Is this correct?
    The code works fine for me. In this case, we don't need to use the variable pressed.

  • @cristapia6833
    @cristapia6833 7 ปีที่แล้ว

    What is the size of the capacitor between VCC and GND (power to the uC)?

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  13 ปีที่แล้ว

    @josepaul2000 Canon Rebel T1i.

  • @n3crokun
    @n3crokun 10 ปีที่แล้ว

    I am having trouble understanding how the 2nd if() statement comes
    into play.I realize that its used to prevent continuous toggling by
    making sure the button was released first before toggling.But if i
    were to follow the code systematically im having a hard time visualizing
    how this is done. So if i were to start the program and the button has not
    been pressed, it will fall into the else{} where var "Pressed" will be assigned
    to 0. But if we now press the button, it will now satisfy the first if() condition
    and thus fall into that bracket . it will now check the condition for the 2nd if()
    statement which should be true since var "Pressed" was assigned to 0 on the else{} earlier.
    Therefore now the toggling of the 2 leds occur. This is the part where im having trouble with as I don’t see how the button needs to be released ,since as soon as we press the button both if() conditions are satisfied and the toggling should occur immediately as opposed to waiting for the button to be released before toggling.
    But the code does seem to work as ive tried it, but i just cant grasp how the waiting for release is done.

  • @m4unot
    @m4unot 12 ปีที่แล้ว

    Hi Patrick, are you using 2 or 4 pin button, what the value of the capacitor are you using? and you can actually use true and false if you include the header stdbool.h which is part of the standard C library.

  • @kithinjimuriungi8811
    @kithinjimuriungi8811 8 ปีที่แล้ว +4

    I officially declare you the best :-D

  • @beepsntones
    @beepsntones 11 ปีที่แล้ว

    also what do you recommend if I need to use video recognition any kits out there which works with atmel? thanks

  • @m4unot
    @m4unot 12 ปีที่แล้ว

    It would be Nice if you can show how to use AT42QT1012 as touch button, becouse AT42QT1012 is made with the ability to toggle in touch mode

  • @thejking
    @thejking 13 ปีที่แล้ว

    noob question here.
    Can i use any micro controller with the programming language that you are using?
    (with a different programmer) and if i'm right the micro controller is enough of a resistor to not short the circuit right? since you connected the button to ground.

  • @badyammar2289
    @badyammar2289 7 ปีที่แล้ว

    Thank you so much.. Amazing explanation
    Would you please add numbers to the names of the videos in the playlist so the one can remember where did he arrived

  • @ShaunDobbie
    @ShaunDobbie 12 ปีที่แล้ว

    I end up with one LED being dim and the green one switching off when I press the button.

  • @harshitagarwal5188
    @harshitagarwal5188 9 ปีที่แล้ว

    how does capacitor remove switch debouncing ?

  • @josepaul2000
    @josepaul2000 13 ปีที่แล้ว

    What camera are you using?

  • @aalavandhaann
    @aalavandhaann 12 ปีที่แล้ว

    Anajonesr,
    I have a question. By default the DDRB of all the pins will be 0bit right? Then why do we have to explicitly set the Pin1(switch) to 0bit when it is already 0bit? BTW your videos are great and anybody who has not seen it is not blessed.

  • @aalavandhaann
    @aalavandhaann 12 ปีที่แล้ว

    Thank you anajonesr for the clarification. Your tutorial rocks....

  • @jalaluddinrumi4889
    @jalaluddinrumi4889 6 ปีที่แล้ว +2

    U are awesome.U should have different color of wire.Upload a diagram

  • @beepsntones
    @beepsntones 11 ปีที่แล้ว

    what would happen if you had put a delay in the prob meaning if the switch is pressed wait 10ms then switch/toggle. would that also solve the problem?
    back in the day we had used assembly language that seemed to work that was only 8bit and much smaller micro controller . thanks in advance

    • @PatrickHoodDaniel
      @PatrickHoodDaniel  11 ปีที่แล้ว +1

      Delays will work and many folks do this but I like to keep delays out of the code. It's best to treat programming in microcontrollers like state machines where the clock cycles are there if you need it.

  • @nazatkabir3442
    @nazatkabir3442 7 ปีที่แล้ว

    I have made a program for a CLICK project just like you
    but the problem is not about debounce in my case.After uploading this program the led keeps blinking very fast
    can you point out the problem is in this program??
    #include
    #include
    int b=2;
    int main(void)
    {
    DDRA=0x00;
    DDRC=0xFF;
    while (1)
    {
    int c=b/2;
    int a = PINA;
    _delay_ms(10);
    int a1 = PINA;
    _delay_ms(10);
    if(a!=a1){
    b+=1;
    }
    if(c%2==0){
    PORTC=0xFF;
    }
    else if(c%2==1){
    PORTC=0x00;
    }
    }
    }
    in this program i have made PORTC to be output for the LED.
    I have used ATMEL STUDIO`s GCC C but that should not matter.

  • @nazatkabir3442
    @nazatkabir3442 7 ปีที่แล้ว

    sorry i have made a mistake in coments of the program
    A was set as INPUT
    and
    C was set as OUTPUT

  • @arinjoyhere
    @arinjoyhere 6 ปีที่แล้ว

    Hey..pattrick greetings...may I know why both pins pinb0 and pinb1 are initially zero

    • @PatrickHoodDaniel
      @PatrickHoodDaniel  6 ปีที่แล้ว +1

      If no pin is set high or low, it will default to low (0). However, it is important to set the pins that you are using to your desired level even though there is a default state. This is so you understand their intended state and you are not putting the pin in a tri-state (an input state that is not pulled high or low) condition where interference can cause undesirable results.

    • @arinjoyhere
      @arinjoyhere 6 ปีที่แล้ว +1

      Yeah,,I thought that should be that...thank you...got your confirmation...
      Salutations@patrick

  • @thanhphongphan
    @thanhphongphan 7 ปีที่แล้ว

    Amazing tutorials sir!
    I have a question. I tried writing my code like this: if (bit_is_clear(PINB, 1) && (Pressed == 0)) {...}
    but it didn't toggle when I pressed the button (1 LED keeps turning on and the other keeps flashing every time I pressed)
    I changed my code to be like yours and separate 2 conditions then it works as expected.
    Can you explain what is the problem with using && operator here?
    Thank you! Looking forward for your answer!

    • @PatrickHoodDaniel
      @PatrickHoodDaniel  7 ปีที่แล้ว +1

      Thanks for the kind words. Looks like you may be missing a closing parenthesis. if (bit_is_clear(PINB, 1)*)* && (Pressed == 0))
      Does it work by nesting them? That may answer your question.

    • @thanhphongphan
      @thanhphongphan 7 ปีที่แล้ว

      Sir, I don't think I've missed any closing parenthesis. This is my conditional statement when it didn't work
      if ( bit_is_clear(PINB, 1) && (Pressed == 0) ) {
      PORTB ^= (1

    • @PatrickHoodDaniel
      @PatrickHoodDaniel  7 ปีที่แล้ว +1

      Yes, you are right. Sorry about that. Separating the condition into a nested condition worked. Not sure the && doesn't work. I typically use that logic statement in my C# code, but I don't recollect using it in C.

    • @thanhphongphan
      @thanhphongphan 7 ปีที่แล้ว

      Thank you for your answers. I guess I'll just stick to the nested if statement for the moment ;)

    • @PatrickHoodDaniel
      @PatrickHoodDaniel  7 ปีที่แล้ว +1

      You're welcome. Not sure how helpful I was, however. Let me know if you find a solution with the other option.

  • @Sonia_prc
    @Sonia_prc 7 ปีที่แล้ว

    can u please tell how did u put pinb2 as input?? I didn't get d way u made pinb2 input using bitwise operation..

    • @Sonia_prc
      @Sonia_prc 7 ปีที่แล้ว

      hope u'll solve this query..

    • @PatrickHoodDaniel
      @PatrickHoodDaniel  7 ปีที่แล้ว

      You need to set the data direction register for that port and pin as input.

    • @Sonia_prc
      @Sonia_prc 7 ปีที่แล้ว

      Patrick Hood-Daniel
      ddrb&=~(1

    • @PatrickHoodDaniel
      @PatrickHoodDaniel  7 ปีที่แล้ว +1

      You still want to set it even if it is already 0. It's a good practice because programs get longer and more complicated and you may not know what state that bit is all the time. I would suggest really looking at the program closely and test, test, test.

    • @Sonia_prc
      @Sonia_prc 7 ปีที่แล้ว +1

      Patrick Hood-Daniel
      thnxxxx.... I jst started my engineering... really loved d stuff u doing... keep posting tutorial lyk dis..

  • @Omar.bin.khattab
    @Omar.bin.khattab 2 ปีที่แล้ว +1

    thanks so much for your effort and time ..........

  • @ns5253
    @ns5253 12 ปีที่แล้ว

    @anajonesr
    In the statement -
    PORTB |= 1

  • @ParkourNerd
    @ParkourNerd 12 ปีที่แล้ว

    I'm pretty sure some of the code if wrong. it should be
    1. Pressed_Confidence_Level ++;
    Released_Confidence_Level = 0;
    2. Released_Confidence_Level = 0; if inside the if
    3. if (Released_Confidence_Level > 500) needs a closing brace.

  • @aalavandhaann
    @aalavandhaann 12 ปีที่แล้ว

    Thanks a lot guru I understand now. I will follow as my guru says.

  • @imon4mRUET
    @imon4mRUET 2 ปีที่แล้ว

    Thanks brother.

  • @m4unot
    @m4unot 12 ปีที่แล้ว

    i just realzed that you are using the 2 pins button which is most logical, but keep the good work

  • @Kalec1994
    @Kalec1994 5 ปีที่แล้ว

    best of the best

  • @nazatkabir3442
    @nazatkabir3442 7 ปีที่แล้ว

    yes

  • @sksahil4374
    @sksahil4374 6 ปีที่แล้ว

    Thanks you sir

  • @mgcristi
    @mgcristi 10 ปีที่แล้ว +1

    A boolean doesn't use only 1 bit of memory ! Get it right :D. You can't allocate only 1 bit of memory.

  • @nsutahmid
    @nsutahmid 11 ปีที่แล้ว

    I think u should start from the first video to understand @M.Wali M

  • @nazatkabir3442
    @nazatkabir3442 7 ปีที่แล้ว

    let me elaborate it with the program with comments
    #include
    #include
    int b=2;
    int main(void)
    {
    DDRA=0x00; //here i`ve set all the pins of PORTA as output
    DDRC=0xFF; /here i`ve set all the pins of PORTC as input
    while (1)
    {
    int c=b/2; // the value of b is going to be divided by tow for a further purpose
    int a = PINA; // first of all a is going to get a value
    _delay_ms(10); // in this delay the button will pressed or not pressed
    int a1 = PINA; //here i am getting the value to check if the button is pressed in the delay
    //in this part the if logic is going to work
    //and when i will take my finger off the button the b is going to increase with 1 value again
    //totally the increamention of b will be 2
    // and with the (c=b/2) i am going to get a value of c increased 1
    if(a!=a1){
    b+=1;
    }
    //so on the first click c%2 will be 0 and an another click will make the value of c%2 to 1
    // it is going to go on like this
    if(c%2==0){
    PORTC=0xFF; // if the value of c%2 is 0 the LED will light up
    }
    else if(c%2==1){
    PORTC=0x00; //if the value of c%2 is 1 the LED will turn off
    }
    }
    }
    AND I hope that the program should have worked just fine

    • @PatrickHoodDaniel
      @PatrickHoodDaniel  7 ปีที่แล้ว

      Hi Nazat, I am unfortunately not able to investigate code for others, which is why I make tutorials. the best way to learn how to code embedded applications is to dig deep and trudge through. I am generally focused on making new tutorials and investigating others code would take time out of creating new tutorials.

    • @nazatkabir3442
      @nazatkabir3442 7 ปีที่แล้ว

      okii

  • @PatrickHoodDaniel
    @PatrickHoodDaniel  12 ปีที่แล้ว

    I would not consider myself a guru, but thanks anyway.

  • @m.mohammad4545
    @m.mohammad4545 11 ปีที่แล้ว

    i dont understand nothing