can i make multiple if(strcmp) condition?
ex.
if (strcmp (numbers,"A") == 0) {
Serial.print("Letter A");
}
if(strcmp(numbers,"B")==0{
Serial.print("Letter B");
}
if(strcmp(numbers,"C")==0{
Serial.print("Letter C");
}
can i make multiple if(strcmp) condition?
ex.
if (strcmp (numbers,"A") == 0) {
Serial.print("Letter A");
}
if(strcmp(numbers,"B")==0{
Serial.print("Letter B");
}
if(strcmp(numbers,"C")==0{
Serial.print("Letter C");
}
Do you mean using the && operator?
just choosing one among the three sir,
if i type A
display: Serial.print("Letter A");
if i type B again
Serial.print("Letter B");
if i type A again
Serial.print("Letter A");
is it possible?
i wanted to call letter "C", but how?
if (strcmp (numbers,"A") == 0) {
Serial.print("Letter A");
}
if(strcmp(numbers,"B")==0{
Serial.print("Letter B");
}
if(strcmp(numbers,"C")==0{
Serial.print("Letter C");
}
or
if(!strcmp(numbers,"A")==0){
Serial.print("Letter A");
else if(!strcmp(numbers,"B")==0){
else
{
Serial.print("Failed");
}
strcmp() does exactly what it says. It compares strings. If the strings don't match exactly, it will not return 0.
What is in numbers?
In this case it can only contain a single character and a terminating '\0', no crlf, no punctuation, no other characters.
sorry for late reply i was experimenting for this problem.
here's my code, I2C
in master:
Void loop()
{
if(strstr(message, "A")) //when i send it will transmit a byte.
{
char c='A';
Wire.beginTransmission(5);
Wire.write(c);
Wire.endTransmission();
}
if(strstr(message,"AA"))
{
char c='B';
Wire.beginTransmission(5);
Wire.write(c);
Wire.endTransmission();
}
}
In slave: where here's the problem
char numbers[20]={
};
void setup()
{
Wire.begin(5);
Wire.onReceive(receiveEvent);
}
void loop()
{
}
void receiveEvent(int howMany)
{
while(Wire.available() > 0)
{
numbers[i++] = Wire.read(); //this will receive the value from "Master" whether it's 'A' or 'B'
}
if (strcmp (numbers,"A") == 0) // numbers 'A'
{
Serial.print("Success A");
}
if (strcmp (numbers,"B") == 0) //numbers 'B'
{
Serial.print("Success B");
}
}
But then, when i Send "AA" that should be c='B'
it will read and accept the "if (strcmp (numbers,"A") == 0) which this should be read and display >>> (strcmp (numbers,"B") == 0)
thats my problem...
numbers is not getting a '\0' to signal the end of the string that strcmp() requires to know how long the string is.
So that was the only problem ![]()
adding numbers[20]='\0';
great! thank you, its working now. ![]()
A couple of thoughts:
Instead of
if (strcmp (numbers,"A") == 0) // numbers 'A'
{
Serial.print("Success A");
}
if (strcmp (numbers,"B") == 0) //numbers 'B'
{
Serial.print("Success B");
}
You can avoid a redundant if test by using an if-else clause:
if (strcmp (numbers,"A") == 0) // numbers 'A'
{
Serial.print("Success A");
} else
if (strcmp (numbers,"B") == 0) //numbers 'B'
{
Serial.print("Success B");
}
This way, if numbers is "A", the if test for "B" is not performed.
If your code really is checking for only a single letter, something like this seems easier to read to me:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
char letter;
if (Serial.available() > 0) {
letter = toupper(Serial.read());
switch (letter) {
case 'A':
// Do whatever...
break;
case 'B':
// Do whatever...
break;
case 'C':
// Do whatever...
break;
default:
letter = '?';
break;
}
Serial.print("Letter ");
Serial.println(letter);
}
}
aspirines:
So that was the only problemadding numbers[20]='\0';
great! thank you, its working now.
You really need to put it after the last character you put into the array, not past the end of the array.
(Hint)
thanks sir econjack i'll try also the switch case. ![]()
You better rethink your code. That should not have worked. That would make numbers a 21 character long string that should never compare equal to a single character string like "A". How do you prevent numbers from overwriting past its length?
thanks sir, im also exploring this code. The numbers[i++] will not only store a single char 'A','B' but i will also pass a string from master to slave. that's why i made it numbers[i++].. well, just experimenting at the same time, my project ![]()