Trinket_Meter.ino 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Adafruit Trinket analog meter clock
  2. // Date and time functions using a DS1307 RTC connected via I2C and the TinyWireM lib
  3. // Download these libraries from Adafruit's Github repository and install in Arduino Libraries
  4. #include <TinyWireM.h>
  5. #include <TinyRTClib.h>
  6. //For debug, uncomment serial code, use a FTDI Friend with its RX pin connected to Pin 3
  7. // You will need a terminal program (such as freeware PuTTY for Windows) set to the
  8. // USB port of the FTDI friend at 9600 baud. Uncomment out Serial commands to see what's up
  9. //#include <SendOnlySoftwareSerial.h> // See http://forum.arduino.cc/index.php?topic=112013.0
  10. #define HOUR_PIN 1 // Hour display via PWM on Trinket GPIO #1
  11. #define MINUTE_PIN 4 // Minute display via PWM on Trinket GPIO #4 (via Timer 1 calls)
  12. //SendOnlySoftwareSerial Serial(3); // Serial transmission on Trinket Pin 3
  13. RTC_DS1307 rtc; // Set up real time clock
  14. void setup () {
  15. pinMode(HOUR_PIN, OUTPUT); // define PWM meter pins as outputs
  16. pinMode(MINUTE_PIN, OUTPUT);
  17. PWM4_init(); // Set timer 1 to work PWM on Trinket Pin 4
  18. TinyWireM.begin(); // Begin I2C
  19. rtc.begin(); // Begin DS1307 real time clock
  20. //Serial.begin(9600); // Begin Serial Monitor at 9600 baud
  21. if (! rtc.isrunning()) { // Uncomment lines below first use of clock to set time
  22. //Serial.println("RTC is NOT running!");
  23. // following line sets the RTC to the date & time this sketch was compiled
  24. //rtc.adjust(DateTime(__DATE__, __TIME__));
  25. }
  26. }
  27. void loop () {
  28. uint8_t hourvalue, minutevalue;
  29. uint8_t hourvoltage, minutevoltage;
  30. DateTime now = rtc.now(); // Get the RTC info
  31. hourvalue = now.hour(); // Get the hour
  32. if(hourvalue > 12) hourvalue -= 12; // This clock is 12 hour, is 13-24, convert to 1-12
  33. minutevalue = now.minute(); // Get the minutes
  34. // if you have calibration issues, you can change the last two values (zero higher, 255 lower)
  35. // to have the needle move less if your scale is not pasted on 100% straight.
  36. hourvoltage = map(hourvalue, 0, 12, 0, 255); // Convert hour to PWM duty cycle
  37. minutevoltage = map(minutevalue, 0, 60, 0, 255); // Convert minutes to PWM duty cycle
  38. /*
  39. // Uncomment out this and other serial code to check that your clock is working.
  40. Serial.print(now.year(), DEC);
  41. Serial.print('/');
  42. Serial.print(now.month(), DEC);
  43. Serial.print('/');
  44. Serial.print(now.day(), DEC);
  45. Serial.print(' ');
  46. Serial.print(now.hour(), DEC);
  47. Serial.print(':');
  48. Serial.print(now.minute(), DEC);
  49. Serial.print(':');
  50. Serial.print(now.second(), DEC);
  51. Serial.print(" - ");
  52. Serial.print(hourvoltage, DEC);
  53. Serial.print(' ');
  54. Serial.print(minutevoltage, DEC);
  55. Serial.println();
  56. */
  57. analogWrite(HOUR_PIN, hourvoltage);
  58. analogWrite4(minutevoltage);
  59. // code to put the processor to sleep might be preferable - we will delay
  60. delay(5000); // check time every 5 seconds. You can change this.
  61. }
  62. void PWM4_init() {
  63. // Set up PWM on Trinket GPIO #4 (PB4, pin 3) using Timer 1
  64. TCCR1 = _BV (CS10); // no prescaler
  65. GTCCR = _BV (COM1B1) | _BV (PWM1B); // clear OC1B on compare
  66. OCR1B = 127; // duty cycle initialize to 50%
  67. OCR1C = 255; // frequency
  68. }
  69. // Function to allow analogWrite on Trinket GPIO #4
  70. void analogWrite4(uint8_t duty_value) {
  71. OCR1B = duty_value; // duty may be 0 to 255 (0 to 100%)
  72. }