You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

156 lines
3.4 KiB

  1. /* Sunrise Alarm Clock */
  2. //Including required libraries
  3. #include <avr/io.h>
  4. #include <util/delay.h>
  5. #include <avr/interrupt.h>
  6. #include "USART.h"
  7. #include "pinDefines.h"
  8. //Defining buzzer related ports
  9. #define ABuzz PB0
  10. #define ABuzz_DDR DDRB
  11. #define ABuzz_PORT PORTB
  12. //Defining bit changer for buzzer
  13. #define setBit(sfr, bit) (_SFR_BYTE(sfr) |= (1 << bit))
  14. #define clearBit(sfr, bit) (_SFR_BYTE(sfr) &= ~(1 << bit))
  15. #define toggleBit(sfr, bit) (_SFR_BYTE(sfr) ^= (1 << bit))
  16. //Declaring and initializing global variables
  17. volatile int enabled = 1;
  18. int printed = 0;
  19. uint8_t sensitivity;
  20. int delay;
  21. void setSensitivity();
  22. void setDelay();
  23. //Button interrupt
  24. ISR(PCINT2_vect)
  25. {
  26. //Check if it the alarm is on
  27. if (bit_is_set(ABuzz_PORT, ABuzz))
  28. {
  29. //Turn the buzzer off
  30. clearBit(ABuzz_PORT, ABuzz);
  31. //Stop the loop
  32. enabled = 0;
  33. }
  34. else
  35. {
  36. /* I found this to be too annoying personally so I decided to comment it out
  37. //Ask user if to reset sensitivity
  38. printString("Would you like to reset the sensor's sensitivity? (Yes = 1)\n");
  39. uint8_t toReset = getNumber();
  40. receiveByte();
  41. if (toReset == 1) { setSensitivity(); }
  42. //Ask user if to reset delay
  43. printString("Would you like to reset the interrupt's delay? (Yes = 1)\n");
  44. toReset = getNumber();
  45. receiveByte();
  46. if (toReset == 1) { setDelay(); }
  47. */
  48. //Set and print success message
  49. enabled = 1;
  50. printString("You're all set! Off to sleep!\n");
  51. }
  52. /* Attempted to use delay to skip the second interrupt input but it didn't
  53. work as I hoped. So I decided to embrace it as a feature instead.
  54. */
  55. //Delay between interrupts to turn off alarm
  56. for(int i = 0; i < delay; i++)
  57. {
  58. _delay_ms(10);
  59. }
  60. //Reset the sensor to run again
  61. printed = 0;
  62. PCIFR |= (1 << PCIF2);
  63. }
  64. int main(void)
  65. {
  66. //Setting up for light sensor
  67. ADMUX |= (1 << REFS0);
  68. ADCSRA |= (1 << ADPS2);
  69. ADCSRA |= (1 << ADEN);
  70. //Setting up the button interrupt
  71. PCICR |= (1 << PCIE2);
  72. PCMSK2 |= (1 << PD2);
  73. MCUCR |= (1 << PUD);
  74. sei();
  75. //Initializing USART
  76. initUSART();
  77. //Print title to string
  78. printString("---- Sunrise Alarm Clock ------\r\n");
  79. //Declaring variables
  80. uint16_t adcValue;
  81. uint8_t brightness;
  82. //Initialize buzzer
  83. setBit(ABuzz_DDR, ABuzz);
  84. setSensitivity();
  85. setDelay();
  86. //Main Loop
  87. while (1)
  88. {
  89. if (enabled > 0)
  90. {
  91. //Start ADC conversion
  92. ADCSRA |= (1 << ADSC);
  93. //Wait for the conversion to complete
  94. loop_until_bit_is_clear(ADCSRA, ADSC);
  95. //Get the ADC value
  96. adcValue = ADC;
  97. //Map it into a scale of 7
  98. brightness = (adcValue >> 7);
  99. //Check if the light is brighter than the selected sensitivity
  100. if (brightness > sensitivity)
  101. {
  102. if (printed == 0)
  103. {
  104. setBit(ABuzz_PORT, ABuzz);
  105. printString("WAKEUP!!!!\n");
  106. printed = 1;
  107. }
  108. }
  109. }
  110. }
  111. return 0;
  112. }
  113. void setSensitivity()
  114. {
  115. do
  116. {
  117. //Get sensitivity input from user
  118. printString("What sensitivity do you want it to be? (Scale of 1 - 6)\n");
  119. sensitivity = getNumber();
  120. receiveByte();
  121. } while (sensitivity > 6 || sensitivity < 1);
  122. }
  123. void setDelay()
  124. {
  125. do
  126. {
  127. //Get delay input from user
  128. printString("How long do you want the delay to be? (1 = 10ms, Scale of 0 - 200)\n");
  129. delay = getNumber();
  130. receiveByte();
  131. } while (delay > 200 || delay < 0);
  132. }