SHARE
TWEET

Attiny85 pin change interrupt V2

a guest Apr 15th, 2014 401 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Extremly low power 433MHz switch
  3.  *
  4.  * Button0: Turn off all lights(Switch A,B,D)
  5.  * Button1: Turn on big light(Switch B)
  6.  */
  7. #include <RCSwitch.h>
  8. #include <avr/sleep.h>
  9. #include <avr/wdt.h>
  10.  
  11. // RCSwitch config
  12. int vccPin = 3;
  13. int dataPin = 4;
  14. char systemCode[6] = "01110";
  15. char switchA[6] = "10000";
  16. char switchB[6] = "01000";
  17. char switchC[6] = "00100";
  18. char switchD[6] = "00010";
  19. char switchE[6] = "00001";
  20. RCSwitch mySwitch = RCSwitch();
  21.  
  22. // Switches
  23. int button0 = 0;
  24. int button1 = 1;
  25.  
  26. void setup() {                
  27.   // Turn off interrupts
  28.   cli();
  29.   // Pin to turn on and of transmitter
  30.   pinMode(vccPin, OUTPUT);
  31.   digitalWrite(vccPin, LOW); // Turn off radio    
  32.   delay(500);
  33.  
  34.   // Buttons
  35.   pinMode(button0, INPUT_PULLUP);
  36.   pinMode(button1, INPUT_PULLUP);
  37.  
  38.   // RCSwitch
  39.   mySwitch.enableTransmit(dataPin);
  40.  
  41.   // Energie sparen
  42.   ADCSRA &= ~(1<<ADEN); //Deaktiviere ADC
  43.   ACSR = (1<<ACD); //Deaktiviere Analog Comparator
  44.  
  45.   // Sleepmode: Power-Down
  46.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  47.  
  48.   // Interrupts  
  49.   GIMSK = (1 << PCIE);  //Enable Pin Change Interrupts
  50.   PCMSK = (1 << PCINT0) | (1 << PCINT1); // Enable pin0 and pin1 interrupts
  51.   // Enable interrupts
  52.   sei();
  53. }
  54.  
  55. void loop() {
  56.   executeCommand();
  57.   powerDown();
  58. }
  59.  
  60. void executeCommand() {
  61.   // Button 1?
  62.   if (digitalRead(button0) == LOW) {
  63.     powerUpTransmitter();
  64.     // Turn off all lights(Switch A,B,D)
  65.     mySwitch.switchOff(systemCode, switchA);
  66.     mySwitch.switchOff(systemCode, switchB);
  67.     mySwitch.switchOff(systemCode, switchD);    
  68.   }
  69.   // Button 2?
  70.   else if(digitalRead(button1) == LOW) {
  71.     powerUpTransmitter();
  72.     // Turn on big light(Switch B)
  73.     mySwitch.switchOn(systemCode, switchA);
  74.   }
  75. }
  76.  
  77. void powerUpTransmitter() {
  78.   digitalWrite(vccPin, HIGH);
  79.   // Wait for module to strart
  80.   delay(20);
  81. }
  82.  
  83. void powerDown() {
  84.   GIMSK = (1 << PCIE);       // Enable Pin Change Interrupts
  85.   digitalWrite(vccPin, LOW);  // Turn off transmitter
  86.   sleep_mode();
  87.  
  88.   GIMSK &= ~(1 << PCIE);  // Disable Pin Change Interrupts (Debounce button)
  89. }
  90.  
  91. ISR (PCINT0_vect)       //the PCINT0_vect vector is used to identify the pin change interrupt.
  92. {
  93.    
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top