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;
  }

LDR solve

LDR solve
ldr
0% (0 votes)
atmega2560
0% (0 votes)
Total votes: 0
This topic has a solution.
Last Edited: Fri. Oct 18, 2019 - 11:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

atahan wrote:
line of code that I painted in yellow (#define isik 500), what does it mean

That's a 'C' textbook question - nothing specific to AVR:

 

https://publications.gbdirect.co.uk//c_book/chapter7/

 

From that line on, wherever it finds the text "isik", the preprocessor will replace it with the text "500" - just like doing a search-&-replace in your editor.

 

atahan wrote:
that value is chosen according to which criterion

That's the kind of thing which should have been explained by a comment in the code!

 

The original language of the code doesn't seem to be English, so maybe "isik" has some meaning or significance in the original language?

 

The only place it's used is:

        adc_sonuc=adc_oku(0); //PA0'dan okuma yapması için
        
        if(adc_sonuc<isik)
           PORTC|=(1<<PC0) | (1<<PC1) | (1<<PC2);
        else
           PORTC&=~((1<<PC0) | (1<<PC1) | (1<<PC2));    

Where it's being used as a threshold to either set or clear some port bits.

 

This is the trouble with just copying code from the internet!

 

Perhaps you could go back to the site where you found it, and ask the author ?

 

 

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

Thank u for your comment.

 

I translate to code my language. im really new at avr and im really want how to programming.  I read datasheet and i try to some exercise chapter by chapter. I'm trying to learn with codes in algorithms created next to datasheet. Im from Turkey and isik means light and adc_oku =adc_read, adc_sonuc=adc_result

 

I'm not just copying code from internet. I try the code to understand the logic of the registers and note why it is written in my own language.

 

I couldn't tell what I wanted to tell i know define light 500 is threshold value. I decided this value (500) because I constantly tested how the program works when I give that value. I wanna ask to how to set the threshold value of photoresistance? 

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

atahan wrote:
 im really want how to programming.

Learning programming is really  a separate issue from learning the AVR.

 

If you're not already familiar with 'C' programming, it's probably easier to learn that separately on a PC - rather than also trying to learn the AVR at the same time with all the added complications that brings.

 

Here are some 'C' learning/reference materials - including a free online textbook, and links to some tutorials on this very site:

 

http://blog.antronics.co.uk/2011...

 

 

I wanna ask to how to set the threshold value of photoresistance? 

You set it where you want it!

 

What light level do you want? Set the threshold to whatever value corresponds to that level!

 

 

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...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

@atahan

 

Firstly, welcome to AVRFreaks.

 

I think that you are asking "Why 500?". The answer involves a few concepts/ideas.

 

Firstly, the ATMega2560 has an analogue to digital converter that produces a digital value ranging between 0 and 1023. This is described as a 10 bit ADC. The resolution, or actual voltage that each bit represents depends upon the reference voltage selected in your line " ADMUX|=(1<<REFS0); // Referans voltaj değeri Vcc=5V olarak ayarlanır. " In that case you have chosen to use the supply voltage, nominally 5 volts. The actual supply voltage will depend upon the accuracy of your 5 volt supply, but that is a different topic.

 

So the ADC will determine what portion your input voltage is of the reference voltage. So a 1 volt input would be 1/5 of the 5 volt reference or 204 reported by the ADC.

 

I believe that your LDR will be connected so that it is the lower component of a resistor divider between Vcc and ground. The top resistor will have some fixed value. The junction of the LDR and the resistor will connect to the ADC input. When a light shines on the LDR, its resistance lowers and so does the voltage presented to the ADC. The ADC value will also go lower. When the LDR is in the dark, its resistance will be high and so will be the ADC input voltage and its output value.

 

The 500 value is a little less than half the 10 bit maximum value of 1023. So the code is "saying", test the situation when the LDR voltage is above or below approximately 2.5 volts. (actually 2.44 volts). Someone has decided that this voltage matches a suitable light level on that LDR for this test. Other LDRs may have a different value that is suitable. Also the test value will depend upon the value of the fixed resistor in the top  of the voltage divider.

 

I hope that helps to answer your question and guide your experiments.

 

Cheers,

 

Ross

 

Ross McKenzie ValuSoft Melbourne Australia

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

Be sure to use an actual meter to measure , so you KNOW what voltage level is going to the ADC pin, rather than saying I always get 00 or 1023, which are common values when the wiring is not proper.  If you know the voltage at the ADC pin (and ADC ref level), then you can say it is a software problem. 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

valusoft you are best. Thank u so much. I certainly wanted to know why that value was chosen as 500. Now i understand, thanks to you. Thank u for good comment one more time.