trouble with programming a calibration routine

Hi,

I'm attempting to write a calibration routine for a digital compass. the compass enters calibration mode when it receives a message "c" and exits calibration mode when it receives "e".

The code is below.

The issue I am having is that I get the appropriate message when entering "c", but I only get a value of "101" when I enter "e". I don't understand why it isn't entering the if statement assigned to that reading. Can someone possibly help me understand what is going on here? Any help or advice is much appreciated!

below is the output after entering "c" then "e"

Welcome To The HMC6352 Calibration Routine! 

Press lower case  'c' To Begin Calibration Routine
Press lower case 'e' To End Calibration Routine
99
Beginning Routine
C
101
#include <Wire.h>

void setup()
{
  Serial.begin (9600);
  Serial.println("Welcome To The HMC6352 Calibration Routine! ");
  Serial.println("");
  Serial.println("Press lower case  'c' To Begin Calibration Routine");
  Serial.println("Press lower case 'e' To End Calibration Routine");
 

}

void loop()
{

 Wire.beginTransmission(0x21);

while (Serial.available() == 0);
  int val = Serial.read();
  Serial.println(val);
  if (val == 99){
    Wire.send("C");
  Serial.println("Beginning Routine");
  Serial.println("C");
  }
   if (101 == Serial.read())
  {
    Serial.println("check");
    Wire.send("E");
    Serial.println("Ending Calibration Routine");
  }
  
  else if(val!=99 && val!=101)
    
    Serial.println("please enter only 'c' or 'e'");
  
}

but I only get a value of "101" when I enter "e"

find an ASCII table, and look up the character code for 'e'.

Never use the ASCII codes always use the char 'c' ect.

Mark

You're making this really hard on yourself. Just look for the character codes 'c' and 'e'. You should be comparing against 'val' in with the if(101==...

Define val as a char.

@awol, the conversion was in there, and it used the correct values

@mark and afremont.

I rewrote the code to make it more straightforward looking, but it is the same piece of code. When I type anything other than "c". This is the output, the code is below that.

Welcome To The HMC6352 Calibration Routine! 

Press lower case  'c' To Begin Calibration Routine
Press lower case 'e' To End Calibration Routine
c
Beginning Routine
C
e
Beginning Routine
C
f
Beginning Routine
C
 #include <Wire.h>
char c = 'c';
char e = 'e';
void setup()
{
  Serial.begin (9600);
  Serial.println("Welcome To The HMC6352 Calibration Routine! ");
  Serial.println("");
  Serial.println("Press lower case  'c' To Begin Calibration Routine");
  Serial.println("Press lower case 'e' To End Calibration Routine");
 

}

void loop()
{

 Wire.beginTransmission(0x21);

while (Serial.available() == 0);
  char b = Serial.read();
  Serial.println(b);
  if (b =c){
    Wire.send("C");
  Serial.println("Beginning Routine");
  Serial.println("C");
  }
   if (e == Serial.read())
  {
    Serial.println("check");
    Wire.send("E");
    Serial.println("Ending Calibration Routine");
  }
  
  else if(b!=c && b!=e)
    
    Serial.println("please enter only 'c' or 'e'");
  
}
if (b =c){

BZZZZT

if (e == Serial.read())How many characters here are available to read?

while (Serial.available() == 0);

Do nothing until a single character is received.

char b = Serial.read();

Read a character

   if (e == Serial.read())

Read another character, even if there isn't one available.

I think I have it running properly now

#include <Wire.h>
 #include <NewSoftSerial.h>
char c = 'c';
char e = 'e';
void setup()
{
  
  Serial.begin (9600);
  Serial.println("Welcome To The HMC6352 Calibration Routine! ");
  Serial.println("");
  Serial.println("Press lower case  'c' To Begin Calibration Routine");
  Serial.println("Press lower case 'e' To End Calibration Routine");
 

}

void loop()
{

 Wire.beginTransmission(0x21);

while (Serial.available() == 0)
{

  char b = Serial.read();
  if (c == b){
    Wire.send("C");
  Serial.println("Beginning Routine");
  Serial.println("C");
  }
   if (e == b)
  {
    Serial.println("check");
    Wire.send("E");
    Serial.println("Ending Calibration Routine");
  }
  
}

thanks everyone!

btricha2:
I think I have it running properly now

while (Serial.available() == 0)

{
  char b = Serial.read();

I think you're wrong. Change that == to != and you might be in with a chance.

The while would be better as an if ie if (Serial.available()!=0){} as you are already in loop().

Mark