Arduino KY-016 RGB LED (Function help please)

Hello Everyone,
Newbie on a steep learning curve (Fully understand Serial instructions now and the Platfomio:Serial Monitor icon for example.
Trying to get my head around Functions and make my code more ‘readable’ / ‘professional’.
From the attached code I’ve had the RGB led cycling through Red, Green and Blue successfully Brightening then Dimming (Green commented out code).
Then I’ve tried to create a program with Global declared variables, and two common functions to do the Brightening and Dimming.
The code compiles without errors but nothing happens with the RGB led?
Please could someone point (pardon the pun) where the code error(s) are and why?

2nd Edited code: Understood about having un-initialised Global variables so placed them into the functions.
Tidied up the code but still no output with no errors on compiling…just can’t see/understand why the functions are not working?

#include <Arduino.h>
/*
void setup() {
  int result = myFunction(2, 3);
}
void loop() {
  // put your main code here, to run repeatedly:
}
int myFunction(int x, int y) {
  return x + y;
}*/

int intensity_Dim = 0;  
int intensity_Bright = 50; //select how bright led is
int delaytime = 10;

//put function declarations here:
void DimmerFunction_Bright(int); 
void DimmerFunction_Dim(int);

// put your setup code here, to run once:
void setup() 
{ 
  int boardpin=0;
  pinMode(boardpin, OUTPUT);
  Serial.begin(9600);
}

// put your main code here, to run repeatedly:
void loop() 
{ int boardpin;
  for(boardpin=11; boardpin < 9; boardpin--)
 {  Serial.print(boardpin, DEC);
    DimmerFunction_Bright (boardpin);
    delay(100);
    DimmerFunction_Dim (boardpin);
  }

   for(boardpin = 9; boardpin >11; boardpin++)
 {
    DimmerFunction_Bright (boardpin);
    
    delay(100);
    DimmerFunction_Dim (boardpin);
  }
} 

// put function definitions here:
void DimmerFunction_Bright (int boardpin)
{ int val;
  for(val=intensity_Dim; val<intensity_Bright; val++)
 {  
  analogWrite(boardpin, val);
  delay(delaytime);
  Serial.print(val, DEC);
 }
}

void DimmerFunction_Dim (int boardpin)
{ int val;
  for(val=intensity_Bright; val>0; val--)
 {
  analogWrite(boardpin, val);  
  delay(delaytime);
  Serial.print(val, DEC);
 }
}

This makes no sense. This just initializes D0 as “output”, but your actual RGB pins, which are 11 downto 9, are not initialized by this at all and remain INPUTS. You should set those as outputs, without touching D0.

Global variables can be initializes too. In fact you’re already doing that with

All of these are global variables that have an initial value. Since they are not const, they can also be reassigned to a new value in code later.

Also this for loop is wrong, it will never execute a single loop of it. You start with boardpin = 9. Then,to enter the body of the for loop, you write that the condition boardpin > 11 must hold. But since boardpin is only 9, and thus not greater than 11, this condition and wrong, and thus nothing will happen. If you wanted the loop to run from 9 to 11 inclusive, you should have written for(boardpin = 9; boardpin <= 11; boardpin++) instead. Which is also the same as for(boardpin = 9; boardpin < 12; boardpin++).

The same thing goes to for the other for loop:

Starting value 11. Your condition for executing the loop is < 9. But 11 is not less than 9. So it never executes. To correctly count down from 11 downto 9 (inclusive), you should have written for(boardpin=11; boardpin >= 9; boardpin--) instead. Think about how this goes in each iteration.

pin   pin >= 9?     
11    yes    --> pin--
10    yes    --> pin--
9     yes    --> pin--
8     no     --> loop exit (body not executed)

In all of these for loops you can also make use of that fact that you can create the iteration variable in the first statement of the for loop. That is, instead of writing

int val;
for(val=intensity_Dim; val<intensity_Bright; val++) {
}

write

for(int val=intensity_Dim; val<intensity_Bright; val++) {
}

This is more compact and readable.

If you want a reference solution:

#include <Arduino.h>

const int intensity_Dim = 0;
const int intensity_Bright = 50; // select how bright led is
const int delaytime = 10;
const int rgb_pins_start = 11;
const int rgb_pins_end = 9;

// put function declarations here:
void DimmerFunction_Bright(int);
void DimmerFunction_Dim(int);

void setup()
{
  for (int pin = rgb_pins_start; pin <= rgb_pins_end; pin++)
  {
    pinMode(pin, OUTPUT);
  }
  Serial.begin(9600);
}

void loop()
{
  for (int boardpin = rgb_pins_end; boardpin >= rgb_pins_start; boardpin--)
  {
    Serial.print(boardpin, DEC);
    DimmerFunction_Bright(boardpin);
    delay(100);
    DimmerFunction_Dim(boardpin);
  }

  for (int boardpin = rgb_pins_start; boardpin <= rgb_pins_end; boardpin++)
  {
    DimmerFunction_Bright(boardpin);

    delay(100);
    DimmerFunction_Dim(boardpin);
  }
}

void DimmerFunction_Bright(int boardpin)
{
  for (int val = intensity_Dim; val < intensity_Bright; val++)
  {
    analogWrite(boardpin, val);
    delay(delaytime);
    Serial.print(val, DEC);
  }
}

void DimmerFunction_Dim(int boardpin)
{
  for (int val = intensity_Bright; val > 0; val--)
  {
    analogWrite(boardpin, val);
    delay(delaytime);
    Serial.print(val, DEC);
  }
}

Hello,

Thanks for the reply, before reading your reply I realised a few mistakes to the code which I’ve corrected to the below copy. It’s working fine…except analogueWrite(boardpin, val); isn’t outputting anything from the board pins?

#include <Arduino.h>
/*
void setup() {
  int result = myFunction(2, 3);
}
void loop() {
  // put your main code here, to run repeatedly:
}
int myFunction(int x, int y) {
  return x + y;
}*/

int intensity_Dim = 0;  
int intensity_Bright = 80; //select how bright led is
int delaytime = 100;

//put function declarations here:
void DimmerFunction_Bright(int); 
void DimmerFunction_Dim(int);

// put your setup code here, to run once:
void setup() 
{ 
  int boardpin=0;
  pinMode(boardpin, OUTPUT);
  Serial.begin(9600);
}

// put your main code here, to run repeatedly:
void loop() 
{ 
  int boardpin=0;
  {
    for(boardpin=9; boardpin < 11; boardpin++)
    {  
       DimmerFunction_Bright (boardpin);
       delay(10);
       DimmerFunction_Dim (boardpin);
    }
   for(boardpin = 11; boardpin >9; boardpin--)
    {  
       DimmerFunction_Bright (boardpin);
       delay(10);
       DimmerFunction_Dim (boardpin);
    }
  }
} 

// put function definitions here:
void DimmerFunction_Bright (int boardpin)
{ int val;
  for(val=intensity_Dim; val<intensity_Bright; val++)
 { Serial.println(boardpin,DEC);
   Serial.print(val, DEC);
   analogWrite(boardpin, val);
   delay(delaytime);
 }
}

void DimmerFunction_Dim (int boardpin)
{ int val;
  for(val=intensity_Bright; val>0; val--)
 { Serial.println(boardpin,DEC);
   Serial.print(val, DEC);
   analogWrite(boardpin, val);  
   delay(delaytime);
  }
}

All working fine now…helps even more when you find a loose 5v pin connector!!!
Thank you maxgerhardt for those code snippets…saved in the memory bank for my next sensors coding.

Again, the upper loop won’t be executed with boardpin = 11 and the lower loop will never be executed with boardpin = 9, so they’re each missing one of the RGB pins. Explanation see above.

    for(int boardpin=9; boardpin < 11; boardpin++) {
       printf("pin %d\n", boardpin);
    }

pin 9
pin 10

That’s really interesting Max, thank you. I understand the difference with the mathematical operator > against >= and < against <=. However the below code is indeed using all pins 9, 10 and 11 to be output.
#include <Arduino.h>
/*
void setup() {
int result = myFunction(2, 3);
}
void loop() {
// put your main code here, to run repeatedly:
}
int myFunction(int x, int y) {
return x + y;
}*/

int intensity_Dim = 0;
int intensity_Bright = 80; //select how bright led is
int delaytime = 100;

//put function declarations here:
void DimmerFunction_Bright(int);
void DimmerFunction_Dim(int);

// put your setup code here, to run once:
void setup()
{
int boardpin=0;
pinMode(boardpin, OUTPUT);
Serial.begin(9600);
}

// put your main code here, to run repeatedly:
void loop()
{
int boardpin=0;
{
for(boardpin=9; boardpin < 11; boardpin++)
{
DimmerFunction_Bright (boardpin);
delay(10);
DimmerFunction_Dim (boardpin);
}
for(boardpin = 11; boardpin >9; boardpin–)
{
DimmerFunction_Bright (boardpin);
delay(10);
DimmerFunction_Dim (boardpin);
}
}
}

// put function definitions here:
void DimmerFunction_Bright (int boardpin)
{ int val;
for(val=intensity_Dim; val<intensity_Bright; val++)
{ Serial.println(boardpin,DEC);
Serial.print(val, DEC);
analogWrite(boardpin, val);
delay(delaytime);
}
}

void DimmerFunction_Dim (int boardpin)
{ int val;
for(val=intensity_Bright; val>0; val–)
{ Serial.println(boardpin,DEC);
Serial.print(val, DEC);
analogWrite(boardpin, val);
delay(delaytime);
}
}

I do appreciate your help, and agree your rgb_pins_start and rgb_pins_end, const int declerations are much more exquisite!

Yes, but that is because the first loop goes from 9 to 10 (inclusive), the second loop is going from 11 to 10 (inclusive), so when all used pins are printed, it indeed shows 9,10 and 11, but each loop respectively is only using 2 of the 3 RGB pins. If you remove one of the for loops in loop(), you will come to know what I mean.

If that is exactly how you wanted it to behave, then there’s no problem.

Ahhhh, yes I see it now Max running your reference code solution against mine (just needed to make rgb_pins_start =9 and rgb_pinsend = 11)

Small but very important steps to learn, your’e a good teacher thanks for your help and patience.