Hello community,
I wrote a program to create an alarm clock! I receive from another chip over the Serial interface the current time + date. It is working however I do this process three times each day. Afterwards I switch off the Serial interface because otherwise I would receive some trash data before I receive the next time+date.
I found out that in the moment the command
Serial.end();
appears the program stop running and do nothing anymore I have to restart the complete system before I can do anything. Can someone explain me the reason for this?
Just a little notice:
I just the SoftSerial library to generate another interface!
I only changed all the Serial with the name with my generated SoftSerial interface. Now it is working.... can someone explain this strange behavoir?
Scanox89:
Just a little notice:
You don't seem to have NOTICED @Delta_G's hint ...
Post your code if you want help. We are not clairvoyant.
If there is a risk of receiving garbage without realizing it is garbage there is obviously something wrong.
...R
update: the SoftSerial command doenst work always same problem like Serial
#include <MsTimer2.h> // Interrupt for minute
#include <Wire.h> // I2C
#include <SPI.h> // SPI (Adafruit lib)
#include <Adafruit_GFX.h> // display
#include <Adafruit_SSD1306.h> // display
#define OLED_RESET 4 //display
Adafruit_SSD1306 display(OLED_RESET); //display
#include <SoftwareSerial.h>
SoftwareSerial DCF(13, 12);
// INPUTS/OUTPUTS
int b_snooze = 5; // digital pin 5
int b_status = 6; // digital pin 6
int b_hour = 7; // digital pin 7
int b_minute = 8; // digital pin 8
int antenna = 3; // digital pin 3
int screen = 2; // digital pin 2
int buzzer = 11; // digital pin 11 PWM
int led1 = 9; // digital pin 9 PWM
int led2 = 10; // digital pin 10 PWM
int accumulator = 3; // analog pin 3
byte Day; //
byte Month; //
byte Year; //
byte Hour; // variables with time and date information
byte Minute; //
byte Weekday; //
int alarm_h = 8; //
int alarm_m = 30; // variables for alarm setup
boolean alarm_d[7] = {false, false, false, false, false, false, false}; //
int weekdayCounter = 0; //
int alarmStatus = 0; //
unsigned int wake_m = 0; // variable for wake up light
int wake_m2 = 0; // variable for wake up light (necessary if it starts in previous hour)
boolean alert = false; // to switch on buzzer
boolean flag_light = false; // to switch on wake up light
int brightness = 0; // PWM value for LEDs
boolean flag_input = false;
boolean snooze_time = false;
byte snooze_count = 0;
boolean wake_lasthour = false;
int button_snooze; //
int button_status; //
int button_hour; //variables for buttons
int button_minute; //
boolean time_change = false;
boolean display_on = true;
int display_count = 0;
byte minute_count = 0;
boolean error = false; // if decoder input has a large deviation to the internal time
boolean flag_status = false;
long status_bat = 0;
long bat = 0;
long bat_perecent = 0;
void setup()
{
pinMode(antenna, OUTPUT);
pinMode(screen, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(b_snooze, INPUT);
digitalWrite(b_snooze, HIGH);
pinMode(b_status, INPUT);
digitalWrite(b_status, HIGH);
pinMode(b_hour, INPUT);
digitalWrite(b_hour, HIGH);
pinMode(b_minute, INPUT);
digitalWrite(b_minute, HIGH);
digitalWrite(screen, HIGH);
delay(100);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(10);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();
disp();
CLKPR = 0x80;
CLKPR = 0x02; // change clock divider to "4" =>2 MHz (8MHz crystal)
MsTimer2::set(7500, timeChange); //125 = 1 second; 7500 = 1 minute
MsTimer2::start();
}
void timeChange()
{
Minute++;
flag_input=false;
time_change = true;
digitalWrite(led1, HIGH);
if(flag_light==true&&brightness<220){brightness = brightness+50;} // Hier lässt sich Helligkeitssteigerung pro Minute einstellen
if(snooze_time==true){snooze_count = snooze_count + 1;} // Zähler für Minuten seit betätigen des Snooze Buttons
}
void loop()
{
byte externalClock[14] = {};
button_snooze = digitalRead(b_snooze);
button_status = digitalRead(b_status);
button_hour = digitalRead(b_hour);
button_minute = digitalRead(b_minute);
if(button_snooze==0||button_status==0||button_hour==0||button_minute==0) {display_count = 2;}
if (Minute>59) {Minute = 0; Hour = Hour +1;}
if (Hour>23) {Hour = 0; Day = Day + 1; Weekday + 1;}
if (Weekday>7) {Weekday = 1;}
if(Minute==15&&(Hour==3||Hour==11||Hour==19)) // switch antenna+decoder on
{digitalWrite(antenna, HIGH);} //
if(Minute==16&&(Hour==3||Hour==11||Hour==19)) // start Serial
{DCF.begin(19200);} // 2400*8
if(Minute==30&&(Hour==3||Hour==11||Hour==19)) // switch off antenna+decoder
{DCF.end(); delay(30);digitalWrite(antenna, LOW);} //
if(DCF.available() > 13)
{
for (int i = 0;i<14;i++)
{
externalClock[i] = DCF.read();
}
externalClock[1] = externalClock[1] - 65; //Day
externalClock[2] = externalClock[2] - 65; //Month
externalClock[3] = externalClock[3] - 65; //Year
externalClock[4] = externalClock[4] - 65; //Hour
externalClock[5] = externalClock[5] - 65; //Minute
externalClock[7] = externalClock[7] - 65; //Weekday
if(Day==externalClock[1])
{
if(Month==externalClock[2])
{
if(Year==externalClock[3])
{
if(Hour==externalClock[4])
{
if((externalClock[5]>=Minute-5)&&(externalClock[5]<=Minute+5))
{
if(Weekday=externalClock[7])
{
MsTimer2::stop();
Minute = externalClock[5];
MsTimer2::start();
}else{error = true;}
}else{error = true;}
}else{error = true;}
}else{error = true;}
}else{error = true;}
}else{error = true;}
}
if(time_change==true&&display_on==true)
{disp();time_change = false;digitalWrite(led1, LOW);}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
Scanox89:
update: the SoftSerial command doenst work always same problem like Serial
Can you explain this in detail please?
I note that you have a lot of libraries in your code. Are you sure they can all work happily together? A conflict (or 2) would not be at all surprising.
...R
if(time_change==true&&display_on==true)
{disp();time_change = false;digitalWrite(led1, LOW);}
}
You can't afford to have more lines so that each statement is on its own line, but you can afford to have all that useless white space?
I tried it few time with replacing the standard Serial command with the SoftSerial library in two of four tests the system crashed aswell if he microcontroller is stopping the serial interface
@PaulS yeah its not that much pretty at the moment have to fix it ![]()
Scanox89:
I tried it few time with replacing the standard Serial command with the SoftSerial library in two of four tests the system crashed aswell if he microcontroller is stopping the serial interface
If this is a response to my Reply #5, I was hoping for a great deal more information.
Have you tried temporarily disabling some (or most) of the libraries you are trying to use?
...R
@PaulS yeah its not that much pretty at the moment have to fix it
The way to avoid having to "pretty up" your code later is to not write ugly code in the first place.
There are several general styles for writing C code. Not a one of them recognizes writing more than one statement per line. Not a one of them recognizes more than a single blank line between blocks of code.
Adopting your own style is OK, as long as other people can actually read it. Yours is NOT a readable style.
Take 5 minutes NOW and put every statement on its own line. Put every { on a new line. Put every } on a new line. Get rid of the excessive white space. Use Tools + Auto Format. You'll be amazed at how much easier it is to follow your own code.
PaulS:
you'll be amazed at how much easier it is to follow your own code.
+5
Spending a few hours programming with Python is a great way to get good formatting habits because whitespace matters in Python.
...R
Spending a few hours programming with Python is a great way to get good formatting habits because whitespace matters in Python.
Which drives those of us that properly indent code, and consistently use { and } nuts.
PaulS:
Which drives those of us that properly indent code, and consistently use { and } nuts.
I don't get this.
Python does not use {} in the way that C/C++ does so, yes, you have to remember to use them in C/C++ code.
This is a two-way street. Python needs some things done differently also.
But the indentation practices in Python work fine in C/C++.
...R
Its the fact that indenting is the ONLY thing that matters to Python that causes me issues every time I need to use it. Which, thankfully, isn't that often.
PaulS:
Its the fact that indenting is the ONLY thing that matters to Python that causes me issues
Yes. That was irritating for a while after switching from Ruby, but I have got used to it. But it has also made me more anal about Arduino formatting ![]()
...R