Newer
Older
SafeCrack / template.cpp
root on 10 May 2019 3 KB documentation
  1. #include <avr/sleep.h>
  2. #include <avr/interrupt.h>
  3. #include <SoftwareSerial.h>
  4.  
  5. const int rx = -1; // -1 = dont use
  6. const int tx = 5; // pin 5 for serial.print()
  7. SoftwareSerial Serial(rx,tx); // rx, tx pins
  8.  
  9. int enc1pinA = 2; // Encoder pin A
  10. int enc1pinB = 3; // Encoder pin B
  11. int counter = 0; // could use a byte here?
  12. byte enc1oldPins = 0; // will hold state of pins last time encoder was read
  13. byte enc1newPins = 0; // will hold new encoder pin reading, to be compared with last time's pin states
  14. byte enc1move = 0; // when two encoders are used to control the same parameter, this will hold the combined deduced movement
  15.  
  16. const byte RotaryDecodeTable[4][4] = {
  17. {B00, B10, B01, B11},
  18. {B01, B00, B11, B10},
  19. {B10, B11, B00, B01},
  20. {B11, B01, B10, B00}
  21. };
  22.  
  23. int bLed = 1; // rotary encoder LED pin
  24. int rgLed = 4; // red/green LED pin
  25. int val = 0; // rotary encoder current position
  26.  
  27. unsigned long lastCheck = 0; // time (for sleep state)
  28. bool sleepState = 0; // sleep or awake
  29.  
  30. void setup() {
  31. pinMode(bLed, OUTPUT);
  32. pinMode(rgLed, INPUT);
  33.  
  34. pinMode(enc1pinA, INPUT); // configure the pin connections as inputs
  35. pinMode(enc1pinB, INPUT);
  36. pinMode(enc1pinA, INPUT_PULLUP); // turn on internal pull-up resistor for each input
  37. pinMode(enc1pinB, INPUT_PULLUP);
  38. delay(1); // allow 1ms for encoder voltages to setle
  39. enc1oldPins = (digitalRead(enc1pinA)<<1|digitalRead(enc1pinB)); // read the initial state of encoder
  40. lastCheck = millis(); // for sleep state
  41.  
  42. pinMode(rx,INPUT); // setup and print to serial
  43. pinMode(tx,OUTPUT);
  44. Serial.begin(9600);
  45. Serial.println("Running");
  46. }
  47.  
  48. void green(int del){ // turn on green LED for delay()
  49. pinMode(rgLed, OUTPUT);
  50. analogWrite(rgLed, HIGH);
  51. delay(del);
  52. pinMode(rgLed, INPUT);
  53. }
  54. void red(int del){ // turn on red LED for delay()
  55. pinMode(rgLed, OUTPUT);
  56. analogWrite(rgLed, 255);
  57. delay(del);
  58. pinMode(rgLed, INPUT);
  59. }
  60.  
  61. void keepAwake(int dir){ // runs every time rottary encoder is turned to keep deviec awake
  62. lastCheck = millis();
  63. if(sleepState == 1){
  64. sleepState = 0;
  65. }
  66. }
  67.  
  68. void sleep(){
  69. println("Sleeping");
  70. sleepState = 1;
  71. analogWrite(bLed, LOW);
  72.  
  73. GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
  74. PCMSK |= _BV(PCINT2); // Use pin 2 as interrupt pin
  75. PCMSK |= _BV(PCINT3); // Use pin 3 as interrupt pin
  76. PCMSK &= ~_BV(PCINT3); // Turn off pin 3 as interrupt pin
  77. ADCSRA &= ~_BV(ADEN); // ADC off
  78. set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement
  79.  
  80. sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
  81. sei(); // Enable interrupts
  82. sleep_cpu(); // sleep
  83.  
  84. cli(); // Disable interrupts
  85. PCMSK &= ~_BV(PCINT3); // Turn off PB3 as interrupt pin
  86. sleep_disable(); // Clear SE bit
  87. ADCSRA |= _BV(ADEN); // ADC on
  88.  
  89. sei(); // Enable interrupts
  90. }
  91.  
  92. void loop(){
  93. enc1newPins = 0; // Empty byte ready to shift-in new pins
  94. enc1newPins = (digitalRead(enc1pinA)<<1|digitalRead(enc1pinB)); // read the current state of encoder1 pins
  95. enc1move = RotaryDecodeTable[enc1oldPins][enc1newPins]; // used RotaryDecodeTable to decide movement, if any
  96. enc1oldPins = enc1newPins; // update encoder1state to be current pin values
  97. if (enc1move == B01){ // if result was move right (CW), decrement counter
  98. counter--;
  99. keepAwake();
  100. }
  101. if (enc1move == B10){ // if result was move left (anti-CW), inrement counter
  102. counter++;
  103. keepAwake();
  104. }
  105.  
  106. // inactive > 10 secs so go into sleep state
  107. if ( (millis() - lastCheck > 10000) && sleepState == 0 ) { // 10000 == 10 secs
  108. sleep();
  109. }
  110. }
Buy Me A Coffee