Answer the question
In order to leave comments, you need to log in
How to declare a global variable belonging to a class?
Good afternoon.
I decided to create a kind of watchman substitute for the university, which will give calls instead of them on time. For convenience, I described the time class.
Since I will check with timer interrupts, it would be nice to have the current time and the time of the next call in global variables. Or at least the next one. The compiler does not swear, but the piece of iron hangs.
I tried to get out through the pointers, but it did not help much. I have to apologize for the quality of the code.
#include <EEPROM.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> // written by mattt on the Arduino forum and modified by D. Sjunnesson
int incomingByte;
int address = 1;
class time{
private:
int minute;
int hour;
int week;
public:
time () {
hour = RTC.get(DS1307_HR,true);
minute = RTC.get(DS1307_MIN,false);
if ((week = RTC.get(DS1307_DOW,false))==7) week = 0;
else week = 1;
}
time (int a, int b, int c){
hour = a;
minute = b;
week = c;
}
void update () {
hour = RTC.get(DS1307_HR,true);
minute = RTC.get(DS1307_MIN,false);
if ((week = RTC.get(DS1307_DOW,false))==7) week = 0;
}
void output_time (){
Serial.print(hour);
Serial.print(":");
Serial.println(minute);
}
void output_date (){
Serial.print(RTC.get(DS1307_DATE,false));
Serial.print("/");
Serial.print(RTC.get(DS1307_MTH,false));
Serial.print("/");
Serial.println(RTC.get(DS1307_YR,false));
}
friend void next_ring (time& a, time& b){
if (a==b) {
if (address == 47) address = -1;
address++;
b.hour = EEPROM.read(address);
address++;
b.minute = EEPROM.read(address);
}
else {
while(a>b){
if (address == 47){
address = -1;
address++;
b.hour = EEPROM.read(address);
address++;
b.minute = EEPROM.read(address);
break;
}
address++;
b.hour = EEPROM.read(address);
address++;
b.minute = EEPROM.read(address);
}
}
Serial.print("Now - ");
a.output_time();
Serial.print("Next ring - ");
b.output_time();
}
friend int operator== (time& a, time& b) {
return ((a.minute == b.minute) && (a.hour == b.hour) && a.week && b.week);
}
friend int operator> (time& a, time& b) {
if (a.hour>b.hour){
return (1);
}
else {
if (a.hour==b.hour){
return (a.minute>b.minute);
}
else {
return (0);
}
}
}
friend int operator< (time& a, time& b) {
if (a.hour<b.hour){
return (1);
}
else {
if (a.hour==b.hour){
return (a.minute<b.minute);
}
else {
return (0);
}
}
}
};
int led = 13;
int relay = 10;
int t=0;
void set(){
int temp;
Serial.print("Do you really want to change the time? (y/n)");
while (!Serial.available()){
};
incomingByte = Serial.read();
if (incomingByte == 'y'){
RTC.stop();
RTC.set(DS1307_SEC,0);
Serial.print("\r\nEnter day - ");
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = incomingByte - 48;
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = temp * 10 + incomingByte - 48;
RTC.set(DS1307_DATE, temp);
Serial.print("\r\nEnter mounth - ");
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = incomingByte - 48;
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = temp * 10 + incomingByte - 48;
RTC.set(DS1307_MTH, temp);
Serial.print("\r\nEnter year - ");
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = incomingByte - 48;
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = temp * 10 + incomingByte - 48;
RTC.set(DS1307_YR, temp);
Serial.print("\r\nEnter day of week (1-monday, ..., 7-sunday) - ");
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = incomingByte - 48;
RTC.set(DS1307_DOW, temp);
Serial.print("\r\nEnter hour - ");
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = incomingByte - 48;
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = temp * 10 + incomingByte - 48;
RTC.set(DS1307_HR, temp);
Serial.print("\r\nEnter minute - ");
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = incomingByte - 48;
while (!Serial.available()){
};
incomingByte = Serial.read();
Serial.print(incomingByte-48);
temp = temp * 10 + incomingByte - 48;
RTC.set(DS1307_MIN, temp);
RTC.start();
}
Serial.println();
Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
Serial.print(":");
Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
Serial.print(":");
Serial.print(RTC.get(DS1307_SEC,false));//read seconds
Serial.print(" "); // some space for a more happy life
Serial.print(RTC.get(DS1307_DATE,false));//read date
Serial.print("/");
Serial.print(RTC.get(DS1307_MTH,false));//read month
Serial.print("/");
Serial.print(RTC.get(DS1307_YR,false)); //read year
Serial.println();
}
time* now_address;
time* next_address;
void ring (){
Serial.print("Ring! ");
Serial.print(RTC.get(DS1307_HR,false));
Serial.print(":");
Serial.println(RTC.get(DS1307_MIN,false));
//now.output_time();
digitalWrite(relay, HIGH);
delay(1000);
digitalWrite(relay, LOW);
}
ISR(TIMER2_OVF_vect)
{
if (t==50) {
digitalWrite(led, !digitalRead(led));
if (*now_address == *next_address) ring();
now_address->output_time();
t = 0;
}
else {
t++;
}
}
void setup()
{
Serial.begin(9600);
TCCR2A = 0x00; // нормальный режим таймера
TCCR2B |= (1<<CS22) | (1<<CS21) | (1<<CS20); //предделитель на 1024
TIMSK2 |= 1<<TOIE2; // прерывание по переполнению
pinMode(led, OUTPUT);
}
void loop()
{
time now;
time next (EEPROM.read(0), EEPROM.read(1), 1);
now_address = &now;
next_address = &next;
next_ring(now, next);
while (true){
while (!Serial.available()){
delay (100);
};
switch ((incomingByte = Serial.read())) {
case 'h':
Serial.print("h - help\r\ns - set time\r\nt - time\r\nd - date\r\nr - ring!\r\nn - next ring\r\n");
break;
case 's':
set();
now.update();
next_ring(now, next);
break;
case 't':
now.update();
now.output_time();
break;
case 'd':
now.output_date();
break;
case 'r':
ring();
break;
case 'n':
next_ring(now, next);
break;
default:
Serial.print("error\r\nh - help\r\ns - set time\r\nt - time\r\nd - date\r\nr - ring!\r\nn - next ring\r\n");
};
}
}
Answer the question
In order to leave comments, you need to log in
You have a fucking delay inside the ring() that you call from the ISR. In general, doing work in an ISR is bad form, check boxes, queue jobs and do work in the main loop.
And one more thing: are you sure that now_address and next_address will be set before interrupts are enabled?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question