"too many arguments to function"

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I'm trying to turn on a LED RGB of a color read in the monitor serial but it says " too many arguments to function "

 

This is the code:

 

int LEDrojo = 6;
int LEDverde = 3;
int LEDazul = 5;
String entrada;

String colores[] = {"Blanco","Rojo","Lima","Azul","Amarillo","Cian","Magenta","Gris","Marrón","Olivo","Verde","Morado","Azulverde","Marino"};

int R[] = {255,255,0,0,255,0,255,128,128,128,0,128,0,0};
int G[] = {255,0,255,0,255,255,0,128,0,128,128,0,128,0};
int B[] = {255,0,0,255,0,255,255,128,0,0,0,128,128,128};

void setup()
{
  pinMode(LEDrojo, OUTPUT);
  pinMode(LEDverde, OUTPUT);
  pinMode(LEDazul, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()>0){
    entrada=Serial.readString();
    Serial.println(entrada);
    ColorLed(entrada);
  }

void ColorLed()
{
  for(int i=0; i<14; i++)
  {
    if(colores[i]=entrada)
    {
      analogWrite(LEDrojo,R[i]);
      analogWrite(LEDverde,G[i]);
      analogWrite(LEDazul,B[i]);
    }else
     {
      analogWrite(LEDrojo,0);
      analogWrite(LEDverde,0);
      analogWrite(LEDazul,0);
     }
  }
}
 

This topic has a solution.

Medical and biologycal science

Last Edited: Mon. Apr 27, 2020 - 09:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Welcome to AVRFreaks!

 

mel_delacreme wrote:
if(colores[i]=entrada)

"=" only works with numbers, you will need to do a string compare, i.e. strcmp()

example   https://www.arduino.cc/en/Tutori...

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
you will need to do a string compare, i.e. strcmp()

doesn't the C++ String class have its own methods for that ... ?

 

EDIT

 

https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/compareto

 

and, of course, '=' is assignment - not comparison ...

 

EDIT 2

 

Actually, they have overloaded the '==' (equality comparison) operator:

 

https://www.arduino.cc/en/Tutorial/StringComparisonOperators

 

 

EDIT 3

 

See Tip #1 (in my signature, below) for how to properly post source code

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Apr 27, 2020 - 09:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I bet there's some more detail besides that error text like num1:num2?
.
When the compiler gives errors as well as the filename it reports line_num:column_num. What are those?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The function  colorsLed is not defined with a parameter, but it is called with the parameter "entrada".

 

Try this function that uses entrada as an index into the table of colors, and turns the string 'entrada' into a number.

 

void ColorLed(String miEntrada)
{
    uint_8 coloresEntrada;
 
    coloresEntrada = atoi (miEntrada);

    analogWrite(LEDrojo,R[coloresEntrada]);
    analogWrite(LEDverde,G[coloresEntrada]);
    analogWrite(LEDazul,B[coloresEntrada]);

}
 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You are calling a function with an argument:

    ColorLed(entrada);

but the function expects to have zero arguments i.e.

void ColorLed()

As ki0bk has noted,  you have used = when you are comparing two Strings,

Since both items are String,  you can test for equality with ==

Or use the standard String methods e.g. entrada.compare(colores[i])

 

It looks as if this is an Arduino sketch.   Arduino only supports the common String methods.

String makes my head hurt.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OMG it worked!

thank you so much! 

 

Medical and biologycal science

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you! It really helped me :)

Medical and biologycal science

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

"Klingon functions do not have parameters. They have ARGUMENTS and they always win them."

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:-)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's normal to have small thingsto debug in your code like = instead of ==, and forgetting to pass or declare a parameter to your functions.

If you want a simple menu on your serial port you can look into:
https://github.com/sonyhome/SerialMenu
Shameless plug :P

It works by using callbacks, so when a user chooses a menu entry the function is called directly for that choice. You don't need to put an if/else statement in your main loop().

Also note in the example below we do pointer comparisons so "entrada == Blanco" actually compares the pointer entrada to the pointer Blanco, and we've either passed the pointer Blanco or Rojo. I used "char *" not "string" because it's not needed.
In your code if you pass the string entrada read from the Serial console it will be slower because "entrada == Blanco" will use the string's operator::== which will implement a string comparison, comparing every characters. String is a class not an array of characters, it is built with an array of characters inside though. It is not a pointer comparison but a value comparison.

 

    #include <SerialMenu.hpp>
    const SerialMenu& menu = SerialMenu::get();

    const char LeMenu[] PROGMEM = "Menu";
    const char Blanco[] PROGMEM = "Blanco";
    const char Rojo[] PROGMEM = "Rojo";

    const SerialMenuEntry menu1[] = {
     {LeMenu, false, 'M', [](){menu.show(); },
     {Blanco, false, 'B', [](){ColorLed(Blanco) },
     {Rojo,   true,  'R', [](){ColorLed(Rojo) }
    };
    constexpr uint8_t menu1Size = GET_MENU_SIZE(menu1);

    void ColorLed(char * entrada)
    {
      if (entrada == Blanco) {...}
    }

    void setup() {
     menu.load(menu1, menu1Size);
     menu.show();
    }

    void loop() {
      menu.run(100);
      delay(100);
    }

My GitHub (FAB_LED bitbang for ws2812, DigitalIO, SerialMenu...).
I make portable, code optimized Arduino libraries as a hobby and use AtTiny85 the most for my builds.
I am a burner and an OS and systems programmer by trade.

Last Edited: Fri. May 1, 2020 - 04:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sonyhome wrote:
It's normal to have small things to debug in your code like = instead of ==

So common, in fact, that most compilers will warn about that particular thing.

 

Does the Arduino IDE not enable that ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sonyhome wrote:
like = instead of ==
Not every one can cope with the look of it but if you put constants on the left in such tests you can avoid a lot of it. So

if (speed == 10) {

which has been mis-typed as:

if (speed = 10) {

is actually written as:

if (10 == speed) {

if you mis-type that as:

if (10 = speed) {

it's not just a warning it is an error.

 

But not every one (and I am one of those) can cope with the "reverse polish" look of such a thing !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
 not every one (and I am one of those) can cope with the "reverse polish" look of such a thing !

It is, indeed, cringe-making.

 

I try to do it because it is the "Right Thing" - not because it's a nice thing to behold.

 

And then I wonder if I should make all my other comparisons "backwards" as well - to be consistent ...

 

frown

 

if( LIMIT > value )

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I personally hate to read stuff like if (1 == var) {...}, it's an old workaround.

 

At work I had -Wall on so yes.. but I think that Arduino doesn't have warning on all the way. Likely because it's not clean code.

 

Preference -> Compiler warnings = default (you can bump to more or all)

In fact when I did turn it up I got a warning, unfortunately it's badly worded and cryptic:

warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (curr = 0)
       ~~~~~^~~

 

Another thing is I've found weird things with the sketches depending on how you declare the functions:

 

void foo()

{

}

 

versus

 

void

foo()

{

}

 

I've had cases where the latter failed with warnings when foo is declared after loop(), because there's some magic wrapper that sucks up the sketch and seems to do transformations it into a cpp file for gcc. There's magic forward references that are declared I think. 

Also I hate to see compressed code. It doesn't make the code compile better, it's just lazy, and harder to debug. I mean  "foo(bar(),barbar()" or "bool a,b; b=true; if (a = b)".

My GitHub (FAB_LED bitbang for ws2812, DigitalIO, SerialMenu...).
I make portable, code optimized Arduino libraries as a hobby and use AtTiny85 the most for my builds.
I am a burner and an OS and systems programmer by trade.

Last Edited: Fri. May 1, 2020 - 09:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sonyhome wrote:
I personally hate to read stuff like if (1 == var) {...}

Tsk Tsk - Magic Numbers ... ?

 

cheeky

 

Actually, I have found that I get used to it now, and it does (often) make sense to put the emphasis on what you're looking for - rather than where it's coming from ...

 

if( WHAT == where_from )

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...