Go Down

Topic: Accelerometer ADXL335 and GPS EM411 in Arduino UNO (Read 682 times) previous topic - next topic

folkes

Halo friend.
I need your help.

I have arduino UNO, Accelerometer adxl335 and GPS EM411
I want to build a third communication device with the following scenario:

When the Arduino is turned on, the GPS reading. if the GPS status = valid, then the GPS will send GPRMC position to the computer. Once the position is received, then sends accelerometer vibration conditions x, y, z.

I've tried from a few examples of the forum but when executed, the status of the GPS readings are still messed up.

I beg for help. thank you

This is my code:

//////////////////////////////////////////////////////////////////
//Sensor Acceleroemter ADXL335
//////////////////////////////////////////////////////////////////

#include <SoftwareSerial.h>
#include <nmea.h>
SoftwareSerial GPS = SoftwareSerial(11,12);


//read pin analog accelerometer
const int xPin = A0;
const int yPin = A1;
const int zPin = A2;

//read parameter gps
byte SWval;
char dataformat [7] = "$GPRMC";
char messageline[200] = "";
int i = 0;
byte rx = 11;
byte tx = 12;
#define bit9600Delay 84
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94

//The minimum and maximum values
//take when the accelerometer standing still
int minVal = 226;
int maxVal = 286;

//hold the caculated values
double x;
double y;
double z;

void setup(){
  Serial.begin(9600);
  //print posisi GPS
  char2string();
  //for (int i=0;i<201;i++){       // Initialize a buffer for received data
  //   messageline=' ';
  //}
   Serial.print(messageline);
   Serial.println("");
  delay(200);
}

void loop(){
  //read the analog values from the accelerometer
  int xRead = analogRead(xPin);
  int yRead = analogRead(yPin);
  int zRead = analogRead(zPin);

  //convert read values to degrees -90 to 90 - Needed for atan2
  int xAng = map(xRead, minVal, maxVal, -90, 90);
  int yAng = map(yRead, minVal, maxVal, -90, 90);
  int zAng = map(zRead, minVal, maxVal, -90, 90);

  //Caculate 360deg values like so: atan2(-yAng, -zAng)
  //atan2 outputs the value of -? to ? (radians)
  //We are then converting the radians to degrees
  x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
  y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
 
  //Output the caculations
  Serial.print("x: ");
  Serial.print(x);
  Serial.print(" | y: ");
  Serial.print(y);
  Serial.print(" | z: ");
  Serial.println(z);
// if(x=
  delay(100);//just here to slow down the serial output - Easier to read

//Led konfirm
  int ledPinX = 7; //whatever PWM digital pin you want
  int ledPinY = 8; //whatever PWM digital pin you want
  int ledPinZ = 9; //whatever PWM digital pin you want
 
  pinMode(ledPinX, OUTPUT);
  pinMode(ledPinY, OUTPUT);
  pinMode(ledPinZ, OUTPUT);
 
  int LEDx = map(x, 0, 180, 0, 1023);
  int LEDy = map(y, 0, 180, 0, 1023);
  int LEDz = map(z, 0, 180, 0, 1023);
 
  analogWrite(ledPinX, LEDx);
  analogWrite(ledPinY, LEDy);
  analogWrite(ledPinZ, LEDz);
 
}

//fungsi SWread
int SWread(){
byte val = 0;
while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit4800Delay);
  for (int offset = 0; offset < 8; offset++) {
    delayMicroseconds(bit4800Delay);
    val |= digitalRead(rx) << offset;
  }
  delayMicroseconds(bit4800Delay);
  return val;
  }
}

//fungsi char2string
void char2string(){
  i=0;
  messageline[0] = SWread();
  while (messageline != 13) //carriage return or max size
    {
      i++;
      messageline = SWread();
      }
   Serial.write(SWread());
}


and result is :

I$=²44Ð,¹æ:3?æÆ99q93Í3y6?35q6:Å006¹3?1ŹÆ.ÅqÅ03qÆÇ89yÖ9ÅN,æÙæ6ÍqÆÆ80&ÙÆA

x: 16.65 | y: 19.32 | z: 40.46
x: 16.81 | y: 18.97 | z: 41.31
x: 17.42 | y: 17.93 | z: 44.12
x: 18.10 | y: 16.09 | z: 48.58
x: 19.84 | y: 27.27 | z: 34.99
x: 18.25 | y: 34.66 | z: 25.50
x: 20.56 | y: 33.94 | z: 29.12
x: 20.25 | y: 31.45 | z: 31.10
x: 22.62 | y: 39.09 | z: 27.15
x: 21.80 | y: 38.66 | z: 26.57
x: 17.90 | y: 27.98 | z: 31.29
x: 18.28 | y: 20.15 | z: 41.99
x: 19.98 | y: 38.50 | z: 24.57
x: 16.59 | y: 35.48 | z: 22.68
x: 14.97 | y: 31.13 | z: 23.88
x: 13.15 | y: 25.92 | z: 25.68
x: 11.62 | y: 31.67 | z: 18.43
x: 9.46 | y: 31.04 | z: 15.48
x: 12.37 | y: 32.28 | z: 19.15
x: 7.93 | y: 34.85 | z: 11.31
x: 3.53 | y: 39.17 | z: 4.33
x: 6.12 | y: 37.30 | z: 8.00
x: 358.21 | y: 22.11 | z: 355.60
x: 352.07 | y: 30.75 | z: 346.83
x: 1.40 | y: 30.34 | z: 2.39
x: 358.78 | y: 33.ÿ

PaulS

You create an instance of the SoftwareSerial class, and then bit-bang reading serial data. Why? Why not let SoftwareSerial do the reading of the pins you told it it could use exclusively?

If you are not going to let SoftwareSerial own those pins, or do anything, why create the instance?

Go Up