Clap Clap Switch

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there;

 

Firstly sorry about my english. Im new to AVR microcontroller. Im using Arduino mega and im programming it with Atmet studio 7. I'm trying to understand how to use registers by examining the codes in several projects. I find a code about clap switch but I can't understand some thing about code. Let me share code which I found for u. What is the purpose of the variable I painted with yellow color. why did he give that variable a value of 1 in the external "ISR" interrupt. I'd appreciate it if you could enlighten me on this.

 

* Copyright (c) 2019, Łukasz Marcin Podkalicki <lpodkalicki@gmail.com>
  * ATtiny13/050
  * Clap-Clap Switch.
  */
   
  #include <avr/io.h>
  #include <avr/interrupt.h>
  #include <util/delay.h>
   
  #define LED_PIN PB0
  #define MIC_PIN PB1
   
  static volatile uint8_t state = 0;
   
  int
  main(void)
  {
  uint8_t reset = 0, claps = 0;
   
  /* setup */
  DDRB |= _BV(LED_PIN); // set LED pin as output
  MCUCR &= ~(_BV(ISC01)|_BV(ISC00)); // trigger INT0 interrupt when low level detected
  GIMSK |= _BV(INT0); // enable INT0 interrupt
  sei(); // enable global interrupts
   
  /* loop */
  while (1) {
  /* if single clap detected then start procedure */
  if (state) {
  claps++; // increment claps count
  if (claps == 1) { // if it's first clap,
  reset = 0// set reset counter to zero
  else if (claps == 2) { // if it's second clap,
  PORTB ^= _BV(LED_PIN); // toggle LED on/off
  claps = reset = 0// and set initial values
  }
  state = 0// for each clap detection, reset state
  _delay_ms(200); // and wait 0.2s.
  }
  /* if rest counter is max (1s) then set initial values */
  if (++reset >= 10) {
  claps = state = reset = 0;
  }
  _delay_ms(100); // loop delay
  }
  }
   
  ISR(INT0_vect)
  {
   
  state = 1;
  }
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It looks like "state" is a flag that is set when the micro "hears" a clap sound from the microphone connected to the INT0 pin of the micro.

Note it is a "volatile" variable because it is used in both the interrupt service routine() as well as main() and its value can change at any time.

Hope that helps.

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank u for your comment. Can I ask one more question.  I couldn't understand exactly what I wanted to do with the line of code I shared in the appendix. Is the value of the state variable "1" in the ISR function because it is desired to be set each time an interrupt occurs?

if (++reset >= 10) {
  claps = state = reset = 0;
  }
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
It looks like "state" is a flag that is set when the micro "hears" a clap

Indeed.

 

Not a very helpful or informative name!

And this shows why you should comment your variable declarations with a description of what they do.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

atahan wrote:
Is the value of the state variable "1" in the ISR function because it is desired to be set each time an interrupt occurs?

It's just a binary flag - it is either set, or it isn't.

 

Please see Tip #1 on how to properly post source code.

 

atahan wrote:
the appendix

What "appendix" ?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/**
 * Copyright (c) 2019, Łukasz Marcin Podkalicki <lpodkalicki@gmail.com>
 * ATtiny13/050
 * Clap-Clap Switch.
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define LED_PIN PB0
#define MIC_PIN PB1

static volatile uint8_t state = 0;

int
main(void)
{
    uint8_t reset = 0, claps = 0;

    /* setup */
    DDRB |= _BV(LED_PIN); // set LED pin as output
    MCUCR &= ~(_BV(ISC01)|_BV(ISC00)); // trigger INT0 interrupt when low level detected
    GIMSK |= _BV(INT0); // enable INT0 interrupt
    sei(); // enable global interrupts

    /* loop */
    while (1) {
        /* if single clap detected then start procedure */
        if (state) {
            claps++; // increment claps count
            if (claps == 1) { // if it's first clap,
                reset = 0; // set reset counter to zero
            } else if (claps == 2) { // if it's second clap,
                PORTB ^= _BV(LED_PIN); // toggle LED on/off
                claps = reset = 0; // and set initial values
            }
            state = 0; // for each clap detection, reset state
           _delay_ms(200); // and wait 0.2s
        }
        /* if rest counter is max (1s) then set initial values */
        if (++reset >= 10) {
            claps = state = reset = 0;
        }
        _delay_ms(100); // loop delay
    }
}

ISR(INT0_vect)
{

    state = 1;
}

I reshared the source code as you suggested.  The word "appendix" means additional. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your use of the word 'appendix' doesn't quite mean what you wanted to english speakers. Nevertheless, what you intended to say is clear now.

 

 

With the code you posted, the ISR is superfluous - it adds no real value. The writer could have simply tested the interrupt flag rather than using 'state'. eg:

/**
 * Copyright (c) 2019, Łukasz Marcin Podkalicki <lpodkalicki@gmail.com>
 * ATtiny13/050
 * Clap-Clap Switch.
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define LED_PIN PB0
#define MIC_PIN PB1

int main(void)
{
    uint8_t reset = 0, claps = 0;

    /* setup */
    DDRB |= _BV(LED_PIN); // set LED pin as output
    MCUCR &= ~(_BV(ISC01)|_BV(ISC00)); // trigger INT0 interrupt when low level detected

    sei(); // enable global interrupts

    /* loop */
    while (1) {
        /* if single clap detected then start procedure */
        if (GIFR & (1<<INTF0))    //was there a change on the INT0 pin?
            {
            GIFR = (1<<INTF0); reset the interrupt flag

            claps++; // increment claps count

            if (claps == 1)
                { // if it's first clap,
                reset = 0; // set reset counter to zero
                }

            else if (claps == 2)
                { // if it's second clap,
                PORTB ^= _BV(LED_PIN); // toggle LED on/off
                claps = reset = 0; // and set initial values
                }

           _delay_ms(200); // and wait 0.2s
            }

        /* if rest counter is max (1s) then set initial values */
        if (++reset >= 10)
            {
            claps = state = reset = 0;
            }
        _delay_ms(100); // loop delay
    }
}

 

Last Edited: Sat. Oct 19, 2019 - 11:10 AM