1

I am getting the error in my title when I try to compile the following code:

#define char incomingByte; //Defines incomingByte 
#define char sendBack = K;
#define char ?;
#define pollTime;

bool running = true;

void setup() {
Serial.begin(9600); //Set data rate.
}

void loop() {
for(Serial.available() > 0;)  { //Gets you the number of bytes that are available to be read from the serial port.
pollTime = delayMicroseconds(200); // Sets a poll time of 200 microseconds.
incomingByte  = Serial.read();
if(incomingByte = ?) {
Serial.print(sendBack);
  }
 }
}  
3

Your code is completely wrong in almost every respect.


#define char incomingByte; //Defines incomingByte 

That line means that the compiler will substitute incomingByte; whenever it sees char.


#define char sendBack = K;

Now it will substitute sendBack = K; when it sees char.


#define char ?;

Now it will substitute ?; when it sees char.


#define pollTime;

I don't know what you are thinking here. If you are trying to declare a variable you have to give a type, like this:

int pollTime;

bool running = true;

void setup() {
Serial.begin(9600); //Set data rate.
}
void loop() {

Those lines are OK.


for(Serial.available() > 0;)  { //Gets you the number of bytes that are available to be read from the serial port.

You don't want the semicolon there. A for loop is not written like that. You possibly mean while, ie.

while(Serial.available() > 0) 

pollTime = delayMicroseconds(200); // Sets a poll time of 200 microseconds.

That is doing nothing of the sort. It is delaying 200 µs, it isn't setting anything.


incomingByte  = Serial.read();

That line is OK, assuming you declared incomingByte like this, and not the way you did:

char incomingByte; 

if(incomingByte = ?) {

That needs to be in single quotes, as you are comparing a character. Also you compare with == not = ie.

if(incomingByte == '?') {

Serial.print(sendBack);

It looks from your earlier code you mean:

Serial.print('K');

  }
 }
}  

These lines look OK.


I suggest you read a beginners page about C programming. This is just pure guesswork what you are doing here. For one thing, read up on how #define works. Here is one page: https://www.techonthenet.com/c_language/constants/create_define.php. And another: http://www.cprogramming.com/reference/preprocessor/define.html

3
  • Thank you. This was very helpful. I'll look into the links that you provided. Oct 16 '16 at 15:31
  • P.S. you should change "respect" to "aspect" in the first line
    – Dat Ha
    Oct 17 '16 at 0:15
  • 2
    I'm not sure about that. See in all respects which means: entirely, outright, wholly, thoroughly, altogether, totally, utterly
    – Nick Gammon
    Oct 17 '16 at 10:39
0

[UPDATE]

Try this:

int question = 63; //in the ascii table, ? is 63

void setup() {
  Serial.begin(9600); //set baud rate.
}

void loop() {
  if (Serial.available())  { //Gets you the number of bytes that are available to be read from the serial port.
    if (Serial.read() == question) {
      Serial.println("OK");
    }
  }
}
6
  • When I run the code it gives the following error: Oct 16 '16 at 0:50
  • invalid conversion from 'const char*' to 'char' [-fpermissive] Oct 16 '16 at 0:50
  • refresh the page and test the new code
    – Dat Ha
    Oct 16 '16 at 0:59
  • Thank you. This part worked. I forgot to mention that I also need it to turn off the LED if the condition is not met. When I run the code you have provided the LED is on. Oct 16 '16 at 1:04
  • 3
    There's really no reason to use the ascii code for a question mark here, it makes the program hard to understand. Use '?'your earlier error was a result of using a string (double quotes) rather than a single character (single quotes). Oct 16 '16 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.