Giving commands to Serial

I want to give commands to the Serial in order to start a process in the format

Start LED1 $

and switch on the LED only when it reads '$' character

and

Stop LED1 #

and switch off the LED only when it reads '#' character

I need to store the sentence "Start LED1" or "Stop LED1", that's why I do not get the sentences as commands, but only the '$' and '#' characters. I mean the sentence is not fixed, but varies, whereas the command characters, that I mentioned before, are fixed. Any ideas?

What will be sending the commands to the Arduino?

As far as the Arduino programming is concerned it will be much easier if you can send single character commands - for example 'A' could mean turn on the LED on pin3 and 'a' could mean turn it off. The 'B' and 'b' could be used for a different LED.

Or if you can send a single command for all the LEDs it could be like "11101" where the position identifies the LED and 1 means ON and 0 means OFF

...R
Serial Input Basics - simple reliable non-blocking ways to receive data.

Yes, I know that, but I need something different. I will be sending the commands to the Arduino (through the serial) and store them to an SD card, so I need when I send the sentence for switching on the LED, to store the sentence to the SD card and switch on the LED, and when I send the sentence to switch off the LED, to store the sentence on the SD card and switch off the LED.

Why do you need to do something different? What would be wrong with my suggestions? Knowing that will help me to understand what you are trying to achieve.

You have not told us what is sending the commands - is it a human or another computer program.

What is the purpose of saving the commands on the SD Card? Is it the intention to read them back in the future and execute them again? If so please give more details.

...R

As I said, "I" will send the commands. Human. I want to store timestamps with a sentence....

alex5678:
I want to store timestamps with a sentence....

And what, exactly, are you having trouble with?

It would still be best if you can reduce the "sentence" to a single character, or to a string that represents the states of all the LEDs.

Maybe have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

How will the timestamp be generated? Will you be sending it in addition to your sentence, or will the Arduino use the value of millis() at the time the sentence is received?

What is the purpose of the timestamp? What will it be used for?

The more I think about it the more this seems like an XY Problem. It would be a big help if you describe the overall project you are trying to create.

...R

As I said before, I know that single characters work, but I want within the timestamp to store a sentence that I give (with the command) on the SD card.

Can I implement it with interrupts?

(My question for the problem is very clear. If anyone can help I would appreciate.. Thank you...)

  1. Install a battery backed up real time clock (RTC) such as DS3231.
  2. In a loop -
    wait reading the serial input until you have a complete command;
    read the RTC and format as the time stamp you require;
    start a new line on the SD card to write to;
    write the time stamp to the SD card;
    write a separator such as a tab char to the SD card;
    write the command to the SD card;
    write some end of command chars;
    flush the SD card write;
    go back and start again.

You seem to have a distorted view of interrupts.

alex5678:
(My question for the problem is very clear. If anyone can help I would appreciate.. Thank you...)

It may seem clear to you, but when you are asking for help the important thing is for the question to be clear to others.

The fact that you have no mentioned interrupts makes things even less clear. I can't imagine what is in your mind that makes you think interrupts may be part of the solution.

And this does not make sense "within the timestamp to store a sentence that I give (with the command) on the SD card.". How can anything be inside a timestamp - something can be stored along with a timestamp, but not inside it. I asked you in Reply #5 how the timestamp would be generated and used but you have not responded.

When you don't respond to questions it makes it very difficult to help. If you don't understand the question please say so.

Does the sequence of events in Reply #7 seem appropriate? I have been assuming all along that that is approximately what you have in mind.

What is preventing you from giving us a description of the overall project? If you had done that in your Original Post you would probably have a solution by now.

...R