Number to Variable (?)

Hi.
This is the situation.

I have this Colour code in Hex

unsigned long a = 0x00FF00;

Now. I have to use it here:

trellis.setPixelColor(43,  a);

But instead of using the variable, Im sending a number through serial.

trellis.setPixelColor(43,  SerialData);

How can I change the number (any number necesary) to represent that variable?

unsigned long SerialData = a;

Im sending a number through serial.

Are you sending a number or characters representing a number ?

See:
https://forum.arduino.cc/index.php?topic=396450.0

You have to give more information to clear things up.

the original use is: there is a variable "a" of type unsigned long which is assigned a value to

unsigned long a = 0x00FF00;

This value represents a color and the color is set with the command

trellis.setPixelColor(43,  a);

No you want to send the color-values through a serial connection.
So there must be a partner that is sending.

How does the sending looks like?
Are you sending:

Serial.print("0x00FF00");

or are you sending:

unsigend long myColorNumber = [color=#222222]0x00FF00;[/color]
Serial.print(myColorNumber );

or are you sending:

Serial.print("00FF00");

or are you sending:

Serial.print("65.280<U+202C>");

or Is the device that is sending not an arduino but something different whcih has its own format of how it does send it and you can't change the format?

If yes you would have to analyse what does your arduino receive?

You should give an example of the whole process and describe the sending device and the receiving device

best regards Stefan

Thanks for the answers.

I will explain what I want to do better. I try to keep it short to not bore people.

Im sending data from PC (MAX/MSP) to Arduino via Serial.
Max/msp sends ASCCI, so if I want to use the letter ¨a¨ I have to convert it.

Then, so goes my thought, I need to change that number from ASCII to the variable letter (a).
So I can use the colour code.
And I have to be able to use different colour variables.

Moris526:
Max/msp sends ASCCI, so if I want to use the letter ¨a¨ I have to convert it.

Then, so goes my thought, I need to change that number from ASCII to the variable letter (a).
So I can use the colour code.

I think what you are saying is that you want to send the character 'a' and then the receiving program should use a particular value.

You can't refer to variables by name at runtime because the compiler will have replaced all the names with addresses in memory. So you need code something like this

if (receivedChar == 'a') {
  colorCode = 0x00FF00;
}
else if (receivedChar == 'b') }
  colorCode = 0x0000FF;// just guessing at a value
}
// etc

trellis.setPixelColor(43,  colorCode);

...R

What you are describing is not possible to do in the way that you describe. You could, however, read a value, assign it to a variable then the variable in your code

The crucial part is reading the ASCII values and converting them to an integer value to be assigned to the variable. Do you have control over the format of the data being sent to the Arduino ?

UKHeliBob:
What you are describing is not possible to do in the way that you describe. You could, however, read a value, assign it to a variable then the variable in your code

The crucial part is reading the ASCII values and converting them to an integer value to be assigned to the variable. Do you have control over the format of the data being sent to the Arduino ?

Yes. But I think I have to send in ascii format

So I quote:

septillion:
See:
Serial Input Basics - updated - Introductory Tutorials - Arduino Forum

I think I have to send in ascii format

That is not a problem. Look at the techniques used in the topic linked to and add a start and end marker to your data

I will explain what I want to do better. I try to keep it short to not bore people.

NO ![/b] this all is about **annoying[/b] people that they have to ask back again and aigain and again because the given information was still toooo less ! **
You can leave out your hair-color and the brand of your mouse. But anything related to your project could be relevant and useful information. I have never seen complain any user about writing too much text as long as you format them to paragraphs.
> Im sending data from PC (MAX/MSP)
I have to ask for more details again: What program is sending the data over serial?
Write the exact name or post a screenshot of this software that is doing the sending.
If you have to type in the data into the SEND-program by hand write an example of how this data looks like
I have posted some examples above each of them will work. It is just a matter of varying the conversion from the received data to get a number out of it that could be stored in a variable.
You are too quick in assuming how things work and there have been multiple cases where your conclusions were wrong.
So it is even more important to write more details than less.
to increase your udnderstanding of programming:
the code
**__ <strong>**unsigned long a = 0x00FF00; trellis.setPixelColor(43, a);**</strong> __**
will be "translated" to te exact same program as if you would write the code
**__ <strong>**unsigned long XYZ = 0x00FF00; trellis.setPixelColor(43, XZY);**</strong> __**
Your code needs to be extended in a way
char myRxVariable[32];
receive data and store it into the variable myRxVariable
convert character-sequence stored in variable myRxVariable into an intergervalue
store integervalue into a variable myColorVariable
and then do a
**__ <strong>**trellis.setPixelColor(43, myColorVariable );**</strong> __**
that is the basic route to go. To understand how this work you should work through the examples of the
two times quoted tutorial
Serial Input Basics
best regards Stefan

Thanks all for the asnwers.

Robins solution worked just right.

Thanks Stefan for the time you took to answer.

About this..

NO ! this all is about annoying people that they have to ask back again and aigain and again because the given information was still toooo less !

I understand that with more info is better to find solutions but its not like I hide information to make it hard for you to help me....
I already received in this forum impatient answers for my ignorance.
I dont understand the annoyance though, nobody is forcing nobody to answer dumb, hard or incomplete questions.

I dont understand the annoyance though, nobody is forcing nobody to answer dumb, hard or incomplete questions.

Most long term members here enjoy helping people. The annoyance comes from the fact that with better, more complete information it is easier to provide solutions to problems, but new members often do not help themselves by reading and following the freely available advice such as How to get the best out of this forum - Programming Questions - Arduino Forum which is s sticky post at the top of the forum page

If people pick & choose which questions to respond to (they do), they’re more likely to contribute to the questions with the most complete problem description .

It doesn’t hurt if the OP is inaccurate - we can help fix that, but guessing wastes a lot of time.

In the latter cases, responders just click on the next question, which may have more complete input, then the OP gets frustrated with the lack of assistance.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.