About comparing a char in an "if" statment.

Hello everyone,
I am very new to the arduino. For making a project i need to write the code in such a manner so that it can compare a char in an if conditional logic.

I have written a code. It doesn't show any error message while compiling. But after uploading it to the arduino it doesn't work too. Can anyone please solve this for me.

char myStrings;
int u;
int q; int r; int s; int t;
int m; int n; int o; int p;
char a, b, c, d;

void setup() {

Serial.begin(9600);
}

void loop() {

if (Serial.available() > 0 ) {

myStrings = Serial.read();
Serial.print("I received: ");
Serial.println(myStrings);
u=u+1;
if (u=1) {a=myStrings;
Serial.println(a);}
if (u=2) {b=myStrings;
Serial.println(b);}
if (u=3) {c=myStrings;
Serial.println(c);}
if (u=4) {d=myStrings;
Serial.println(d);}
}

if (a=='a')
m=1;
if (a=='b')
m=2;
if (a=='c')
m=3;
if (a=='d')
m=4;
if (a=='e')
m=5;
if (a=='f')
m=6;
if (a=='g')
m=7;
if (a=='h')
m=8;

if (c=='a')
n=1;
if (c=='b')
n=2;
if (c=='c')
n=3;
if (c=='d')
n=4;
if (c=='e')
n=5;
if (c=='f')
n=6;
if (c=='g')
n=7;
if (c=='h')
n=8;

if (b=='1')
o=1;
if (b=='2')
o=2;
if (b=='3')
o=3;
if (b=='4')
o=4;
if (b=='5')
o=5;
if (b=='6')
o=6;
if (b=='7')
o=7;
if (b=='8')
o=8;

if (d=='1')
p=1;
if (d=='2')
p=2;
if (d=='3')
p=3;
if (d=='4')
p=4;
if (d=='5')
p=5;
if (d=='6')
p=6;
if (d=='7')
p=7;
if (d=='8')
p=8;

q=n-m; r=p-o;
s= abs(q);
t=abs(r);
}

You know, you can use whole words for variable names so you can tell later which one is which. You're not limited to single letters.

What do you mean when you say it doesn't work? What actually happens? How does that compare with what you wanted?

A useful simplification for you. Not an answer to your question sorry, but it will combine lots of those lines into just four equivalent ones.

...
if( (a >= 'a') && (a <= 'h')) m = a -'a' +1;
if( (b >= 'a') && (b <= 'h')) n = b -'a' +1;
if( (c >= '1') && (c <= '8')) o = c -'1' +1;
if( (d >= '1') && (d <= '8')) p = d -'1' +1;
...
 if (u=1) {a=myStrings;

Why set u to 1?

Mark

Yes, fix these:

                u=u+1;
                if (u=1) {a=myStrings;
                Serial.println(a);}
                if (u=2) {b=myStrings;
                Serial.println(b);}
                if (u=3) {c=myStrings;
                Serial.println(c);}
                if (u=4) {d=myStrings;
                Serial.println(d);}

to use u == #

Does this line
u=u+1;
ever get set back to u = 0?
Or does it get just increment until u equals some ridiculous high number?

char myStrings;

I hope this doesn’t imply that you’ll do other stupid shit like:

int myFloats;
float myChars;
char myFloats;

Delta_G:
What do you mean when you say it doesn't work? What actually happens? How does that compare with what you wanted?

the arduino doesn't process the comparison. I want to give the arduino a serial command in a format "a2a3" which means it will make the output "1 2 1 3" where a, b, c.... is 1, 2,3...

ok… here is my full code… please help me fix it… i need to run the motors when a coordinate is given by serial in “a2b3” format which means it will move a thing from a2 posion to b3 position.

#include <AFMotor.h>
#include <Servo.h>

int Electromagnet = 2;

AF_Stepper motor1(32, 1);
AF_Stepper motor2(32, 2);

char myStrings;
int u;
int q; int r; int s; int t;
int m; int n; int o; int p;
char a, b, c, d;

void setup() {
pinMode(Electromagnet, OUTPUT);
motor1.setSpeed(500);
motor2.setSpeed(500);

Serial.begin(9600);
}

void loop() {

if (Serial.available() > 0 ) {
myStrings = Serial.read();

Serial.print("I received: ");
Serial.println(myStrings);
u=u+1;
if (u=1) {a=myStrings;
Serial.println(a);}
if (u=2) {b=myStrings;
Serial.println(b);}
if (u=3) {c=myStrings;
Serial.println(c);}
if (u=4) {d=myStrings;
Serial.println(d);}
u=0;
}

if (a==‘a’)
m=1;
if (a==‘b’)
m=2;
if (a==‘c’)
m=3;
if (a==‘d’)
m=4;
if (a==‘e’)
m=5;
if (a==‘f’)
m=6;
if (a==‘g’)
m=7;
if (a==‘h’)
m=8;

if (c==‘a’)
n=1;
if (c==‘b’)
n=2;
if (c==‘c’)
n=3;
if (c==‘d’)
n=4;
if (c==‘e’)
n=5;
if (c==‘f’)
n=6;
if (c==‘g’)
n=7;
if (c==‘h’)
n=8;

if (b==‘1’)
o=1;
if (b==‘2’)
o=2;
if (b==‘3’)
o=3;
if (b==‘4’)
o=4;
if (b==‘5’)
o=5;
if (b==‘6’)
o=6;
if (b==‘7’)
o=7;
if (b==‘8’)
o=8;

if (d==‘1’)
p=1;
if (d==‘2’)
p=2;
if (d==‘3’)
p=3;
if (d==‘4’)
p=4;
if (d==‘5’)
p=5;
if (d==‘6’)
p=6;
if (d==‘7’)
p=7;
if (d==‘8’)
p=8;

q=n-m; r=p-o;
s= abs(q); t=abs(r);

if ( q>0 && r>0 ) //1
{
motor1.step(100m-1, FORWARD, DOUBLE);
delay (500);
motor2.step(100
o-1, FORWARD, DOUBLE);
delay (500);
digitalWrite(Electromagnet, HIGH);
delay (500);
motor1.step(100.5, FORWARD, DOUBLE);
delay (500);
motor2.step(100.5, FORWARD, DOUBLE);
delay (500);
motor2.step(100t-1, FORWARD, DOUBLE);
delay (500);
motor1.step(100
s-1, FORWARD, DOUBLE);
delay (500);
motor1.step(100.5, FORWARD, DOUBLE);
delay (500);
motor2.step(100.5, FORWARD, DOUBLE);
delay (500);
digitalWrite(Electromagnet, LOW);
delay (500);
motor2.step(100p-1, BACKWARD, DOUBLE);
delay (500);
motor1.step(100
n-1, BACKWARD, DOUBLE);
delay (500);
}

if ( q>0 && r<0 ) //2
{

motor1.step(m-1, FORWARD, DOUBLE);
delay (500);
motor2.step(o-1, FORWARD, DOUBLE);
delay (500);
digitalWrite(Electromagnet, HIGH);
delay (500);
motor1.step(0.5, FORWARD, DOUBLE);
delay (500);
motor2.step(0.5, BACKWARD, DOUBLE);
delay (500);
motor2.step(t-1, BACKWARD, DOUBLE);
delay (500);
motor2.step(q-1, FORWARD, DOUBLE);
delay (500);
motor1.step(0.5, FORWARD, DOUBLE);
delay (500);
motor2.step(0.5, BACKWARD, DOUBLE);
delay (500);

digitalWrite(Electromagnet, LOW);
delay (500);
motor2.step(p-1, BACKWARD, DOUBLE);
delay (500);
motor1.step(n-1, BACKWARD, DOUBLE);
delay (500);

}

if ( q<0 && r>0 ) //3
{

motor1.step(m-1, FORWARD, DOUBLE);
delay (500);
motor2.step(o-1, FORWARD, DOUBLE);
delay (500);
digitalWrite(Electromagnet, HIGH);
delay (500);

motor2.step(0.5, FORWARD, DOUBLE);
delay (500);
motor1.step(0.5, BACKWARD, DOUBLE);
delay (500);
motor1.step(s-1, BACKWARD, DOUBLE);
delay (500);
motor2.step(t-1, FORWARD, DOUBLE);
delay (500);
motor2.step(0.5, FORWARD, DOUBLE);
delay (500);
motor1.step(0.5, BACKWARD, DOUBLE);
delay (500);

digitalWrite(Electromagnet, LOW);
delay (500);
motor2.step(p-1, BACKWARD, DOUBLE);
delay (500);
motor1.step(n-1, BACKWARD, DOUBLE);
delay (500);

}

if ( q<0 && r<0 ) //4
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(s-1, BACKWARD, DOUBLE); delay (500);
motor2.step(t-1, BACKWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}

if ( q==0 && r>0 ) //5
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor1.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(t-1, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, FORWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}

if ( q==0 && r<0 ) //6
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor1.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor2.step(t-1, BACKWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}

if ( q>0 && r==0 ) //7
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor1.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(s-1, FORWARD, DOUBLE); delay (500);
motor1.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}

if ( q<0 && r==0 ) //8
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor2.step(0.5, FORWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(s-1, BACKWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}
}

I am trying to make a chess robot where i will give input to the arduino as a format “a2b3”. I have done the code but it’s not working. In the comparison step which is done using if statment doesn’t work. Here is my code. please help me fix this.

#include <AFMotor.h>
#include <Servo.h>

int Electromagnet = 2;

AF_Stepper motor1(32, 1);
AF_Stepper motor2(32, 2);

char myStrings;
int u;
int q; int r; int s; int t;
int m; int n; int o; int p;
char a, b, c, d;

void setup() {
pinMode(Electromagnet, OUTPUT);
motor1.setSpeed(500); // 10 rpm
motor2.setSpeed(500); // 10 rpm

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
// send data only when you receive data:
if (Serial.available() > 0 ) {
// read the incoming byte:
myStrings = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(myStrings);
u=u+1;
if (u=1) {a=myStrings;
Serial.println(a);}
if (u=2) {b=myStrings;
Serial.println(b);}
if (u=3) {c=myStrings;
Serial.println(c);}
if (u=4) {d=myStrings;
Serial.println(d);}
u=0;
}

if (a==‘a’)
m=1;
if (a==‘b’)
m=2;
if (a==‘c’)
m=3;
if (a==‘d’)
m=4;
if (a==‘e’)
m=5;
if (a==‘f’)
m=6;
if (a==‘g’)
m=7;
if (a==‘h’)
m=8;

if (c==‘a’)
n=1;
if (c==‘b’)
n=2;
if (c==‘c’)
n=3;
if (c==‘d’)
n=4;
if (c==‘e’)
n=5;
if (c==‘f’)
n=6;
if (c==‘g’)
n=7;
if (c==‘h’)
n=8;

if (b==‘1’)
o=1;
if (b==‘2’)
o=2;
if (b==‘3’)
o=3;
if (b==‘4’)
o=4;
if (b==‘5’)
o=5;
if (b==‘6’)
o=6;
if (b==‘7’)
o=7;
if (b==‘8’)
o=8;

if (d==‘1’)
p=1;
if (d==‘2’)
p=2;
if (d==‘3’)
p=3;
if (d==‘4’)
p=4;
if (d==‘5’)
p=5;
if (d==‘6’)
p=6;
if (d==‘7’)
p=7;
if (d==‘8’)
p=8;

q=n-m; r=p-o;
s= abs(q); t=abs(r);

if ( q>0 && r>0 ) //1
{
motor1.step(100m-1, FORWARD, DOUBLE);
delay (500);
motor2.step(100
o-1, FORWARD, DOUBLE);
delay (500);
digitalWrite(Electromagnet, HIGH);
delay (500);
motor1.step(100.5, FORWARD, DOUBLE);
delay (500);
motor2.step(100.5, FORWARD, DOUBLE);
delay (500);
motor2.step(100t-1, FORWARD, DOUBLE);
delay (500);
motor1.step(100
s-1, FORWARD, DOUBLE);
delay (500);
motor1.step(100.5, FORWARD, DOUBLE);
delay (500);
motor2.step(100.5, FORWARD, DOUBLE);
delay (500);
digitalWrite(Electromagnet, LOW);
delay (500);
motor2.step(100p-1, BACKWARD, DOUBLE);
delay (500);
motor1.step(100
n-1, BACKWARD, DOUBLE);
delay (500);
}

if ( q>0 && r<0 ) //2
{

motor1.step(m-1, FORWARD, DOUBLE);
delay (500);
motor2.step(o-1, FORWARD, DOUBLE);
delay (500);
digitalWrite(Electromagnet, HIGH);
delay (500);
motor1.step(0.5, FORWARD, DOUBLE);
delay (500);
motor2.step(0.5, BACKWARD, DOUBLE);
delay (500);
motor2.step(t-1, BACKWARD, DOUBLE);
delay (500);
motor2.step(q-1, FORWARD, DOUBLE);
delay (500);
motor1.step(0.5, FORWARD, DOUBLE);
delay (500);
motor2.step(0.5, BACKWARD, DOUBLE);
delay (500);

digitalWrite(Electromagnet, LOW);
delay (500);
motor2.step(p-1, BACKWARD, DOUBLE);
delay (500);
motor1.step(n-1, BACKWARD, DOUBLE);
delay (500);

}

if ( q<0 && r>0 ) //3
{

motor1.step(m-1, FORWARD, DOUBLE);
delay (500);
motor2.step(o-1, FORWARD, DOUBLE);
delay (500);
digitalWrite(Electromagnet, HIGH);
delay (500);

motor2.step(0.5, FORWARD, DOUBLE);
delay (500);
motor1.step(0.5, BACKWARD, DOUBLE);
delay (500);
motor1.step(s-1, BACKWARD, DOUBLE);
delay (500);
motor2.step(t-1, FORWARD, DOUBLE);
delay (500);
motor2.step(0.5, FORWARD, DOUBLE);
delay (500);
motor1.step(0.5, BACKWARD, DOUBLE);
delay (500);

digitalWrite(Electromagnet, LOW);
delay (500);
motor2.step(p-1, BACKWARD, DOUBLE);
delay (500);
motor1.step(n-1, BACKWARD, DOUBLE);
delay (500);

}

if ( q<0 && r<0 ) //4
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(s-1, BACKWARD, DOUBLE); delay (500);
motor2.step(t-1, BACKWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}

if ( q==0 && r>0 ) //5
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor1.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(t-1, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, FORWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}

if ( q==0 && r<0 ) //6
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor1.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor2.step(t-1, BACKWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}

if ( q>0 && r==0 ) //7
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor1.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(s-1, FORWARD, DOUBLE); delay (500);
motor1.step(0.5, FORWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}

if ( q<0 && r==0 ) //8
{

motor1.step(m-1, FORWARD, DOUBLE); delay (500);
motor2.step(o-1, FORWARD, DOUBLE); delay (500);
digitalWrite(Electromagnet, HIGH); delay (500);

motor2.step(0.5, FORWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(s-1, BACKWARD, DOUBLE); delay (500);
motor2.step(0.5, BACKWARD, DOUBLE); delay (500);
motor1.step(0.5, BACKWARD, DOUBLE); delay (500);

digitalWrite(Electromagnet, LOW); delay (500);
motor2.step(p-1, BACKWARD, DOUBLE); delay (500);
motor1.step(n-1, BACKWARD, DOUBLE); delay (500);

}
}

Please edit your post and use code tags.
** **[code]** **
your code here
** **[/code]** **
will result in

 your code here

Based on below excerpt of your code, I don’t think that the variable ‘u’ will ever have the values 2, 3 or 4? The last line resets ‘u’ to 0. I strongly suggest that you read serial input basics and learn how to use arrays.

  if (Serial.available() > 0 ) {
    myStrings = Serial.read();

    Serial.print("I received: ");
    Serial.println(myStrings);
    u = u + 1;
    if (u = 1) {
      a = myStrings;
      Serial.println(a);
    }
    if (u = 2) {
      b = myStrings;
      Serial.println(b);
    }
    if (u = 3) {
      c = myStrings;
      Serial.println(c);
    }
    if (u = 4) {
      d = myStrings;
      Serial.println(d);
    }
    u = 0;
  }

Next, don’t use single character variable names and ABSOLUTELY do not use them for global variables. Give those variables sensible names. The variable name ‘u’ above is probably meant as a kind of an index. So call it something like ‘index’.

I think it was already mentioned that something like below

  if (a == 'a')
    m = 1;
  if (a == 'b')
    m = 2;
  if (a == 'c')
    m = 3;
  if (a == 'd')
    m = 4;
  if (a == 'e')
    m = 5;
  if (a == 'f')
    m = 6;
  if (a == 'g')
    m = 7;
  if (a == 'h')
    m = 8;

can easily be replaced by

if(a>='a' && a <='h')
{
  m = a - 'a' + 1;
}

So what does the letter ‘m’ stand for? Give it a decent name and while you’re doing so, give those other single character variables decent names as well.

if (u=1) {a=myStrings;
                Serial.println(a);}
                if (u=2) {b=myStrings;
                Serial.println(b);}
                if (u=3) {c=myStrings;
                Serial.println(c);}
                if (u=4) {d=myStrings;
                Serial.println(d);}

Garbage. Pure unadulterated garbage. Just like in your other post.

I have done the code but it's not working. I

I'm not totally surprised. Try doing some C tutorials.

Oh, and read the forum "stickies". In particular the part about using code tags.

 if (a=='a')
        m=1;
        if (a=='b')
        m=2;
        if (a=='c')
        m=3;
        if (a=='d')
        m=4;
        if (a=='e')
        m=5;
        if (a=='f')
        m=6;
        if (a=='g')
        m=7;
        if (a=='h')
        m=8;

Is there a pattern there? Perhaps you can do a simple subtraction?

Duplicate of this thread

Stop cross-posting.

Thread locked.

Iftekhar:
I am very new to the arduino.

Two-day ban for cross-posting. Are you new to the Internet as well?

sterretje:
Duplicate of this thread

It's generally better to use the Report to Moderator link, and let the moderators deal with the idiots that cross-post.

OK, thanks.

//Edit: Reported :wink:

. . . and the OP is enjoying a brief forum timeout, to give him/her a chance to read the posting guidelines.