Hola Amigos,
Soy nuevo en el foro. Me estoy iniciando el mundo arduino siguiendo el libro 'Beninning Arduino' y me acabo de encontrar un código que no entiendo (aún con las explicaciones del autor). Es el proyeto número 8: RGB Mood Lamp:
float RGB1[3];
float RGB2[3];
float INC[3];
int red, green, blue;
int RedPin=11;
int GreenPin=10;
int BluePin=9;
void setup()
{
randomSeed(analogRead(0));
RGB1[0]=0;
RGB1[1]=0;
RGB1[2]=0;
RGB2[0]=random(256);
RGB2[1]=random(256);
RGB2[2]=random(256);
}
void loop()
{
randomSeed(analogRead(0));
for(int x=0; x<3; x++)
{
INC
}
for(int x=0; x=256; x++);
{
red = int(RGB1[0];
green = int(RGB1[1];
blue =i nt(RGB1[2];
analogWrite(RedPin, red);
analogWrite(GreenPin,green);
analogWrite(BluePin,blue);
delay(100);
RGB1[0] -= INC[0];
RGB1[1] -= INC[1];
RGB1[2] -= INC[2];
for (int x=0; x<3; x++)
{
RGB2
RGB2
- = constrain(RGB2
- , 0, 255);
delay(1000);
}
}
Los comentarios del autor son los siguientes:
'In the main programo loop, you first take a look at the start and end RGB values and work out what value is needed as an increment to progress from one value to the other in 256 steps (as PWM value can only be between 0 and 255). You do this with the following:
for(int x=0; x<3; x++)
{
INC
}
This 'for' loop sets the INCrements values for the R, G and B channels by working out the difference between the two brightness values and dividing that by 256.
You have another 'for' loop
for(int x=0; x=256; x++);
{
red = int(RGB1[0];
green = int(RGB1[1];
blue =i nt(RGB1[2];
analogWrite(RedPin, red);
analogWrite(GreenPin,green);
analogWrite(BluePin,blue);
delay(100);
RGB1[0] -= INC[0];
RGB1[1] -= INC[1];
RGB1[2] -= INC[2];
}
that sets the red, green, and blue values to the values in the RGB1 array; writes those values to Digital Pins 9, 10 and 11; deducts the increment values; and repeats this process 256 times to slowly fade from one random color to the next step. The delay of 100 ms in between each step ensures a slow and steady progression.
After you have taken 256 slow steps from one random color to the next, the RGB1 array will have the same values(nearly) as the RGB2 array. You now need to decide upon another set of three random values ready for the next time. You do this with another 'for' loop:
for (int x=0; x<3; x++)
{
RGB2
RGB2
- = constrain(RGB2
- , 0, 255);
delay(1000);
}
The random number is chosen by picking a random number between 0 and 556 (256+300) and then deducting 300. In this manner, you are trying to force primary colors from time to time to ensure that you won't always just get pastel shades. You have 300 chances out of 556 in getting a negative number and therefore forcing a bias towards one or more of the other two color channels.
The next command makes sure that the numbers sent to the PWM pins are not negative by using the 'constrain()' function.
As you use random(556)-300 for you RGB values, some of those values will be lower than zero; the constrain function makes sure that the values sent to the PWM is not lower than zero.'
Espero puedan ayudarme. Tengo varias dudas de como funciona el programa:
1) En el programa aparecen tres 'for'. Entiendo que el segundo 'for' no se inicia hasta que se completa el primero, no? Y lo mismo debe ocurrir con el tercer, verdad?
2) ¿Cúal es el objetivo de la ecuación 'INC
- ? ¿Que valores devuelve la primera vez que se incia el programa? Negativos?
3) ¿Qué se consigue con el segundo for? por ejemplo que devuelve red=int(RGB1[0]) en un rango de x=0; x<256 si en void setup() se define que RGB1[0]=0;
Bueno demasiadas dudas,
Siento no concretar mucho más de cuales son mis dudas reales pero no veo como trabaja el código,
Espero puedan orientarme y darme algún consejo de como puedo ir aprendiendo el lenguaje de programación arduino
Muchas gracias,
Un saludo