When people refer to "creating an instance of (something)", what do they mean ? What is an "instance" ?
Thanks
an 'instance' is the creation of an object so it is usable by a sketch.
an example using the servo library:
#include <Servo.h>
Servo myservo;
void setup(){
myservo.attach(9); // use the instance to do something with the servo
}
Servo is an object defined in servo.h.
the expression
Servo myservo;
creates an instance of the Servo object, the instance is called myservo. In some cases, for example the hardware serial object, the instance (i.e. Serial) is created at startup for the user.
The term is used a lot with object oriented programming. Some libraries describe an object (and a set of operations on that object). In order to do anything with such a library, you have to actually create a copy of the object that the library describes - this is called instantiation. The object created is an instance.
-j
In order to do anything with such a library, you have to actually create a copy of the object that the library describes
Just to be clear, someone does but not necessarily you the arduino user. Many of the Arduino supplied objects are created for you in the core startup code.
So, one could create an instance using a debounce function, and then apply it to multiple swithes, or a "check switch" for a similar result ?
EX:
void loop()
{
readbuttons;
}
void readbuttons()// Read seats button
{
seatval = digitalRead(seats);
delay(10)// read if both chairs are occupied
homeval = digitalRead(home);
if (seatval == LOW)
{
digitalWrite(seatmotor, HIGH); // begin sending chairs to home location
}
if (homeval == HIGH)
{
digitalWrite(seats, LOW); //when chairs spin to "home" location stop.
Serial.println("you are home");
}
}
In this case, is the "readbuttons" function a instance ? I am trying to do a function that will read the state of multiple buttons...instead of writing code for each button (debounce, read, and digitalwrite for 5 different inputs).
Thnas for the quick responces.
C
readbuttons is a function and there is one and only one instance of this. To create multiple instances you need to create a class. You can read about how do do this in the 'creating a library' tutorial' http://arduino.cc/en/Hacking/LibraryTutorial
But you can make your readbutton function handle different inputs by passing the information using arguments. In the posted form, your function has no arguments, but if you changed it to something like this you should be able to do what you want.
void readbuttons(int inputPin, int outputPin )// Read buttons
{
// inputPin is the button to read
// outputPin is the pin that will be written when the button is pressed
// your code here?.
}
An "instance" is usually a copy of a data structure (an "object", in object-oriented programming languages, but "instance" can be used in ordinary languages as well.) The objects generally share "methods" (functions) with the other objects of the same class.
So in the button debounce example, you might have an "instance" of a button that describes which pin it's connected to and... All the instances of a button could share the same debounce method. A more complex device like a serial port is probably a better example, since each instance of a serial port probably contains data like the speed, an input buffer, an output buffer, counts of characters in the buffers, and so on.