how to draw a flowchart for showSpectrum & showRGB

how to draw a flowchart for showSpectrum & showRGB

Can someone show me the start before I can contiune

void showSpectrum()
{
int x;

for(x = 0; x < 768; x++)
{
showRGB(x);
delay(10);
}
}

void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;

if (color <= 255) // zone 1
{
redIntensity = 255 - color; // redgoes from on to off
greenIntensity = color; // green goes fromoff to on
blueIntensity = 0; // blue is always off
}
else if (color <= 511) //zone 2
{
redIntensity = 0; // red is always off
greenIntensity = 255 - (color - 256); // green on to off
blueIntensity = (color - 256); // blue off to on
}
else // color >= 512 // zone3
{
redIntensity = (color - 512); // red if off to on
greenIntensity = 0; // green is always off
blueIntensity = 255 - (color - 512); // blue off to on
}

analogWrite(RED_PIN, redIntensity);
analogWrite(GREEN_PIN, greenIntensity);
analogWrite(BLUE_PIN, blueIntensity);
}

What is it exactly you need to understand?

The code has a value varying between 0 and 767

When the value is between 0 and 255 there is an inversely correlated mix between red and green initially full red and no green to full green and no red

Then from 256 to 511 green and blue get mixed - you go from full green (so continuation of previous color) to full blue

Then from 512 to 767 you mix blue and red from full blue back to full red

This is pretty crude because human eyes do not perceive R, G or B the same way so the inverse correlation of the mix will not deliver a superbly looking rainbow spectrum.

Also make sure you wire your RGB led the right way with current limiting resistors, some colors don't have the same forward voltage but you will still send from 0 to 5V into each pin (blue and green have the same specs usually so same resistor, red Usually needs a higher resistor). Also remember the max current ability of your arduino when defining the resistor values.

what does showSpectrum() mean, can explain
before i can start my flowcharts. Thank

void showSpectrum()
{
int x;

for(x = 0; x < 768; x++)
{
showRGB(x);
delay(10);
}

The showSpectrum() function simply calls the showRGB() function with each of 768 different values, presumably to show a complete spectrum of colours using combinations of R, G and B values.

Hi may I know the flowchart is correct for showSpectrum, Please advise

Usually in a flow chart you would explode the for loop in its 3 components

Initialize
Test
Increment

Test is the diamond shape part the other should be regular boxes in the right location

Hi I have done the flowcharts but I not sure is correct anot. Please advise

I would do the flowchart by having separate actions for initialising x (a box), incrementing x (a box) and testing x for completion (a diamond) The results of the test diamond would either lead to another action box calling showRGBxthen a delay() box then looping back to the incrementing x box, or exiting the function.

It’s not correct to have a for loop inside a decision box.

Treat your ‘for’ loops (for (A; B; C) body;) as the equivalent ‘while’ loop:

A;  // Initialize
while (B) {  // Test
    body;
    C;  // Increment
}

You can then break the ‘while’ down into ‘if’ and ‘goto’:

A; // Initialize
Y: if (!B) goto X   // Test
body;
C;  // Increment
goto Y;
X:  // End of loop

That is why pseudocode is often better than a flowchart. It doesn't force you to perform goto's. :slight_smile: