Go Down

Topic: #define not working within switch case? (Read 828 times) previous topic - next topic

jerseyguy1996

if I put:

Code: [Select]
#define outOne 10
#define on 11


and then later on write:

Code: [Select]
switch(client.read()){
              case '1':
                Serial.write(outOne);
                Serial.write(on);


i get this error:

Code: [Select]
Temperature:101: error: expected primary-expression before '=' token

What am I doing wrong?

I know it is that line of code because if I change the "outOne" to the number 10 it works fine.
Arduino Uno;
Mega328

WizenedEE

The problem is somewhere else, so post your code.

You'd see that error if you said
Code: [Select]

#define outOne = 10

for example

James C4S

I would look around line 101.  (line numbers are shown in the lower left hand corner of the ide).
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

jerseyguy1996

Oops......I was programming last night and when I set up all of my #define's I did have an "=" in there.  Works now and I need to work on my attention to detail :-)
Arduino Uno;
Mega328

johnwasser


Oops......I was programming last night and when I set up all of my #define's I did have an "=" in there.  Works now and I need to work on my attention to detail :-)


Better yet, change your programming style:
Code: [Select]

const int outOne = 10;
const int on = 11;


This gives the compiler more information and lets it do more syntax checking for you.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

jerseyguy1996



Oops......I was programming last night and when I set up all of my #define's I did have an "=" in there.  Works now and I need to work on my attention to detail :-)


Better yet, change your programming style:
Code: [Select]

const int outOne = 10;
const int on = 11;


This gives the compiler more information and lets it do more syntax checking for you.


Thanks!  I will remember that and use it in my sketch.  I mainly just need to be able to keep the commands straight across multiple sketches because the commands will be sent from one arduino over bluetooth to be received by another arduino.  I was having trouble keeping things straight across my different sketches.
Arduino Uno;
Mega328

johnwasser


I mainly just need to be able to keep the commands straight across multiple sketches because the commands will be sent from one arduino over bluetooth to be received by another arduino.  I was having trouble keeping things straight across my different sketches.


One thing you can do it encapsulate the parts that are common to both sketches into a library.  You can call it something like "MyProtocol".  You would put MyProtocol.h into a MyProtocol directory inside a 'libraries' directory in your sketch folder.  Then you can #include "MyProtocol" in both sketches and anything you define in the .h file will be common to both.

If there are functions or objects common to both you can put them in MyProtocol.cpp in the same MyProtocol directory.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up