RobotCompass
Plan a treasure hunt with this digital compass.
Compass
The robot has a compass module, which it uses to find its direction. This sketch will make sure the robot goes towards a certain direction.
When you hold the robot in your hands and rotate, you will the screen change, indicating direction.
NB : magnets will interfere with the compass. If you're getting unexpected results, check to make sure there are none around.
Hardware Required
- Arduino Robot
Instruction
- Upload the example, unplug USB and turn on power. 
- Place the robot on the ground. 
- After the starting screen, a graph will appear on-screen, representing the compass 
- The robot will start moving in a direction (in this example, it will start heading south). If you move it in a different direction, it will turn back to the way it wants to move. 
- If you want to change the robot's heading, in the code, look for 
 at the beginning of the code. Change this value to another number, between 0 and 359. 0 represents north, 90 is east, 180 is south, and 270 is west. Where do you want to go?- int direc=180
Try it out
Code
1/* Robot Compass2
3 The robot has an on-board compass module, with4
5 which it can tell the direction the robot is6
7 facing. This sketch will make sure the robot8
9 goes towards a certain direction.10
11 Beware, magnets will interfere with the compass12
13 readings.14
15 Circuit:16
17 * Arduino Robot18
19 created 1 May 201320
21 by X. Yang22
23 modified 12 May 201324
25 by D. Cuartielles26
27 This example is in the public domain28
29 */30
31// include the robot library32#include <ArduinoRobot.h>33#include <Wire.h>34
35int speedLeft;36int speedRight;37int compassValue;38int direc = 180;  //Direction the robot is heading39
40void setup() {41
42  // initialize the modules43
44  Robot.begin();45
46  Robot.beginTFT();47
48  Robot.beginSD();49
50  Robot.displayLogos();51}52
53void loop() {54
55  // read the compass orientation56
57  compassValue = Robot.compassRead();58
59  // how many degrees are we off60
61  int diff = compassValue - direc;62
63  // modify degrees64
65  if (diff > 180) {66
67    diff = -360 + diff;68
69  } else if (diff < -180) {70
71    diff = 360 + diff;72
73  }74
75  // Make the robot turn to its proper orientation76
77  diff = map(diff, -180, 180, -255, 255);78
79  if (diff > 0) {80
81    // keep the right wheel spinning,82
83    // change the speed of the left wheel84
85    speedLeft = 255 - diff;86
87    speedRight = 255;88
89  } else {90
91    // keep the right left spinning,92
93    // change the speed of the left wheel94
95    speedLeft = 255;96
97    speedRight = 255 + diff;98
99  }100
101  // write out to the motors102
103  Robot.motorsWrite(speedLeft, speedRight);104
105  // draw the orientation on the screen106
107  Robot.drawCompass(compassValue);108}Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.
