Advertisement
Guest User

maxEmbedded - How to build an IR sensor

a guest
Aug 20th, 2013
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. // Post: "How to build an IR sensor"
  2. // URL: http://maxembedded.com/2013/08/04/how-to-build-an-ir-sensor/
  3. // Author: Vishwam Aggarwal, VIT University
  4.  
  5. /*  DISCLAIMER
  6.  
  7.     This is the same code as given in the above URL. Though the code is tested and approved, the author
  8.     is not responsible for any damage, physical, mental or social, which might occur due to the implementation
  9.     of this code and its derivatives, whether in full or part thereof. Use it at your own risk.
  10.  
  11.     LICENSE
  12.    
  13.     The user is free to modify, reuse, reproduce and distribute the code and its derivatives to any extent,
  14.     whether for free or paid. The user can also use this code for commercial purposes. In other words,
  15.     do whatever you want with the code!
  16.    
  17. */
  18.  
  19.  
  20. #define F_CPU 1000000UL
  21.  
  22. #include <avr/io.h>
  23.  
  24. int ch_prime(int); //Prototyping the function for testing a number to be prime or not
  25.  
  26. int main(void)
  27. {
  28.           uint8_t num=0, res; //Creating variables 'num' & 'res' for storing the number of detections & result of the function respectively
  29.           DDRA &= ~(1<<0); //Setting 0th pin of Port A as input
  30.           DDRD |= (1<<0); //Setting 0th pin of Port D as output
  31.  
  32.           while(1)
  33.           {
  34.                    if (bit_is_set(PINA, 0)) //Checking if the sensor has detected something in front of it
  35.                    {
  36.                             while (bit_is_set(PINA, 0)); // We use the 'While' function to make sure that one detection increments the value of num by only one value
  37.                             res = ch_prime(++num); //Incrementing the value of num and sending it to the function which tests it for being a prime number
  38.  
  39.                             if (res==1)
  40.                             PORTD |= (1<<PIND0); //If returned value is 1, indicate prime number by pulling the 0th pin on Port D High
  41.                             else
  42.                             PORTD &= ~(1<<PIND0); //If returned value is 0, indicate composite number by pulling the 0th pin on Port D Low
  43.                    }
  44.                    else
  45.                    continue; //If sensor has nothing in front of it, continue detecting
  46.           }
  47. }
  48.  
  49. int ch_prime(int num)
  50. {
  51.           int i; //Declaring variable 'i' to use in the for loop
  52.           for (i=2;i<num;i++) //Run the loop for the value of i from '2' to 'num-1'
  53.           {
  54.                    if (num%i==0) //Check and compare to 0 the remainder when dividing num by i, if 0, then composite number
  55.                    return(0); //If Number is composite, return 0
  56.           }
  57.           if (i==num) //Compare the value of i to num, if same, then prime number
  58.           return(1); //If number is prime, return 1
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement