Error While writing Library for Gy521



  • I'm trying to make an Arduino library for GY521. Created .cpp and .h files and also completed the code. But I'm getting an error while calling library function via main code.

    look my code below or via project repo

    Accelometer:10:7: error: request for member 'getAcData' in 'goo', which is of non-class type 'Gy521()'
       goo.getAcData();
           ^~~~~~~~~
    Accelometer:11:7: error: request for member 'getGyData' in 'goo', which is of non-class type 'Gy521()'
       goo.getGyData();
    

    Accelometer.ino (main code):

    //#include <Wire.h>
    #include "Gy521.h"
    
    Gy521 goo();
    
    void setup(){
      Serial.begin(9600);
    }
    void loop(){
      goo.getAcData();
      goo.getGyData();
    }
    

    Gy521.cpp

    #include "Gy521.h"
    #include "Arduino.h"
    #include <Wire.h>
    
    Gy521::Gy521()
    {
      Wire.begin();
      Wire.beginTransmission(_MPU);
      Wire.write(0x6B); 
      Wire.write(0);    
      Wire.endTransmission(true);
    }
    
    void Gy521::getAcData(){
      Wire.beginTransmission(_MPU);
      Wire.write(0x3B);  
      Wire.endTransmission(false);
      Wire.requestFrom(_MPU,6,true);  
      AcX=Wire.read()<<8|Wire.read();    
      AcY=Wire.read()<<8|Wire.read();  
      AcZ=Wire.read()<<8|Wire.read();   
     
      Serial.print("Accelerometer: ");
      Serial.print("X = "); Serial.print(AcX);
      Serial.print(" | Y = "); Serial.print(AcY);
      Serial.print(" | Z = "); Serial.println(AcZ);  
    }
    void Gy521::getGyData(){
      Wire.beginTransmission(_MPU);
      Wire.write(0x43);  
      Wire.endTransmission(false);
      Wire.requestFrom(_MPU,6,true);  
       
      GyX=Wire.read()<<8|Wire.read();  
      GyY=Wire.read()<<8|Wire.read();  
      GyZ=Wire.read()<<8|Wire.read();
      
      Serial.print("Gyroscope: ");
      Serial.print("X = "); Serial.print(GyX);
      Serial.print(" | Y = "); Serial.print(GyY);
      Serial.print(" | Z = "); Serial.println(GyZ);
      Serial.println(" ");
      delay(100);
    }
    
    

    Gy521.h

    #ifndef Gy521_h
    #define Gy521_h
    
    #include "Arduino.h"
    class Gy521
    {
      public:
        Gy521();
        void getAcData();
        void getGyData();
        int16_t GyX,GyY,GyZ;
        int16_t AcX,AcY,AcZ;
            
      private:
        const int _MPU = 0x68;
    };
    
    
    
    #endif
    

    :- This is my first try to make an Arduino Library 🙂



  • When you want to create an object of a class and it doesn't take in any parameters, then

    Gy521 goo;  //object declaration
    

    is enough. Then invoke the initialization function,

    goo.begin();  //initialization
    

    For that, you need to create an initialization function. You can copy the statements in your constructor there.



  • @vishnumaiea Thank you. I made the changes, it worked well.
    Next, I want to return 3 variables in getAcData() function. Is there any way to do this ? Can i access AcX,AcY,AcZ globally via main code.



  • @rafitc99 The member variables of a class can not be accessed without an accompanying object; that's the concept of a class. If you want to return three values, simply update the member variables AcX, Ac and AcZ, and access them as,

    Serial.println(goo.AcX);
    Serial.println(goo.AcY);
    Serial.println(goo.AcZ);
    


  • @vishnumaiea

    Serial.println(goo.AcX);
    Serial.println(goo.AcY);
    Serial.println(goo.AcZ);
    

    while using above code, I'm getting '0'. not updating,



  • @rafitc99 is it fixed ?,u found a solution?



  • @saheen_palayi Nop, while calling goo.AcX getting zero as output. not getting updated value.



  • @rafitc99 I dont think u can access those like that did you tried those lines after

     goo.getAcData();
     goo.getGyData();
    


  • @saheen_palayi yeah! that was the problem. Now it's worked. Thank you @saheen_palayi and @vishnumaiea



  • @rafitc99 What was the solution?


Log in to reply
 

Recent Posts

  • @Suhailjr But I think since it's not a plug and plug play system and it will laying on the PCB for the long term, it might be fine. for me, it's similar to the Intel Edison and the PICO-IMX7 System-on-Module

    dade502a-71f6-4da0-bd7d-0e9a0b602cb0-image.png

    a4b8898f-90d4-4a34-ba5d-81ab76fda904-image.png

    read more
  • @salmanfaris at last they update their design. T
    What you think on the updated connector. I felt like its more fragile and high chance for corottion issue due to humidity for long term use.

    read more
  • Built on the same 64-bit quad-core BCM2711 application processor as Raspberry Pi 4, Compute Module 4 delivers a step-change in performance over its predecessors: faster CPU cores, better multimedia, more interfacing capabilities, and, for the first time, a choice of RAM densities and a wireless connectivity option.

    3094f292-3301-4364-a444-d828f6e77ff8-image.png

    You can find detailed specs here,

    1.5GHz quad-core 64-bit ARM Cortex-A72 CPU VideoCore VI graphics, supporting OpenGL ES 3.x 4Kp60 hardware decode of H.265 (HEVC) video 1080p60 hardware decode, and 1080p30 hardware encode of H.264 (AVC) video Dual HDMI interfaces, at resolutions up to 4K Single-lane PCI Express 2.0 interface Dual MIPI DSI display, and dual MIPI CSI-2 camera interfaces 1GB, 2GB, 4GB or 8GB LPDDR4-3200 SDRAM Optional 8GB, 16GB or 32GB eMMC Flash storage Optional 2.4GHz and 5GHz IEEE 802.11b/g/n/ac wireless LAN and Bluetooth 5.0 Gigabit Ethernet PHY with IEEE 1588 support 28 GPIO pins, with up to 6 × UART, 6 × I2C and 5 × SPI

    31354fad-6332-40e8-b324-53e0aa5dbace-image.png

    Compute Module 4 IO Board
    89a3b68b-bb74-4002-9c2b-d32c2e1450b3-image.png

    The IO board provides:

    Two full-size HDMI ports Gigabit Ethernet jack Two USB 2.0 ports MicroSD card socket (only for use with Lite, no-eMMC Compute Module 4 variants) PCI Express Gen 2 x1 socket HAT footprint with 40-pin GPIO connector and PoE header 12V input via barrel jack (supports up to 26V if PCIe unused) Camera and display FPC connectors Real-time clock with battery backup

    More details:
    https://www.raspberrypi.org/products/compute-module-4/?variant=raspberry-pi-cm4001000, https://www.raspberrypi.org/blog/raspberry-pi-compute-module-4/

    read more
  • 28e1870e-33fe-4791-bc74-05c9d58098da-image.png

    Seeed is currently counting down for the 10 year anniversary of the debut of Grove 🎉. In the past 10 years, we have seen many wonderful projects created by Grove from our user and community. We want to invite you to join us to celebrate the 10 year anniversary of Grove 🤩.

    In the Grove 10 Years Celebration, we’d like to collect the stories of “Grove with You”, which we want to invite our users to share their stories they have with Grove products.

    => What project you’ve made out of Grove?
    => What is your favourite Grove?
    => which one was the first Grove you’ve purchased?

    Seeed encourages you to post and share photos, videos, stories of Grove in the Seeeed Forum, or any other platforms below that you’d like to use.

    The following ways to find us, and tag #grove10 to share the project with us!

    Twitter (@seeedstudio) Instagram (@seeedstudio) or simply email your project to grove10@seeed.cc

    Feel free to be creative in documenting your projects–we will be selecting top 10 most “Grovy” winning entries from the submissions, and the winners will receive a $100 Grove Surprise Pack 😋 as a special gift.

    Looking forward to seeing your projects! 🥳

    read more
  • @kowshik1729 I think that might not get since the platform is not opensource! but they provided everything you need to develop a system with that.

    read more