Last active
February 15, 2021 09:17
-
-
Save t-nissie/7b475315ecc26ad839dccfcc074b044c to your computer and use it in GitHub Desktop.
lifesaver.ino pushes a button every 22 minutes 22 seconds with an Arduino Nano and a servomotor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lifesaver.ino | |
// Author: Takeshi Nishimatsu License: GPL-3.0-only | |
// Description: You may occasionally encounter situations in which you must push a | |
// button every 22 minutes 22 seconds, or you die. This lifesaver.ino saves your | |
// life by pushing a button automatically with an Arduino board and a servomotor. | |
// Wiring: To plug an SG90 servomotor to 6-4-2 pins of the ICSP connector on an | |
// Arduino board, the order of wires from the servomotor should be changed | |
// from GND(brown)-5V(red)-PWM(orange) to GND(brown)-PWM(orange)-5V(red). | |
// SG90 micro servomotor: http://akizukidenshi.com/download/ds/towerpro/SG90_a.pdf | |
// Seeed Grove 4-Digit Display: https://wiki.seeedstudio.com/Grove-4-Digit_Display/ | |
// Homepage: https://gist.github.com/t-nissie/7b475315ecc26ad839dccfcc074b044c | |
// 3D printing: Servo mount bracket https://www.thingiverse.com/thing:1310167 | |
// https://www.youmagine.com/designs/arduino-nano-mount-holder-yet-another | |
// https://www.youmagine.com/designs/mount-holder-for-seeed-grove-4-digit-display | |
// Arduino Nano and SG90 Servo Holder https://www.thingiverse.com/thing:3925535 | |
//// | |
#include <TimerOne.h> | |
#include "TM1637.h" // Seeed's Grove 4-Digit Display | |
unsigned char minute=0; | |
unsigned char second=11; | |
#define SVO 11 //4th pin of the ICSP connector | |
#define LED 13 //On board LED | |
#define CLK 18 //A4 pin definitions | |
#define DIO 19 //A5 for TM1637 | |
TM1637 tm1637(CLK,DIO); | |
bool flag_changed=true; | |
void isr() // interrupt serve routine | |
{ | |
if (second==0) { | |
minute --; | |
second = 59; | |
} else { | |
second --; | |
} | |
flag_changed=true; | |
} | |
void period20ms(int pin, int duty) | |
{ | |
digitalWrite(pin, HIGH); | |
delayMicroseconds(duty); | |
digitalWrite(pin, LOW); | |
delayMicroseconds(16000-duty); // The argument should | |
delayMicroseconds( 4000); // be less than 16384. | |
} | |
void setup() | |
{ | |
Serial.begin(9800); | |
Serial.println("Let's start"); | |
pinMode(SVO,OUTPUT); digitalWrite(SVO,LOW); | |
pinMode(LED,OUTPUT); digitalWrite(LED,LOW); | |
tm1637.set(); | |
tm1637.init(); | |
tm1637.point(POINT_ON); | |
Timer1.initialize(1000000); // timing for 1s | |
Timer1.attachInterrupt(isr); | |
} | |
void loop() | |
{ | |
period20ms(SVO, 900); | |
if (flag_changed) { | |
if (minute<10) Serial.print("0"); | |
Serial.print(minute); | |
if (second<10) Serial.print(":0"); else Serial.print(":"); | |
Serial.print(second); | |
Serial.println(); | |
unsigned char TimeDisp[4]; | |
TimeDisp[0] = minute / 10; | |
TimeDisp[1] = minute % 10; | |
TimeDisp[2] = second / 10; | |
TimeDisp[3] = second % 10; | |
tm1637.display(TimeDisp); | |
flag_changed=false; | |
} | |
if (minute==0 && second==0) { | |
minute=22; | |
second=22; | |
digitalWrite(LED,HIGH); | |
for (int i=0; i<20; ++i) period20ms(SVO, 1800); | |
digitalWrite(LED,LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment