I am using an Arduino to send signals to an old Microcomputer Trainer as a way to replace keying in programs via the 20 keys on the Trainer. An example of a function in my Arduino program for a code/command sent to the trainer from the Arduino is like this:
void cy() {
digitalWrite(ARDUINO_PIN_4, HIGH);
delay(interval); // Delay half-a-second
digitalWrite(ARDUINO_PIN_4, LOW); // Set the I/O low
delay(interval);
}
I also use the Sparkfun SX1509 IO expander in order to get 20 pins with my Uno. An example of a function using the SX1509 pins is like this:
void ka() {
io.digitalWrite(SX1509_PIN_15, HIGH);
delay(interval); // Delay half-a-second
io.digitalWrite(SX1509_PIN_15, LOW); // Set the I/O low
delay(interval);
}
I suppose I could include the 354 lines like the ones above in every program, but it seems like a header and library that has these functions would be better. However, the problem is I am a total beginner with creating them from scratch.
class Morse
{
public:
Morse(int pin);
void dot();
void dash();
private:
int _pin;
};
But I am not clear on:
the "int pin". Do I need this if every function has it's own defined pin like "ARDUINO_PIN_4"
If I can use something like "ARDUINO_PIN_4" , where do I define it in the header?
How about the "io.digitalWrite(SX1509_PIN_15, HIGH);"? Do I just do it the same way? Do I have to include the lines from my program that are requried for the SX1509 in the header file as well?
#include <Wire.h> // Include the I2C library (required)
#include <SparkFunSX1509.h> //Click here for the library: http://librarymanager/All#SparkFun_SX1509
// SX1509 I2C address (set by ADDR1 and ADDR0 (00 by default):
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address
SX1509 io;
#include "Simple_Class.h"
Simple_Class MyClass(10);
void setup() {
Serial.begin(115200); //115200
Serial.println(" Simple_Class Testing");
Serial.print("Default Value: ");
Serial.println(MyClass.CheckValue());
MyClass.SetValue(100);
Serial.println(MyClass.CheckValue());
Serial.println(MyClass.SetValue(200).CheckValue()); // using Simple_Class & pointer and return *this in a function of a class allows you to do stuff like this.
}
void loop() {
// put your main code here, to run repeatedly:
}
You can hard code the pin numbers in your .cpp file.
The idea of making classes is that you can reuse them in new situations (with different pins). For you that is not really important now.
Why do you make a class anyway?
You can also put a function in a .cpp file..
That might be easier for you.
Ah see I didn't realize that; I was going by the example. That makes more sense now - thanks.
Will I have to include the "wire.h" for the SX1509 so that the .cpp knows what the io.digitalwrite statement is referring to?
I would start my moving the functions that you want to be in your library into a separate .cpp file in your sketch folder. This will also require a .h file, containing the declaration of those functions. Gradually move "stuff" from the sketch to the .cpp/.h file until you are happy with the degree to which functionality is "separated."
THEN you can worry about making the file/directory structure compatible with being stored as an Arduino library, rather than .cpp/.h in each sketch file that uses it. (although that latter scheme IS a possibility, as well.)
Thanks - I have sort of done that. The situation now is that I don't get any compilation errors but with the functions and definitions broken out into the .cpp/.h files nothing happens at all, as opposed to when everything is in a single .ino and the program correctly sends signal to the Trainer.. So I don't have something correct just yet.
Thanks: I copied and pasted those functions in an abridged format for here in a hurry and left those off. They do indeed have the closing braces in the full version.
Ughh...finally found it by printing out hard-copies of my header files and the original all in one version to compare. I could not see my mistake on the screen! I did not include the lines to set the pins to output mode in the .cpp file:
Thanks - as it turns out I did get it all working with 3 files but I will include them here anyway just for any other suggestions anyone might have to improve it. Otherwise, thanks again!