Newish to arduino here, but familiar with electricity (electrician by trade), so still figuring some of this stuff out.
Anyhow.
Building an alarm clock, and the library I am using (from rinky dink electronics) spits out the time in a string. I can set the time using variables. But cannot use IF statements to compare a setup alarm time variables (one variable for hours, one for the minutes) to the string to set the alarm off.
I think my real question is, is there a way to convert 2 integers into a string within the loop? What I basically want to do is (and I know this wouldnt compile it is just the easiest way to explain it):
if (rtc.getTimeStr == alarmstring)
{
this would be the alarm
}
I think I can explain more about what I am talking about:
My time string displays as HOURS:MINUTES:SECONDS. Example: 22:04:41
My alarmhour and alarmminute are 2 integers set by the user using potentiometers and stored as a variable. For example.
alarmhour = 5;
alarminute = 30;
My question lies, how can I convert my alarm time to a string, appending a zero of course to the end for seconds. that way I can use an IF statement to see if my alarm string matches my time string.
Please give a link to the library you are using, or to the page on which it can be found. (Please do not tell us to "just google it". There might be more than one library, or more than one version of the library.)
If your library genuinely provides no way to get the time except as a string, then I would say it is time to look for another library.
cumulateString=stringOne+stringTwo+stringThree; // if you want to connect the strings
...
// with this method you can easily create your own String combinations.
if (varXY<=cumulateString.toInt()<varXZ) { // if you want to compare the cumulateString also with an
Integer, then you use the ".toInt()" function.
...
}
#####################################
Hope this helps you.
I couldn´t test that exact code fragement, but I remember used it that way.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
The library you linked to also has a getTime() method that returns a Time object, which is a struct that has fields hour, min, and sec. So, it is NOT necessary to get the time as a String and parse it.
If you just must, for no good reason, indexOf() might prove useful, along with substring() and toInt() (which doesn't actually return an int).
Isnt String memory corruption problem solved in new library? Few month ago was reading cant remember where that that problem was solved.
When you remember post a link to it and I will look at it. In the meantime I will continue to avoid Strings.
Because of the way Strings work I don't think the problem is fixable. They work fine when there is 2GB of SRAM but not when there is 2K. I use the concept all the time when I write Python code on my PC, but never in my Arduino code.
Robin2:
When you remember post a link to it and I will look at it. In the meantime I will continue to avoid Strings.
Because of the way Strings work I don't think the problem is fixable. They work fine when there is 2GB of SRAM but not when there is 2K. I use the concept all the time when I write Python code on my PC, but never in my Arduino code.
...R
When I last looked at the Arduino String class, it expanded the buffer by one byte every time a character was appended to it, and it expanded the buffer by the exact amount of space required when appending a string. It would be better to expand the buffer exponentially (by, say, 50%) to avoid memory fragmentation. So I believe the String class can be improved to possibly reduce memory fragmentation to some extent.
christop:
When I last looked at the Arduino String class, it expanded the buffer by one byte every time a character was appended to So I believe the String class can be improved to possibly reduce memory fragmentation to some extent.
Not using it at all seems simpler and more reliable than modifying the code for the String class.