I have a Nano connected to a 10DOF sensor module and when I test it with a sketch called "magsensor" it seems to be working.
When I go back to my own code including sensor input (which I copied from somewhere), I get a compiler error that "sensors_event_t event" was declared twice.
I can't find the source of this code so I would appreciate any help as to why this error is occurring and how to avoid it.
Error:
Arduino: 1.6.13 (Windows 7), Board: "Arduino Nano, ATmega328"
C:\Users\Dianne\Documents\Arduino\Transmit_2_0_1\Transmit_2_0_1.ino: In function 'void sensorRead()':
Transmit_2_0_1:149: error: redeclaration of 'sensors_event_t event'
sensors_event_t event;
^
C:\Users\Dianne\Documents\Arduino\Transmit_2_0_1\Transmit_2_0_1.ino:128:19: note: 'sensors_event_t event' previously declared here
sensors_event_t event;
^
exit status 1
redeclaration of 'sensors_event_t event'
Code:
/*
Telemetry Transmit with 10DOF Sensors
*/
#include <SoftwareSerial.h>
// software serial #1: RX = digital pin 10 (not used), TX = digital pin 9
SoftwareSerial portOne(10, 9);
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_10DOF.h>
// Assign a unique ID to the sensors
Adafruit_10DOF dof = Adafruit_10DOF();
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(18001);
//Define soft variables
int compass = 0 ; // Compass in degrees, default 0 degrees
int pressure = 600 ; // Pressure in Pascals,
int pitch = 0 ; // pitch in degrees
int checksum = 0 ;
// Timing
int tick = 0 ; // 1 Second clock
unsigned long currentT = 0 ;
unsigned long lastT = 0 ;
struct DOFDATA
{
uint16_t compass;
uint16_t pressure;
uint16_t pitch;
uint16_t checksum ;
};
float heading ;
//************** SETUP ***********************
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.begin(9600);
Serial.println("Tx Started"); Serial.println("");
// Initialise the sensor
if(!mag.begin())
{
// There was a problem detecting the LSM303 ... check your connections
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
// Initialise the peressure sensor
if(!bmp.begin())
{
// There was a problem detecting the BMP085 ... check your connections
Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
while(1);
}
// Start software serial port
portOne.begin(600);
}
//************** LOOP ***********************
void loop() {
// 1 Second timer loop
(currentT = (millis()/1000)) ;
if (currentT > lastT) {
(lastT = currentT ) ;
tick++ ;
}
if (tick >= 6) {
(tick = 0) ;
}
switch(tick){
case 3: {
upData () ; // update data
// sensorRead() ; // read sensors
tick++ ;
break ;
}
case 4: {
sendData () ; //transmit data
tick++ ;
break ;
}
}
}
void sensorRead(){
// *************** HEADING *******************
// Output 0 to 359 Degrees in heading in Degrees *
// Get a new sensor event
sensors_event_t event;
mag.getEvent(&event);
float Pi = 3.14159;
// Calculate the angle of the vector y,x
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
Serial.print("Compass Heading: ");
Serial.println(heading);
// *************** Pressure *******************
// -20 to +20 Degrees, ffec to 14$
// ***
// Initialise the sensor
// Get a new sensor event
sensors_event_t event;
bmp.getEvent(&event);
// Display the results (barometric pressure is measure in hPa)
if (event.pressure)
{
// Display atmospheric pressure in hPa
Serial.print("Pressure: "); Serial.print(event.pressure); Serial.println(" hPa");
}
else
{
Serial.println("Sensor error");
}
// *************** Inclinometer *******************
// -20 to +20 Degrees, ffec to 14$
sensors_event_t accel_event;
sensors_vec_t orientation;
// Calculate pitch and roll from the raw accelerometer data
accel.getEvent(&accel_event);
if (dof.accelGetOrientation(&accel_event, &orientation))
{
// 'orientation' should have valid .roll and .pitch fields
Serial.print(F("Roll: "));
Serial.print(orientation.roll);
Serial.print(F("; "));
Serial.print(F("Pitch: "));
Serial.print(orientation.pitch);
Serial.print(F("; "));
}
return ;
}
// ******************* DATA SEND ****************
void sendData (){
checksum = ((compass)+(pressure)+(pitch));
// data to send
DOFDATA dd;
dd.compass = compass ; // 0 to 359 degrees, 0 to 168$
dd.pressure = pressure ; // to 1200 "H2O, 0 to 4B0$
dd.pitch = pitch ; // -20 to +20 Degrees, ffec to 14$
dd.checksum = checksum ;
int numBytes = portOne.write((byte*)&dd, sizeof(dd));
Serial.print ("Heading ") ;
Serial.println (compass) ;
Serial.print ("Pressure ") ;
Serial.println (pressure) ;
Serial.print ("Pitch ") ;
Serial.println (pitch) ;
Serial.print ("Checksum ") ;
Serial.println (checksum) ;
Serial.println (" ") ;
return ;
}
void upData () { // inc. fudged data values
compass=(compass+10) ; // inc. compass 0 to 359 degrees, 0 to 168$
if (compass>360) {
compass=0 ;
}
pressure = (pressure +10) ; // inc pressure // 0 to 1200 "H2O, 0 to 4B0$
if (pressure>1200) {
pressure = 0 ;
}
pitch++ ; // inc pitch // -20 to +20 Degrees, ffec to 14$
if (pitch>20) {
pitch= (-20) ;
}
return ;
}