trouble creating library

Hi everyone,

I am pretty new to programming and arduino. Though I’ve had some luck with creating a very basic library in arduino recently, I’m a bit stuck, currently.

I modified an example I found online to:

  1. Tell a servo motor to scan through 0-180 in 10 degree increments
  2. Have it read an averaged value from a sensor at each stop, and store it in an array .
  3. At the end of each cycle it gets the average value of the array of sensor values,
  4. If that average is greater than 200, it goes to the index of the maximum value of the array that assigns the servo location (so it points in the direction where it received the greatest input)
  5. As long as the input to the sensor stays above 200, the motor stays pointing in the same direction
  6. If the average is not greater than 200, it repeats the scanning cycle.

That code by itself seemed to be working. It makes use of the Servo library as well as the Array library.

When I tried to put it into a library, I don’t get any error messages, but it’s definitely not working as expected.

These are my current issues:

  1. Motor does not move at all (It does not seem to be recognizing the array values that I’m trying to write to the servo)
  2. Incoming sensor values appear to be meaningless
  3. Array average and maximum index value are consistently meaningless (index value also seems to always be 2)
  4. I just fried my motor. :frowning:

I read somewhere on the arduino website that there are some issues with incorporating libraries in libraries, and that you need to incorporate the libraries on the sketch level…so I wonder if I am doing something wrong here…or, I’m wondering if possibly I am declaring the array(s) incorrectly in either the header file or the source file?

Anyway, I’m in the process of trying to break it down to its smaller components to try to isolate the problem, but if anyone has any ideas where I’m going wrong, or for anything else I might try, I’d really appreciate your thoughts!

Thanks.

This is the code that is working, which I’m trying to incorporate into a library

//this example seems to be working fine
//motor scans for meaningful input, if there is meaningful input, motor looks in direction of greatest input and stays there as long
//as there is still input above given threshold

#include <Servo.h>
#include <Array.h>

Servo servo1;
int arrSize = 19;
int IR_Sensor = 2;    
int val = 0;      
int distance[18];
int servo_angle[]= {0,10,20,30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180};
int a;
int arrayAverage = 0;
Array<int> array = Array<int>(distance,arrSize);

void setup()
{
 Serial.begin(9600);           
 Serial.println("Servo test!");
 servo1.attach(10); //attaching servo to pin 10
}


void loop()
{
  val = analogRead(IR_Sensor);
  if(val < 200){
  
    for(int x = 0; x <= 18; x++) 
    {
      servo1.write(servo_angle[x]); //tell servo to go to the angle assigned to the index x of the array we are iterating through
      distance[x] = ReadSens_and_Condition(); //store serial values in distance array
      Serial.println(servo_angle[x]); //print the servo angle array
      Serial.println(distance[x]);  //print the distance array
      Serial.println();
      delay(500);    
    }  
    
    a=getIndexOfMaximumValue(distance, arrSize); //distance is the array, arrSize = 18, and is the size of the array
    Serial.print("Max index is------");
    Serial.print(a); 
    Serial.print("------");
    Serial.println(distance[a]); //prints the value at the index of the max value
    Serial.print("\tAverage value:");
    arrayAverage = array.getAverage();
    Serial.print(arrayAverage);
    Serial.println();
  }
 
        if(arrayAverage > 200){
                servo1.write(servo_angle[a]);
                 Serial.print("I'm going to the location of my greatest input value: ");  
                 Serial.print(servo_angle[a]);
                 Serial.print(" ");
                 Serial.print("degrees ");
                 Serial.println();
                 delay(10000);  
    }
       
        
}

//get avg sensor information to store in array
int ReadSens_and_Condition() 
{
  int i;
  int sval;
  for (i = 0; i < 5; i++) 
     {
       sval = sval + analogRead(IR_Sensor);   
     }

  sval = sval / 5;    
  return sval;
}

//get index of maximum value in distance array
int getIndexOfMaximumValue(int* array, int size)
{ 
 int maxIndex = 0; 
 int max = array[maxIndex]; 
   for (int i=1; i<size; i++)
   { 
       if (max<array[i])
       { 
           max = array[i];
           maxIndex = i; 
       }
   }
 return maxIndex; 
}

This is the header file code

/* library to make motor scan and look for and go to greatest source of meaningful input*/

#ifndef MotorScan_h
#define MotorScan_h

#include "WProgram.h"

//seems like you might not be able to inlcude libraries in library...instead might need to include them at the sketch-level
//testing it here anyway
//#include <Servo.h>
//#include <Array.h>

#include "Servo.h"
#include "Array.h"


class MotorScan
{
  public:
    MotorScan(int servoPin, int sensorPin); //declare sensorPin, add semicolon
     //not sure if this should be void...in original code it is declared as int
    int ReadSens_and_Condition();
    int getIndexOfMaximumValue(int* array, int size);
    void scanAndGetSerialValues();
  private:
    Servo _servo1; //not sure where to put this...should it even be in library?
    int _servoPin;
    int _sensorPin;
    int _sensorVal;
    int _arrSize; //eventually would be nice to make this public, so that you can choose how many values to take from sensor
    int _sensorDistance[]; //how to declare arrays in header file
    int _servoAngle[];
    int _a;
    int _arrayAverage;
//    Array<int> array;
};

#endif

this is the source file code for the library

/* library to make motor scan and look for and go to greatest source of meaningful input*/

#include "WProgram.h"
#include "motortest3.h"

//seems like you might not be able to include libraries in library...instead might need to include them at the sketch-level
//testing it here anyway
//#include <Servo.h>
//#include <Array.h>
//maybe like this:

#include "Servo.h"
#include "Array.h"

MotorScan::MotorScan(int servoPin, int sensorPin) //declare sensorPin
{
  _servoPin = servoPin;
  _sensorPin = sensorPin;
  _sensorVal = 0;
  _arrSize = 19;
  _sensorDistance[19];
  _a;
  int _servoAngle[19] = {0,10,20,30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180};
  _arrayAverage;
  _servo1.attach(_servoPin); //not sure where it actually makes sense to put this...or even if it should be in library?
  //where to include this? (tried below):
//Array<int> array = Array<int>(_sensorDistance, _arrSize);
}

void MotorScan::scanAndGetSerialValues()
{
  
  _sensorVal = analogRead(_sensorPin);
  if(_sensorVal < 200){
    for(int x = 0; x <= 17; x++) 
     {
      _servo1.write(_servoAngle[x]); //tell servo to go to the angle assigned to the index x of the array we are iterating through
      _sensorDistance[x] = ReadSens_and_Condition(); //store serial values in distance array
      Serial.println(_servoAngle[x]); //print the servo angle array
      Serial.println(_sensorDistance[x]);  //print the distance array
      Serial.println();
      delay(500);    
    }  
    Array<int> array = Array<int>(_sensorDistance, _arrSize);
    _a=getIndexOfMaximumValue(_sensorDistance, _arrSize); //distance is the array, arrSize = 18, and is the size of the array
    Serial.print("Max index is------");
    Serial.print(_a); 
    Serial.print("------");
    Serial.println(_sensorDistance[_a]); //prints the value at the index of the max value
    Serial.print("\tAverage value:");
    _arrayAverage = array.getAverage();
    Serial.print(_arrayAverage);
    Serial.println();
  }
  
  if(_arrayAverage > 200){
                _servo1.write(_servoAngle[_a]);
                 Serial.print("I'm going to the location of my greatest input value: ");  
                 Serial.print(_servoAngle[_a]);
                 Serial.print(" ");
                 Serial.print("degrees ");
                 Serial.println();
                 delay(10000);  
 }
       
        
}

int MotorScan:: ReadSens_and_Condition() 
{
  int i;
  int sval;
  for (i = 0; i < 5; i++) 
     {
       sval = sval + analogRead(_sensorPin);   
     }

  sval = sval / 5;    
  return sval;
}

int MotorScan:: getIndexOfMaximumValue(int* array, int size)
{ 
 int maxIndex = 0; 
 int max = array[maxIndex]; 
   for (int i=1; i<size; i++)
   { 
       if (max<array[i])
       { 
           max = array[i];
           maxIndex = i; 
       }
   }
 return maxIndex; 
}

this is where i try to use the library

//#include <TESTMotorScan.h>
#include <motortest3.h>


#include <Servo.h>
#include <Array.h>
MotorScan firstMotor(10, 2);

void setup()
{
   Serial.begin(9600);           
 Serial.println("Servo test!");
}

void loop()
{
  firstMotor.scanAndGetSerialValues();
}

MotorScan.h…
int _sensorDistance; //how to declare arrays in header file
int _servoAngle;

Nope. This is not how to declare an array. While, I believe the GCC compiler supports zero-sized arrays, I have no idea how to use them or what purpose they serve. Do you? Put a value inside the brackets.

MotorScan.cpp…
_sensorDistance[19]; // This does nothing. The compiler will remove it as dead code.

// This is a local array. When the contructor returns, it is gone from existance…
int _servoAngle[19] = {0,10,20,30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180};

_arrayAverage; // Dead code

// This is local to scanAndGetSerialValues. When the method returns, the array is destroyed.
Array array = Array(_sensorDistance, _arrSize);

Thanks! I think I have it working now...will know for sure once I get my new motor. Wish I posted before I fried it.