Bonjour tout le monde,
Je suis en train de travailler sur un Projet Arduino actuellement.
J'aimerai faire un truc qui semble assez simple mais sur lequel je suis bloquée depuis peu.
Je dois commander via une liaison Bluetooth un compas digital monté sur le premier Arduino, qui à son tour via une liaison RF commande un servomoteur monté sur le 2ème.
La liaison bluetooth se fait correctement, de l’émetteur au récepteur je pense que ça passe aussi vu que je visualise sur le moniteur série toutes les valeurs prises par le compas..... Seulement le Servomoteur ne bouge pas comme je le souhaite, il réagit juste un peu au début puis rien après.
Voici le programme pour l'émetteur:
// Reference the I2C Library
#include <Wire.h>
#include <HMC5883L.h>
#include <VirtualWire.h>
const int led_pin = 13;
const int transmit_pin = 3;
const int receive_pin = 11;
const int transmit_en_pin = 12;
const int servo_pin=8;
// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;
char msg[6];
int x;
// Out setup routine, here we will configure the microcontroller and compass.
void setup()
{
// Initialize the serial port.
Serial.begin(9600);
Serial.println("Starting the I2C interface.");
Wire.begin(); // Start the I2C interface.
Serial.println("Constructing new HMC5883L");
compass = HMC5883L(); // Construct a new HMC5883 compass.
Serial.println("Setting scale to +/- 1.3 Ga");
error = compass.SetScale(1.3); // Set the scale of the compass.
if(error != 0) // If there is an error, print it out.
Serial.println(compass.GetErrorText(error));
Serial.println("Setting measurement mode to continous.");
error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(error != 0) // If there is an error, print it out.
Serial.println(compass.GetErrorText(error));
vw_setup(2000);
vw_set_tx_pin(receive_pin);
vw_set_tx_pin(transmit_pin);
}
// Our main program loop.
void loop()
{
// Retrive the raw values from the compass (not scaled).
MagnetometerRaw raw = compass.ReadRawAxis();
// Retrived the scaled values from the compass (scaled to the configured scale).
MagnetometerScaled scaled = compass.ReadScaledAxis();
// Values are accessed like so:
int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(scaled.YAxis, scaled.XAxis);
float declinationAngle = 0.0457;
heading += declinationAngle;
// Correct for when signs are reversed.
if(heading < 0)
heading += 2*PI;
// Check for wrap due to addition of declination.
if(heading > 2*PI)
heading -= 2*PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180/M_PI;
// Output the data via the serial port.
Output(raw, scaled, heading, headingDegrees);
}
// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
{
Serial.print(headingDegrees);
Serial.println(" Degrees \t");
dtostrf(headingDegrees,6, 2, msg);
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
delay(200);
}
***Le programme suivant est celui du receveur:
#include <VirtualWire.h>
#include <ServoTimer2.h>
ServoTimer2 myservo;
void setup()
{
Serial.begin(9600); // Debugging only
myservo.attach(9);
Serial.println("setup");
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]);
Serial.print(" ");
}
Serial.println(" ");
Serial.println(strtod ((char *) buf, NULL));
delay(50);
myservo.write(strtod ((char *) buf, NULL));
}
}
Aurais-je fais une erreur dans mes codes?