Advertisement
digigram

RF_Transmitter_328

Jun 23rd, 2014
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <VirtualWire.h>
  2. #include <avr/sleep.h>
  3. #include <avr/interrupt.h>
  4. #include <avr/power.h>
  5. #include <avr/io.h>
  6.  
  7. /*
  8. rf_sender
  9.  This sketch will send the code for toggling a specific light.
  10.  Connect rf module on pin 13 (19 on Atmega). Switch on pin 2 (4).
  11.  Some power saving fu thanx to Nick Gammon - www.gammon.com.au
  12.  Note that this remote is extremely vulnerable to a replay attack
  13.  */
  14. const int buttonPin = 2;
  15. const int ledPin = 3;
  16. int buttonState = 0;
  17.  
  18. void setup()
  19. {
  20.   //DDRD &= B00000011;       // set Arduino pins 2 to 7 as inputs, leaves 0 & 1 (RX & TX) as is
  21.   //DDRB = B00000000;        // set pins 8 to 13 as inputs
  22.   //PORTD |= B11111100;      // enable pullups on pins 2 to 7, leave pins 0 and 1 alone
  23.   //PORTB |= B11111111;      // enable pullups on pins 8 to 13
  24.   // Initialize the IO and ISR
  25.   vw_set_tx_pin(13);
  26.   vw_setup(2000); // Bits per sec
  27.   pinMode(buttonPin, INPUT);
  28.   pinMode(ledPin, OUTPUT);
  29.   // disable ADC
  30.   //ADCSRA = 0;  
  31. }
  32.  
  33. void pinInterrupt(void)
  34. {
  35.   detachInterrupt(0);  
  36. }
  37.  
  38. void sleepNow(void)
  39. {
  40.   attachInterrupt(0, toggleMe, RISING);
  41.   delay(200);
  42.   set_sleep_mode (SLEEP_MODE_PWR_DOWN);  
  43.   sleep_enable();
  44.   sleep_mode();  //or sleep_mode()
  45.   sleep_disable();
  46. }
  47.  
  48. void send (char *message)
  49. {
  50.   vw_send((uint8_t *)message, strlen(message));
  51.   vw_wait_tx(); // Wait until the whole message is gone
  52. }
  53.  
  54. void toggleMe(void)
  55. {
  56.     send("L01");
  57.     digitalWrite(ledPin, HIGH);
  58.     delay(100);
  59.     digitalWrite(ledPin, LOW);
  60.     delay(200);
  61.     digitalWrite(ledPin, HIGH);
  62.     delay(200);
  63.     digitalWrite(ledPin, LOW);
  64.     delay(200);
  65.     digitalWrite(ledPin, HIGH);
  66.     delay(100);
  67.     digitalWrite(ledPin, LOW);  
  68. }
  69.  
  70. void loop()
  71. {
  72.   sleepNow();
  73.   //buttonState = digitalRead(buttonPin);
  74.   //if (buttonState == HIGH) {    
  75.     toggleMe();
  76.   //}
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement