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);
}
}