BazzlewithaK

As stated in the topic, I am a beginner to arduino. Recently I've been working on a private project to construct an ambient weather light using the arduino. Unfortunately, there are still a couple things I am stuck on and I need it done by Wednesday. Explaining my own incompetence aside, I obviously need some help.

The hardware is simple enough, so I won't get into that, and I know there are some answers out there, but as a beginner every tutorial and answer I have found was one I just can't wrap my head around as I don't have enough experience to understand everything they are referring to, so when answering it is not required but I'll still ask for your patience and would be eternally grateful if you could simplify your answer to something a beginner can understand and especially if you could endure my questions. Also, if my code seems painfully messy I apologize as I have not been working with this language for very long.

Anyway, I have two questions:

First, how do you get the arduino to read incoming information as a string rather than individual characters. I was testing my RGB LED's response to the logic statements using the serial port, however I have yet to figure out how I can get it to read '72' instead of '7' '2'.

Here's my current code for reference (which I'll admit I have been using multiple resources to "Frankenstein" this together) :

I should put it in right now that I do intend for this to be compatible with incoming information from a php script that will parse the html I need. The only reason everything is commented out mostly has to do with the fact I need it to be able to read the information as a string first. And apologies if there might be some spare code I forgot to scrape up from my prior attempts to get this thing to work correctly.

Second Part: As more experienced coders do you think the following code is a good method to go about something like this and/or do you know of any good resources for this type of project that can tell me the information in plain and simple terms?

First, how do you get the arduino to read incoming information as a string rather than individual characters.

It's best if you don't go there. Read the characters, one at a time, storing then in an array, until you get the end of packet marker (something that indicates that the sender's message is complete).

Quote

Second Part: As more experienced coders do you think the following code is a good method to go about something like this and/or do you know of any good resources for this type of project that can tell me the information in plain and simple terms?

That depends on what the PHP script is sending to the Arduino. Some stuff is easier to parse/deal with than other stuff.

Before posting this topic (twice), did you read the sticky thread at the top of the section that ask you, politely, to read it before posting?

Why not?

DO NOT CROSS-POST, IT WASTES TIME.

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.Do not send technical questions via personal messaging - they will be ignored.I speak for myself, not Arduino.

BazzlewithaK

First, how do you get the arduino to read incoming information as a string rather than individual characters.

It's best if you don't go there. Read the characters, one at a time, storing then in an array, until you get the end of packet marker (something that indicates that the sender's message is complete).

Quote

Second Part: As more experienced coders do you think the following code is a good method to go about something like this and/or do you know of any good resources for this type of project that can tell me the information in plain and simple terms?

That depends on what the PHP script is sending to the Arduino. Some stuff is easier to parse/deal with than other stuff.

First, thank you very much for replying~! ^_^

Second: The array idea sounds good. How would I be able to set it up to convert it?

Which I need help with as well, although I figured people not might be happy if I asked questions about php on an arduino forum. Still, I use my second php located Here as reference (Viewing the source code making it much easier to find the specific lines I need) Currently it just posts temperature, but I would also like to get it to grab a weather condition once I improve it.

BazzlewithaK

Before posting this topic (twice), did you read the sticky thread at the top of the section that ask you, politely, to read it before posting?

Why not?

DO NOT CROSS-POST, IT WASTES TIME.

I apologize. I honestly had not meant to be a nuisance. When I posted it originally in the Project Guidance section it was kind of with the idea of the second question still in mind and then I realized my first question (and admittedly the dominating one of the post) belonged in this section, so I was more or less confused as to where to put it.

PeterH

It looks to me as if you are using the same sketch as a learning tool and also trying to evolve it into your final solution. That isn't a good way to work. It is far better to write small test sketches that are just for you to learn or test a particular feature of your code - for example, handling serial input. Once you have worked out the best way to do it, you can then incorporate that feature into your project.

Make sure you back up your work as you go. By that I mean keep a copy of old versions of your source code somewhere. If you don't already have any scheme in place for this, I recommend File Hampster as a good, simple automated versioning system.

Get used to deleting code that you no longer use, rather than keeping it commented out 'just in case'. You can always retrieve it from your backup/versioning system if you find you need it again, or just copy+paste it into a separate file that you save somewhere. But don't leave it cluttering up your working code since it makes it harder to see the real code.

Get used to indenting and spacing your code properly. I recommend putting each { and } on separate lines with matching pairs indented by the same amount and ythe lines between them indented one extra level. If you put the { and } on separate lines, the Tools / Auto Format command will sort out the indentation for you.

You have (IMO) far too many blank lines in your code, which means you can only see a small part of your code at a time. The more of your code you can physically fit on the monitor at the same time, the easier it will be to understand. I suggest you delete any blank line which is next to a {, or next to a }, or next to another blank line. Get rid of blank lines between statements that are similar/related. Blank lines are useful as a way to visually separate pieces of code that are logically distinct, and also to separate inline comments from unrelated code. However, you can have too much of a good thing.

There are two approaches to handling serial input as a string.

The first one is to buffer the incoming string in a c-string. A c-string is a char array with a null value after the last character. This is the approach I recommend. With this approach, each time your received a new character you would append it to the buffer by writing it to the next entry in the array and putting a null terminator after it. You need a scheme to recognise when the input string is complete. With textual input, the most obvious way to do that is by putting a newline character at the end of each message. It is good practice to also make sure you don't overflow the array, in case you receive a message that is much longer than you expect. The code could look similar to this:

The alternative approach uses the String class. The code would look broadly similar but appending to the string would be done using the '+' operator and string comparisons would be done using the '==' operator. However, the String class exposes a memory leak which can cause stability problems on Arduino versions prior to 1.0.4, and introduces potential memory fragmentation problems in all versions; I don't recommend using it.

First, how do you get the arduino to read incoming information as a string rather than individual characters. I was testing my RGB LED's response to the logic statements using the serial port, however I have yet to figure out how I can get it to read '72' instead of '7' '2'.