Hey everyone!
I'm a little stumped on my current project. Here's a little background:
1] Ard Pro Mini 5V/16 + HMC5883L + 315Mhz Tx (all Sparkfun) + 3.3V battery for the sensor
2] Ard UnoV3 + 315Mhz Rx
I'm trying to get the Z axis number from one board to the other and Serialprint it on the reciever.
My testing process was:
1] play around with just the HMC - worked
2] play around with just the Tx/Rx, send LED signal - worked
3] combine HMC and Tx code on the Mini and serial write HMC on Mini, Rx code on the Uno - just recieve LED signal - worked
4] leave the LED signal to be sure Tx/Rx works, also send Z-axis to reveiver (Serial.println(z)) - stumped
I tried sending just the word "test", but to no avail. I'm unsure how this part of the code for sending a text works. Also unsure how I should translate the Z-axis (Serial.println(raw.ZAxis); also works, just not sending over Rx/Tx) wirelessly.
Here's the codes:
//simple Tx on pin D6
//transmitter
#include <VirtualWire.h>
#include <Wire.h> //I2C Arduino Library
#include <HMC5883L.h>
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
// Record any errors that may occur in the compass.
int error = 0;
// Store our compass as a variable.
HMC5883L compass;
char *controller;
char *controllerraw;
void setup() {
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
pinMode(13,OUTPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(6);
vw_setup(4000);// speed of data transfer Kbps
}
void loop(){
int x,y,z; //triple axis data
// Retrive the raw values from the compass (not scaled).
MagnetometerRaw raw = compass.ReadRawAxis();
// Output the data via the serial port.
Output(raw);
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
Serial.print("z: ");
Serial.println(z);
delay(1250);
controller="test";
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
delay(200);
controller="1";
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,1);
delay(200);
controller="0" ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13,0);
delay(200);
}
// Output the data down the serial port.
void Output(MagnetometerRaw raw)
{
// Serial.print("Raw:\t");
// Serial.print(raw.XAxis);
// Serial.print(" ");
// Serial.print(raw.YAxis);
// Serial.print(" ");
// Serial.println(raw.ZAxis);
// Serial.print(" \tScaled:\t");
}
//simple Tx on pin D12
// reciever
#include <VirtualWire.h>
#include <Wire.h> //I2C Arduino Library
#include <HMC5883L.h>
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
// Record any errors that may occur in the compass.
int error = 0;
// Store our compass as a variable.
HMC5883L compass;
char *controller;
//char *controllerraw;
void setup(){
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_rx_pin(12);
vw_setup(4000); // Bits per sec
pinMode(13, OUTPUT);
vw_rx_start(); // Start the receiver PLL running
}
void loop(){
// int x,y,z; //triple axis data
// Retrive the raw values from the compass (not scaled).
// MagnetometerRaw raw = compass.ReadRawAxis();
// Output the data via the serial port.
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
if(buf[0]=='1'){
digitalWrite(13,1);
}
if(buf[0]=='0'){
digitalWrite(13,0);
}
if(buf[0]=='test'){
//Output(raw);
Serial.println("test");
}
}
}
// Output the data down the serial port.
void Output(MagnetometerRaw raw)
{
// Serial.print("Raw:\t");
// Serial.print(raw.XAxis);
// Serial.print(" ");
// Serial.print(raw.YAxis);
// Serial.print(" ");
// Serial.println(raw.ZAxis);
// Serial.print(" \tScaled:\t");
}
There may be some junk in there, I was unsure so I left a bunch of stuff that may be unused, but byte size isn't an issue.
Also I would like to know what you guys would recommend for "saving" the sensor number output form the last loop, then comparing it to the current one.
For example:
- if difference between last number and current number is greater than 20 (positive or negative), high red LED, else high green LED
1] first loop z-axis is 800 - minute delay
2] second loop z-axis is 700 - minute delay
= red LED
Edit: come to think of it, that should be just one loop, with a minute delay in the middle and end.
Any help is much appreciated ![]()