Hello Everyone,
I have made the RC Car to work but I am trying to incorporate the HMC5883L with the RC Car which is not working. In my code for now, I am trying the steer the wheels in left or right direction to correct the direction of the car first and then move onto forward-direction. These directions are nothing but the angles. Here is the sample of my code:
#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>
// Create a compass
HMC5883L_Simple Compass;
int D;
void setup()
{
pinMode(13,OUTPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);
Wire.begin();
Compass.SetDeclination(10, 17, 'W');
Compass.SetSamplingMode(COMPASS_SINGLE);
Compass.SetScale(COMPASS_SCALE_130);
Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
}
// Our main program loop.
void loop()
{
if(Serial.available()){
D = Serial.read();
Serial.println(D);
}
int heading = Compass.GetHeadingDegrees();
Serial.print("Heading: \t");
Serial.println( heading );
delay(1000);
do{
if(heading == D){
digitalWrite(13,HIGH); //Left motors, help steer to right
digitalWrite(10,HIGH); //right motors, help steer to left
}
else{
digitalWrite(10,HIGH);
}
}while (heading-D <= 180);
do{
if(heading == D){
digitalWrite(13,HIGH);
digitalWrite(10,HIGH);
}
else{
digitalWrite(13,HIGH);
}
}while (heading-D > 180);
}
Thank you