I new to Arduino and I'm attempting to writte a simple string compare code like this:
void loop()
{
distance(cm);
delay(200);
}
void distance(char[3] unit)
{
if (unit[] == "cm")
Serial.println("cm");
}
Could somebody please advise me how to writte it correctly? Thanks in advance!
1 Answer 1
if (strcmp (unit,"cm") == 0) {
// matches cm
}
For syntax like this the intellisense in products such as Atmel Studio or Visual Studio can really help. Whilst you still need to understand what you might be looking for the intellisense makes all the available Arduino functions visible from the code. If you install the Arduino plugin then there will be an Arduino reference explorer in both ide's which explains everything.
-
-
@PhillyNJ Hi, not true. It is entirely correct, what users often doesn't realize will help most. + I didn't post any links!Visual Micro– Visual Micro2015年07月09日 15:07:15 +00:00Commented Jul 9, 2015 at 15:07
-
Shameless or not, Atmel Studio 7 Beta runs all the Arduino libraries with no plugin.PhillyNJ– PhillyNJ2015年07月09日 15:10:28 +00:00Commented Jul 9, 2015 at 15:10
-
Sure you can use the import tool to convert some Arduino projects to cpp projects. they are then no longer Arduino compatible, no ability to easily take core upgrades from Arduino, no usb upload, only a debugger if you hack your board, only debug with optimization disabled, difficult to add new libraries and the list goes on. Great for some pro users but not for others. Have you tried it? maybe not?Visual Micro– Visual Micro2015年07月09日 15:15:15 +00:00Commented Jul 9, 2015 at 15:15
-
Yes - Both Visual Macro and Beta 7.PhillyNJ– PhillyNJ2015年07月09日 15:31:36 +00:00Commented Jul 9, 2015 at 15:31
char unit[3]
, or, betterchar * unit
. And you call likedistance("cm")
, with the quotes.== 0
to do a comparison.