Can someone explain data type "byte"

helo guys, I understand that byte is an unsigned number (meaning positive number) from 0 to 255. But I don't understand this in context of the following line code. So can anyone explain it to me.

const byte RED[] = {255, 0, 0}; //can you explain this line in this following link http://pastebin.com/YwMXYH2i

thanks.

The key to your question is the two character sequence []

This is defining an array of byte variables, in this case three bytes long and initializing the the first element to 255 and the next two elements to 0. Further the array has been given the constant attribute which tells the compiler not to allow the values in this array to be changed, effectively making the array of bytes 'read only'. Here is the arduino reference for array types:

http://arduino.cc/en/Reference/Array

Lefty

That's an array of three bytes called RED, initialised with values 255, in [0], 0 in [1] and 0 in [2]

Look at each part of that statement.

const tells the compiler that the variable is read-only. Any attempts, therefore to write to the variable will result in a compiler error.

byte defines the type of the variable. You already know what a byte is.

RED is the name of the variable.

[] means that the variable is actually an array - a collection of values of the same type.

= means that values are being assigned.

{255, 0, 0} are the values being assigned to the elements of the array.

Because there was no value in the [], the compiler counts the number of values to determine how much space to allocate for the array.

; is the required ending for all statements.

Thanks people. Actually, what I wanted to know was why the programmer used "255"? Since the binary of 255 = 1111 1111, I assume it has something to do with assigning binary to 'ON' RGB LED colours? Can anyone tell me how this works?

thanks.

Presumably, the programmer is going to pass those colour component values to eight bit PWM channels, with the LED connected to each in a non-inverting manner, so {255, 0, 0} represents maximum brightness for the red channel.

AWOL: Presumably, the programmer is going to pass those colour component values to eight bit PWM channels, with the LED connected to each in a non-inverting manner, so {255, 0, 0} represents maximum brightness for the red channel.

So, from what I understand Arduino PWM frequency is 500Hz, hence time period for one cycle is 2ms. 255 means always ON with a duty cycle of 100%. So, my question is how do you come up with 255. How exactly can I find it? And what does it indicate?

Thanks

Irrespective of PWM frequency, an eight bit PWM channel will be at 100% ON with a value of 255, because 255 (0b11111111 or 0xFF) is the largest number you can represent in eight bits.
A value of 128 will represent a 50% duty cycle (128/255), again, irrespective of PWM frequency.

Note, however, that perceived brightness of a LED is a log function, not linear.

So, my question is how do you come up with 255.

The PWM value is an 8 bit value. That means that it can range from B00000000 to B11111111, or 0 to 255.

Thanks so much to everyone. Here is a code someone gave me as an example. But, I haven’t quite figured out the logic. So can anyone explain please. Just put some comments near each line of command. I can understand code 1, but I dont understand code 2.

Code 1:
t = readTemp();
if (t < 30) doLed(0,255,0);
else if (t< 40) doLed(255,255,0);
else doLed(255,0,0);

An nicer version with gradient colors could be
Code 2:
t = readTemp();
int red = constrain((t-30)*25, 0, 255);
int green = 255 - constrain((t-40)*25, 0, 255);
doLed(red, green, 0);

Code 2 just makes a gradual change of colour as the temperature changes.

int red = constrain((t-30)*25, 0, 255);  
int green = 255 - constrain((t-40)*25, 0, 255);
doLed(red, green, 0);

The documentation for "constrain" is over at the Main Site Reference section.

Thanks.

int red = constrain((t-30)*25, 0, 255); means (t-30)*25 is between 0 - 255.

One more question, which is I have been googling on how to change RGB Led colour and always the codes seems to have an array of 3 values. (ex: Red [] = {255, 0, 0};). My question is why is it that there are 3 arrays used? Does it have something to do with PWM too?

Thanks for all the support and help. Greatly appreciate it.

Does it have something to do with PWM too?

No, not PWM but RGB