Go Down

Topic: having syntax issues (Read 1 time) previous topic - next topic

Guy Cothal

help!!! i am used to c#...and i am getting the following error:

In function 'void doRGB(char*, int)':
error: ISO C++ forbids comparison between pointer and integer In function 'boolean checkfor2commas(char*, int)':

code:

#define STRLEN 16
char buffer[STRLEN];
int  bufferIndex = 0;
int writer[2]={0,200};
int redPin = 11;
int bluePin= 9;
int greenPin = 10;
void setup()
{

}

void loop()  
{
 if( Serial.available())
 {
   char ch = Serial.read();
   if( ch == '\r')  
   {
     buffer[ bufferIndex ] = 0;
     if(checkfor2commas(buffer,bufferIndex));
     {
       doRGB(buffer,bufferIndex);
     }
     bufferIndex = 0;
   }
   else
   {
     buffer[ bufferIndex++ ] = ch;
   }
 }
}

void doRGB(char buff[],int size)
{
int comcount = 0;
int r = 0;
int g = 0;
int b = 0;
for (int x = 0;x<size;x++)
{
  if(buff
  • == ",")
      {
        comcount++;
      }
      else
      {
        switch(comcount)
        {
          case 0:
            r = (r * 100) + int(buff
  • );
            break;
          case 1:
            g = (g * 100) + int(buff
  • );
            break;
          case 2:
            b = (b * 100) + int(buff
  • );
            break;
        }
      }
    }
     color(r,g,b);
    }

    boolean checkfor2commas(char buff[],int size)
    {
    int comcount = 0;
    for (int x = 0;x<size;x++)
    {
      if(buff
  • ==",")
      {
        comcount++;
      }
    }
    if (comcount == 2)
    {
      return true;
    }
    else
    {
      return false;
    }
    }

    void color (unsigned int red, unsigned int green, unsigned int blue)
    {
     analogWrite(redPin, 255-red);       
     analogWrite(bluePin, 255-blue);
     analogWrite(greenPin, 255-green);
     Serial.print(red);
     Serial.print(",");
     Serial.print(blue);
     Serial.print(",");
     Serial.println(green);
    }       

borref

The double quoted comma (",") in C/C++ is a constant zero terminated string and a reference to this is a pointer.  As such the compiler does not make sense of a comparison between a single character (buff
  • ) and a pointer.

    Change the double quotes to single quotes (',' is a character constant) and you should be ok.

    Code: [Select]
    if(buff[x]==",")

Guy Cothal

aaahhh...you are talking to a .Net programmer...it makes sense now

borref

It is actually the same in .NET, but the compiler/interpreter will add the necessary type conversion for you (character to string and then a string compare). C/C++ however is less forgivng in this respect.

Go Up