Please help!

I'm having trouble trying to make my lighBulltin function only be active when I press 'P' in the serial monitor. It would be a great help to have someone helped I've watched 100 videos and still am not sure what to do.

int redLED = 7;
int greenLED = 6;
int blueLED = 5;
int yellowLED = 4;
//int delayTime=200;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
lightBulltin(11);
Serial.begin(9600);
while (! Serial);
Serial.println("Enter LED Number 0 to 3 or 'x' to clear. For a new function press p!");
}

// the loop routine runs over and over again forever:
void loop() {
if (Serial.available()){ //This funtion turns lights on individually based on the character or number selected in the serial monitor
char ch;
ch = Serial.read();

if (ch == '0'){
digitalWrite(redLED, HIGH);
Serial.println("Red LED is on");
}
else if (ch == '1'){

digitalWrite(greenLED, HIGH);
Serial.println("Green LED is on");
}
else if (ch == '2'){

digitalWrite(blueLED, HIGH);
Serial.println("Blue LED is on");
}
else if (ch == '3'){

digitalWrite(yellowLED, HIGH);
Serial.println("Yellow LED is on");
}
else if (ch == 'x'){

digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
digitalWrite(yellowLED, LOW);
Serial.println("Shut off");
}
}
}

void lightBulltin(int n) //This function sequntially turns on LED's 10 times
{
for(int i=0;i<n;i++)
{
digitalWrite(redLED, HIGH);
delay(500);
digitalWrite(redLED, LOW);
delay(500);
digitalWrite(greenLED, HIGH);
delay(500);
digitalWrite(greenLED, LOW);
delay(500);
digitalWrite(blueLED, HIGH);
delay(500);
digitalWrite(blueLED,LOW);
delay(500);
digitalWrite(yellowLED, HIGH);
delay(500);
digitalWrite(yellowLED, LOW);
}

}

“ I'm having trouble trying to make my lighBulltin function only be active when I press 'P' in the serial monitor.”

Where do you have:

else if (ch == 'P'){


Use CTRL T to format your code.
Use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

You should learn how to wrap your code in code tags ... it's real easy, you paste your code, then highlight it and press the little icon that looks like this </>

So that your code is easier to follow and read.

Try this, it should work:

#include <Arduino.h>

String serialData;
bool serialReceived;

const int lightPin = 4;

void setup () {
    Serial.begin(9600);
    pinMode(lightPin,OUTPUT);
}

void lightON(){
    digitalWrite(lightPin,HIGH);
}

void lightOFF() {
    digitalWrite(lightPin,LOW);
}

void checkSerial() {
    while (Serial.available() > 0) {
        String serialIn = Serial.readString();
        serialIn.trim();
        if (serialIn.length() > 1) {
            serialData = serialIn;
            serialData.toLowerCase();
        }
        if (serialData.length() > 0) {
            serialReceived = true;
        }
    }
}

void loop() {
    checkSerial();
    
    if (serialReceived) {
        if (serialData.startsWith("p")) lightON();
        else if (serialData.startsWith("o")) lightOFF();
    }
}

it's real easy, you paste your code, then highlight it and press the little icon that looks like this </>

Even easier, select the code in the IDE then right click and Copy for Forum and paste it here. The code tags are added for you

I'm sorry if the code was a bit hard to read, but I'm new to a lot of this and trying to learn as I go. The reason why I don't have else if (ch == 'P'){ is because I have to use a new function to perform that task. Unless you need to list it? I'm not too sure.

You don’t show us your new function?
You don’t show us the code that calls it? If indeed there is any....

Of course you need code to detect “P” and call your new function. How else is the Arduino to know when to do it?

would I need to declare it void loop and then call it from there or do I list it in the setup?

UKHeliBob:
Even easier, select the code in the IDE then right click and Copy for Forum and paste it here. The code tags are added for you

Is that unique to the Arduino IDE? I usually write my sketches in CLion.

Is that unique to the Arduino IDE?

Almost certainly

UKHeliBob:
Even easier, select the code in the IDE then right click and Copy for Forum and paste it here. The code tags are added for you

Even easier: if you don't select a portion of your code, Copy for Forum will copy the entire sketch (with code tags).
Remember to hit Ctrl-T (on Mac: Command-T) or Tools->Auto Format before copying your sketch for the forum.

johnwasser:
Even easier: if you don't select a portion of your code, Copy for Forum will copy the entire sketch (with code tags).
Remember to hit Ctrl-T (on Mac: Command-T) or Tools->Auto Format before copying your sketch for the forum.

It's almost silly it's so easy ... first thing that popped into my head was, "It's so easy, a caveman could do it."

LOL

It's almost like you have to try to NOT to tag your code ...

squammy:
would I need to declare it void loop and then call it from there or do I list it in the setup?

else if (ch == 'P'){
lightBulltin(1);
}