![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mini Project 10: Displaying stuff from day 7
Dependencies: DmTouch_UniGraphic UniGraphic mbed
Diff: compass_sensor.cpp
- Revision:
- 11:482a9abbc448
- Parent:
- 0:1ebe73e062a7
- Child:
- 12:bd1d030d5c30
--- a/compass_sensor.cpp Tue Jan 17 20:22:49 2017 +0000 +++ b/compass_sensor.cpp Tue Jan 17 20:34:32 2017 +0000 @@ -8,22 +8,13 @@ double heading; //the "degree" variable double sum; //used for calculating the average -int compass_config(void) //returns the number of samples and configures the sensor +void compass_config(void) //configures the sensor { - pc.printf("Enter a number of samples you would like.\r\n"); - int c; - pc.scanf("%c", &c); - pc.printf("\r\nValue:%c", c); - if (c != 1 && c != 2 && c != 4 && c != 8) //if the average number is not 1, 2, 4, and 8, then set it to 1 - { - c = 1; - } i2c_port.start(); //starts the compass i2c_port.write(addr); //sets the address i2c_port.write(0x02); //writes the bytes i2c_port.write(0x00); //writes the bytes i2c_port.stop(); //stops - return (c); //returns the number of samples } double calculation(void) @@ -44,24 +35,24 @@ return heading; } -void compass_n(int c) +float compass_n() { wait(0.02); i2c_port.start(); //starts the compass i2c_port.write(addr); //compass is given its address again i2c_port.write(0x03); //told to given 3 bytes i2c_port.stop(); - for (int m = 0; m<c; m++) + for (int m = 0; m<2; m++) { heading = calculation(); //function that calculates the degrees from N sum += heading; //sum of the heading(s) } - heading = sum/c; //the average of the heading(s) collected + heading = sum/2; //the average of the heading(s) collected if (heading > 180){ heading = heading - 360; //if greater than 180, then subtract 360 since it was too large } if (heading < -180){ heading = heading +360; //if less than -180, then add 360 since it was too small } - pc.printf("North is %.2f. \n\r ", heading); //print the averaged reading + return heading; } \ No newline at end of file