Trying my best to assist new members out of my 50 years in mostly old environments I show this code as help to debug in real time. That way I managed to quickly dig into unknown and large codes.
The example use a triple loop activating an RGB LED.
Do like this:
Open Serial Monitor and set the baudrate in IDE and in the code to the same value.
In the code I used the names A, B and C and they make the loop variable i, j and k to be displayed.
Send A, B or C from Serial, at any time You want, to the controller like this: ABC Cr, or garbageABC Cr. Cr == Cariage return.
void setup() {
// put your setup code here, to run once:
pinMode (9, OUTPUT);//pin used for blue colour
pinMode (10, OUTPUT);//pin used for red colour
pinMode (11, OUTPUT);//pin used for green colour
Serial.begin(115200);//full speed but can be any other speed
analogWrite(9, 125);//turn on blue colour
Serial.print("125 ");//tell on Serial
delay(5000);//give spectator time to verify function
analogWrite(9, 0);//turn off blue
analogWrite(10, 125);//turn on red
Serial.print("125 ");//tell on Serial
delay(5000);//give spectator time to verify function
analogWrite(10, 0);//turn off red
analogWrite(11, 125);//turn on green
Serial.println("125 ");//tell on Serial
delay(5000);//give spectator time to verify function
analogWrite(11, 0);//turn off green
}
void loop() {
int i, j, k;//the three colour loop variables
int debugDisplay;//value to be displyed if a defined name is given
bool debugFlag;//debug flag telling a valid variable name is received
char Sa;//debugged variable name to be displayed
for ( i = 0; i < 125; i = i + 5)//loop for blue
{
for ( j = 0; j < 125; j = j + 5)//loop for red
{
for ( k = 0; k < 125; k = k + 5)//loop for green
{
debugFlag = false;//initiate flag for displaying value
if (Serial.available())//if debug order from keyboard
{
Sa = Serial.read();//get the name for variable to disply
switch (Sa) {
case 'A'://name is 'A', display its variable in switch case below
{
debugDisplay = i;//display correct variable value in case below
debugFlag = true;//tell valid debug name was received
break;
}
case 'B'://name is 'B', display its variable in switch case below
{
debugDisplay = j;//display correct variable in case below
debugFlag = true;//tell valid debug name was received
break;
}
case 'C'://name is 'C', display its variable in switch case below
{
debugDisplay = k; //display correct variable in case below
debugFlag = true;//tell valid debug name was received
break;
}
default:
// debugFlag = false;
break;
}//end of switch
if (debugFlag)//print out if valid debug from Serial
{
Serial.print(Sa); Serial.print(" = ");//name of variable
Serial.println(debugDisplay);// actual variable value
debugFlag = false;//turn off display, vslue displeyd above
}
}
}
analogWrite(9, i);//PWM for blue
analogWrite(10, j);//PWM for green
analogWrite(11, k);//PWM for red
}
}
}