Q: Function syntax for while loop

I might be jumping the gun a little bit. I'm following a youtube series going over the basics of arduino. We were learning how to program an RGB LED but unfortunately, my kit came with an Anode RGB instead of a Cathode. Being the curious creature that I am, I looked up how to get it working. This introduced me into functions before I even really understood how to properly implement them. I can call a function in void setup easily without problems. It is when I try to use the serial reader to input a color where it then kind of falls apart. However, I never really called a function where the compiler is giving me problems, I only called global variables.

The problem in the code is in the paragraph "How bright do you want it? (0-255)". The compiler says " expression cannot be used as a function". I'm not sure I understand because I am only trying to read the input value from the serial, parse it to a new global var 'brightness', then print it out to the console for confirmation.

//Because of the LED provided in the kit, we are forced to use some work-arounds to get the RGB working for a Common Anode LED. The original lesson material was directed for a Common Cathode.

int LEDPower = 8;
int redPin = 6;
int greenPin = 5;
int bluePin = 3;
String color;
int voltInput;
int brightness;

void setup() {
  Serial.begin (2000000);
  pinMode (LEDPower, OUTPUT);
  pinMode (redPin, OUTPUT);
  pinMode (greenPin, OUTPUT);
  pinMode (bluePin, OUTPUT);
  digitalWrite (LEDPower, HIGH);

  Serial.println("What Color Would You Like to Choose?"); //Ask user for input
  while (Serial.available==0() ){ //Wait for user response
}
  color = Serial.readString();
  Serial.println("you have chosen the color" color);

  Serial.println("How Bright Do You Want it? (0-255)");
  while (Serial.available==0() ){
  }
  voltInput = Serial.parseInt(brightness);
  Serial.println(brightness);
  

void intensity (){
  analogWrite (redPin, voltInput);
  analogWrite (greenPin, voltInput);
  analogWrite (bluePin, voltInput);
}

void redColor (){
  analogWrite (redPin, 0);
  analogWrite (greenPin, 255);
  analogWrite (bluePin, 255);
}

void greenColor (){
  analogWrite (redPin, 255);
  analogWrite (greenPin, 0);
  analogWrite (bluePin, 255);
  }

void blueColor (){
  analogWrite (redPin, 255);
  analogWrite (greenPin, 255);
  analogWrite (bluePin, 0);
  }

void loop() {


}
 while (Serial.available==0() ){

Look carefully. You seem to have misplaced the parenthesis there. The compiler is right, 0 is not a function and you can't call the 0() function. You probably wanted those on available.

Your next error will be about having functions defined inside functions which you can't do. You lost the closing brace on the setup function. }

The bit with common anode/cathode RGB LED will not matter to your code, just how you wire it. The urge might be to use a single resistor attached to the common, don't.

Delta_G:

 while (Serial.available==0() ){

Look carefully. You seem to have misplaced the parenthesis there. The compiler is right, 0 is not a function and you can’t call the 0() function. You probably wanted those on available.

Your next error will be about having functions defined inside functions which you can’t do. You lost the closing brace on the setup function. }

haha okay cool. i feel like an idiot now on many levels. I got the code to compile finally but ran into other problems. The most problematic one being I still don’t know how to read a function using the serial reader. Because the color variable I was using was just an empty integer, I was only storing strings into it and not parsing the function’s scope like I intended.

If anyone wants to enlighten me on storing data sets from functions into strings, then I’d be honored to learn. But I may just have to press forward on this lesson without having a cathode RGB and keep doing the baby steps. It seems like most of the coding aspect for this lesson will be review of what we’ve done so far anyway. We haven’t even hit IF statements yet :confused:

DKWatson:
The bit with common anode/cathode RGB LED will not matter to your code, just how you wire it. The urge might be to use a single resistor attached to the common, don't.

It will reverse whether higher or lower numbers mean brighter or dimmer color.

Before the pedants get hold of my last statement, there will be a difference in that the ranges will be reversed.
Have a look at this article.

Busted. Missed it by a minute...

aspen1135:
If anyone wants to enlighten me on storing data sets from functions into strings, then I'd be honored to learn

Can you explain what you mean by this? Like, describe what you want it to do instead of how you think you would go about it. What data sets? All of your functions just set pins to set values. There's nothing for them to really give back to you.

I would suggest spending a day on a good C++ tutorial. If you google "C++ tutorial" there are several good ones in the first few hits. Worth spending a day going through and learning the basics of the syntax and how functions work and how to pass information to them and from them. It's not really all that hard, it's just something that you can't just figure out from trying. You kind of need to read through it at least once.

Delta_G:
Can you explain what you mean by this? Like, describe what you want it to do instead of how you think you would go about it. What data sets? All of your functions just set pins to set values. There's nothing for them to really give back to you.

I would suggest spending a day on a good C++ tutorial. If you google "C++ tutorial" there are several good ones in the first few hits. Worth spending a day going through and learning the basics of the syntax and how functions work and how to pass information to them and from them. It's not really all that hard, it's just something that you can't just figure out from trying. You kind of need to read through it at least once.

I have actually a little background in C# so I understand some minor stuff but I'm not super well versed in it.

Arduino has been significantly easier.

So I made 3 functions: redColor, greenColor, and blueColor.

Each of the functions held voltage values to turn on the LED to red, green and blue by inverting the analogWrite values (as you two were saying before).

I wanted to apply these settings as I wrote them to the Serial Reader after first getting prompted which color I wanted. (I.E "redColor" and it turning red). But I do not know how to call the functions entered in. So far, I've been reading and parsing global variables through the serial port. But each function contains 3 different combinations of analogWrite values so I cannot just assign a global var to the integers involved and read it. I need to read all 3 sets mixed in different ways which is why I made them as separate functions in the first place.

See if this thread on reading from Serial helps at all:

You'll have to read in the string and then compare it to some hard coded string in an if statement and call the function if you find a match.