Control LEDs with UART

Thread Starter

Kittu20

Joined Oct 12, 2022
511
You didn't say whether you were sending ASCII'2' or 0x02 as a command.
That's what I said "You will also need to insert some code between lines 2 and 3 to deal with illegitimate commands."
I have attached a screenshot of the serial terminal tera term for reference Post #1.

I believe when using terminal software like Tera Term to communicate with microcontroller through UART, it send characters as ASCII characters. The microcontroller receives these characters as ASCII characters
 

MrChips

Joined Oct 2, 2009
34,809
There is common misunderstanding of what is ASCII.

Everything is binary. ASCII is still binary. The UART can send 8 bits of binary information, from 00000000 to 11111111. There are 256 possible combinations which can be used to represent 256 different things, such as colours, objects, pictures.

Think of ASCII as just 256 picture cards. Hence, think of the ASCII number as calling up a particular picture card. Card #49 is a picture of "1". Card #65 is a picture of "A".

The bottom line is, everything is binary. What it represents is whatever you and your audience defines it to be. The ASCII table is what the 256 binary patterns represent.
 

BobTPH

Joined Jun 5, 2013
11,515
I was assuming the array was 256 bytes.

If you want it to be only 3, use

period = array[(ReceivedChar-‘1’) % 3];
 

Ian0

Joined Aug 7, 2020
13,131
I was assuming the array was 256 bytes.

If you want it to be only 3, use

period = array[(ReceivedChar-‘1’) % 3];
Probably better to use
if (ReceivedChar<4) period=array[ReceivedChar];

otherwise sending "4" has the same effect as sending "1", when "4" should probably be ignored.

(I'm not covering whether commands are sent in binary or ASCII, and assuming for the moment that they are binary)
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
Probably better to use
if (ReceivedChar<4) period=array[ReceivedChar];

otherwise sending "4" has the same effect as sending "1", when "4" should probably be ignored.
While I've attempted to follow your advice, look to a previous post (Post 19) , and I'm unable to make any further progress.

I appreciate your help thus far, and could you kindly provide a more detailed explanation of your logic. It would be helpful if you could break it down into steps or provide additional context so that I can better grasp how to incorporate it into my code.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
Next job: Differentiate valid commands from invalid commands.
Could you please provide a step-by-step explanation of how you would handle LEDs flashing with Uart commands ? I'm particularly interested in understanding your logic for this scenario. Your detailed insights would be greatly appreciated.
 

Ian0

Joined Aug 7, 2020
13,131
Could you please provide a step-by-step explanation of how you would handle LEDs flashing with Uart commands ? I'm particularly interested in understanding your logic for this scenario. Your detailed insights would be greatly appreciated.
1. Decide how many commands that there will be.
2. Derive a test which gives "true" if the command is valid and "false" if it isn't.
3. Derive a function which takes the command number and returns an index for the lookup table.
4. initialise a lookup table which returns the period from the index.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
4. initialise a lookup table which returns the period from the index.
In this example, I've set up an array called periodLookupTable to store periods, where each element corresponds to a specific index.

C:
// Define the lookup table for periods
const unsigned int periodLookupTable[] = {2000, 1000, 500};
 

Ian0

Joined Aug 7, 2020
13,131
In this example, I've set up an array called periodLookupTable to store periods, where each element corresponds to a specific index.

C:
// Define the lookup table for periods
const unsigned int periodLookupTable[] = {2000, 1000, 500};
So now you need a variable - let's call it "index" - which takes the values 0, 1 or 2 depending on the incoming data. That's #3 on the list.
 

BobTPH

Joined Jun 5, 2013
11,515
And you are left with the decision of what to do when it is not valid.

Looking at Ian’s original code, yould could change it to:

if (ReceivedChar >= ‘1’ && ReceivedChar <= ‘3’)
period = array[ReceivedChar - ‘1’];

This would leave things unchanged on an invalid command.
 
Top