Wangmaster:
A little confused, you can create an object outside setup() or main() like you would Servo myServo, can’t you? So if that is dynamic, shouldn’t it throw an error on compile time as well?
“Servo myServo;” instantiates an instance of the Servo class. That is, the compiler makes sure memory is set aside for the class as well as creating all the necessary class pointers. It is very similar to “int x”. Nothing dynamic happens when you call “int x” and neither does “Servo myServo”.
The problem arises when you construct and initialize the instantiated class. For instance, calling “.begin()”. Although you can (and should) instantiate a class outside of main(), you can’t construct a class outside of main().
Wangmaster:
If I call my object within setup(), wouldn’t I be unable to access it in main() (out of scope)? Or did you mean something else?
You’ve already instantiated the class globally, so any changes made to the class in setup() persist through to loop(). Here’s a simple example:
int x;
void setup()
{
Serial.begin(9600);
x = 5;
}
void loop()
{
Serial.println(x);
delay(100);
}
Even though the value of x is only changed in setup(), x keeps it’s update through to loop().
Wangmaster:
When I said I can’t share I meant that my library is so barebones at the moment I don’t think it would add much to the discussion, but sure, I’ve attached it as is for reference. I’m open to suggestions on how to improve.
I apologize if I sounded mean - didn’t intend to.
Here’s the header:
#ifndef SERVOCONTROL_H_INCLUDED
#define SERVOCONTROL_H_INCLUDED
#define SERVO_UPPER_MICRO 1710
#define SERVO_LOWER_MICRO 610
#define SERVO_TRAVEL_MILLIS 800
#define CHAMBER_COUNT 10
#include <Arduino.h>
#include <Servo.h>
class servoControl{
public:
Servo myServo;
servoControl(int pin) //Constructor, set up positional values for writeMicrosecond()
{
_pinNo = pin;
int increment = (SERVO_UPPER_MICRO - SERVO_LOWER_MICRO) / (CHAMBER_COUNT - 1);
for (int i = 0; i < 10; i++)
{
_posPresetData[i] = SERVO_LOWER_MICRO + (increment * i);
}
}
void initServo(int pin) //Initialize Servo: Place in setup()
{
myServo.attach(pin);
}
void testServo() //Test that servo is operational
{
myServo.writeMicroseconds(SERVO_LOWER_MICRO);
delay(2000);
myServo.writeMicroseconds(SERVO_UPPER_MICRO);
delay(2000);
}
void goToPos(int chamber) //Servo go to specific chamber
{
myServo.writeMicroseconds(_posPresetData[chamber-1]);
if (_currentPos < chamber)
{
delay(SERVO_TRAVEL_MILLIS*(chamber - _currentPos));
}
if (_currentPos >= chamber)
{
delay(SERVO_TRAVEL_MILLIS*(_currentPos - chamber));
}
_currentPos = chamber;
}
void resetServo() //Servo reverts to Chamber 1
{
myServo.writeMicroseconds(SERVO_LOWER_MICRO);
_currentPos = 1;
delay(SERVO_TRAVEL_MILLIS * (CHAMBER_COUNT -1));
}
int getCurrentPos() //Getter for current position
{
return _currentPos;
}
private:
int _pinNo;
int _posPresetData[10];
int _currentPos = 0;
};
#endif // SERVOCONTROL_H_INCLUDED
Suggestions:
1.) Host and version control your library using GitHub. GitHub allows others to easily view, download, submit bug tickets, etc. It also allows you to remotely backup your code while automatically version-controlling it.
2.) You should also be using a .cpp file in your library. The .h is for function and class declarations and the .cpp is for function and class definitions. Take a look at this simple Arduino library.
3.) Instead of using “#ifndef SERVOCONTROL_H_INCLUDED”, you can do a “#pragma once” (I’m guilty of this myself)
4.) Shy away from macros as much as possible (#define). Instead, use the const qualifier.
5.) If the body of an if statement, for loop, while loop, etc is only one line, exclude the curly braces - makes things more readable
Lastly, doing some googling on how C++ classes work might clear some things up.