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'");
}
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==...
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'");
}
#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");
}
}