This commit is contained in:
ivan 2017-06-18 16:38:59 +03:00
commit e8cedd2725
3 changed files with 161 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
twobuttonstimer scetch for Arduino

44
timetoserial.py Normal file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env python
# https://forum.arduino.cc/index.php?topic=92457.msg2803014#msg2803014
import time
import serial.tools.list_ports
import serial
import signal # For trapping ctrl-c or SIGINT
import sys # For exiting program with exit code
def SIGINT_handler(signal, frame):
print('Quitting program!')
ser.close()
sys.exit(0)
signal.signal(signal.SIGINT, SIGINT_handler)
port_names=[]
a=serial.tools.list_ports.comports()
for w in a:
port_names.append(w.device)
port_names.sort()
print('\nDetected the following serial ports:\nDon\'t choose /dev/ttyAMA0.')
i=0
for w in port_names:
print('%d) %s' %(i,w))
i=i+1
total_ports=i # now i= total ports
user_port_selection=input('\nSelect port: (0,1,2...)')
if (int(user_port_selection)>=total_ports):
exit(1) # port selection out of range
ser=serial.Serial(port=port_names[int(user_port_selection)],baudrate=9600,timeout=1)
# ser=serial.Serial("COM1",baudrate=9600,timeout=10)
while(1):
mycmd=ser.read()
if (len(mycmd)>0):
# 10800 is GMT+3
epoch='T' + str(int(time.time()) + 10800) + '\n'
ser.write((epoch).encode())
print (str(epoch))

116
twobuttonstimer.ino Normal file
View File

@ -0,0 +1,116 @@
/*
* based on:
*
* TimeSerial.pde
* example code illustrating Time library set through serial port messages.
*
* Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
* you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
* T1357041600
*
* A Processing example sketch to automatically send the messages is included in the download
* On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
*/
#include <TimeLib.h>
#define TIME_HEADER "T" // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
void setup() {
Serial.begin(9600);
while (!Serial) ; // Needed for Leonardo only
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
setSyncProvider( requestSync); //set function to call when sync required
Serial.println("Waiting for sync message");
}
void loop(){
if (Serial.available()) {
processSyncMessage();
}
if (timeStatus()!= timeNotSet) {
digitalClockDisplay();
}
if (timeStatus() == timeSet) {
digitalWrite(13, HIGH); // LED on if synced
} else {
digitalWrite(13, LOW); // LED off if needs refresh
}
checkTime();
delay(10000);
}
void checkTime(){
if ((hour()==6) && (minute()==1)) {
pressSTART();
}
if ((hour()==8) && (minute()==41)) {
pressSTOP();
}
if ((hour()==15) && (minute()==1)) {
pressSTART();
}
if ((hour()==17) && (minute()==31)) {
pressSTOP();
}
}
void pressSTOP(){
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
delay(61000);
}
void pressSTART(){
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(9, LOW); // turn the LED off by making the voltage LOW
delay(61000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
time_t requestSync()
{
Serial.write(TIME_REQUEST);
return 0; // the time will be sent later in response to serial mesg
}