Help: Pulling integers out of a string, then making a comparison

Hello,

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
}

my integers are: "alarmhour", and "alarmmin"

EDIT:

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.

This is an excellent example of an XY problem.

What is an XY problem? http://xyproblem.info/

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.

Here is the library I am using:

http://www.rinkydinkelectronics.com/library.php?id=73

The Manual is on that page as well, as a PDF.

Don’t send us hunting the internet for code and/or schematics.

Always show us your ‘current’ complete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Posting images:

Hi!

maybe this could help you:
EXAMPLE:
##########################

int var1; //any int variables
int var2;
int var3;

String stringOne, stringTwo, stringThree;
String cumulateString;
.
.
.

void loop ()
{
stringOne = String(var1); //write integers into string
stringTwo = String(var2);
stringThree = String(var3);

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. :slight_smile:

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 parse example in Serial Input Basics may help.

...R

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).

Like I said, this is an XY problem.

The X is checking the current time against a user-selected time to determine whether or not it is now time to do something.

The Y is formatting a pair of integers as a string in such-and-such a format.

Working with times is a rabbit hole. Working with strings is another rabbit hole.

If you are going to go down a rabbit hole, at least make sure it's the right rabbit hole for the particular rabbit you're hunting.

If you are going to go down a rabbit hole, at least make sure it's the right rabbit hole for the particular rabbit you're hunting.

Alice can help you find the right rabbit hole.

All,

Figured it out. It was simply where I had placed my variables. I placed some in setup that should have been placed in the loop.

Thanks.

@Robin2

Isnt String memory corruption problem solved in new library? Few month ago was reading cant remember where that that problem was solved.

surepic:
@Robin2

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.

...R

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.

...R

Robin2:
Not using it at all seems simpler and more reliable than modifying the code for the String class.

...R

I won't disagree with that!