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

  • What are objects?

    Jeff Goodell: Would you explain, in simple terms, exactly what object-oriented software is?

    Steve Jobs: Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

    Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

    You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.

    read more
  • This webinar is focussing on learning Git VCS, a distributed version control system for tracking changes during practical software development.

    We will be exploring the usage of various git commands, plugins/extensions and branching-models based on real-world contexts.

    The session is targetting absolute beginners with minimum or no experience with git, But a mindset to optimize and improve work efficiency and productivity by learning and exploring new tool-chains and strategies is a must!

    Join MakerChat.

    Maker: Muhammad Swalah
    Place: Zoom
    Date: 16th Aug 2020
    Time: 8:00 PM

    Registration: bit.ly/makerchat8

    Join MakerGram: bit.ly/join-mg

    photo_2020-08-06_16-46-33.jpg

    read more
  • 3D PRINTING@300x.png

    read more
  • MakerGram is Live Now!

    MakerChat 0x07: How to Learn Machine Learning (for Humans) by @GKS

    Watch LIVE @ https://www.facebook.com/makergram/videos/736833737153967/

    #MakerGramLearnings #MGLive #AI #ArtificialIntelligence #MachineLearning #Humans #MachineIntellegence #MakerChat

    read more
  • @anuradhasingh426 Please check your mailbox.

    read more