Arduino learning articles custom library

foreword

In use Arduino When mega2560 is used as a control board, if there are many modules that need to be controlled, it will be very bloated to write all the code in one document, which is not conducive to subsequent code maintenance and further development, so write corresponding classes according to different modules. Libraries are very important.

write header file
The class library consists of header files and .cpp files, and can be written in C++ or C language. What I need to do here is a related module to control the optical switch, so I first create a header file OptcialSwitch.h in Notepad, which will put some variables and functions used in the class library. For example, as shown in the code below, the "Arduino.h" header file needs to be added first, then I customize some PIN pins that connect the Arduino mega2560 to the optical switch module, and finally the variable names and function names used in the class library. Note: If the variables of the class library generally cannot be used by the main program, if this is required, the variable name should be declared as a global variable with extern
 

#ifndef
#include "Arduino.h"
#include "../command.h"

/******************** Pin Number *****************/
#define ChannelBit0 23   //34  //2
#define ChannelBit1 22   //36 //3
#define ChannelBit2 25   //38  //4 
#define ChannelBit3 24   //40  //5
#define ChannelBit4 27   //42 //6
#define ResetBit 28   //52  //11
#define ReadyBit 26  //48 //7
#define ErrorBit 29   //50 //8

//variable name used
extern int Channel;

//function name used
bool OpticalSwitch_Setup(void);
void OpticalSwitchChannel(int num);

.CPP file
The next step is to create an OpticalSwitch.cpp file. This file contains the specific optical switch operation, which is relatively simple. Just assign values ​​to each PIN pin in turn. First, we need to include the "Arduino.h" header file and the "OpticalSwitch.h" header file we just created. Next, we need to write two required functions, one for initialization and one for assignment. Of course, this is not the place where the program is compiled and run, so there is no need for void setup() and void loop() in the Arduino IDE
 

#include "Arduino.h"
#include "OpticalSwitch.h"
int Channel;

bool OpticalSwitch_Setup(void)
{
    //Set each PIN pin to output input mode
    pinMode(ChannelBit0, OUTPUT);
    pinMode(ChannelBit1, OUTPUT);
    pinMode(ChannelBit2, OUTPUT);
    pinMode(ChannelBit3, OUTPUT);
    pinMode(ChannelBit4, OUTPUT);
    pinMode(ResetBit, OUTPUT);
    pinMode(ReadyBit, INPUT);
    pinMode(ErrorBit, INPUT);
    delay(50);
}

void OpticalSwitchChannel(int num)
{
    //Assign values ​​to each PIN according to the binary number of the channel number
    num -= 1;
    digitalWrite(ResetBit, HIGH);
    delay(50);
    digitalWrite(ChannelBit4, (num&0x1f)>>4);
    digitalWrite(ChannelBit3, (num&0xf)>>3);
    digitalWrite(ChannelBit2, (num&0x7)>>2);
    digitalWrite(ChannelBit1, (num&0x3)>>1);
    digitalWrite(ChannelBit0, num&0x1);
    delay(300);
    if((digitalRead(ReadyBit)==LOW) && (digitalRead(ErrorBit)==LOW))
	{
		Channel = num+1;
		Serial.println(Channel);
    }
}

keyword highlighting
After writing the above two files, we can also write a keywords.txt file that allows Arduino IDE to recognize and highlight keywords. Write the following code in this document. Under Constants, you can put some variable names that need to be highlighted. Note that between OptcialSwitch_Setup and KEYWORD2 you need to use the "TAB" key instead of the space bar.
 

###########################
# Methods and Functions	KEYWORD2)
###########################
OpticalSwitch_Setup	KEYWORD2
OpticalSwitchChannel	KEYWORD2
###########################
#Constants	(LITERAL1)
###########################

2. Use a class library
Then it's time to use the class library we just wrote. First, put the.H header file,.Cpp file and keywords. Txt file we just wrote into the same folder. The name of the folder is the name of the class library you want to declare. For example, I named it OpticalSwitch. Then put this folder on your computer's C drive - > Documents - > libraries, as shown below. Such a complete class library is built.

Finally we build a test program in Arduino to use this class library. Our first name is OpticalSwitchTest.ino files, and then select toolbar - > Project - > Load Library to see When it comes to the class library we made, you can choose to load it in the code This class library is referenced at the top, of course, you can also manually enter the reference in the bottom contribution Library

#include <OpticalSwitch.h>

void setup() 
{
    Serial.begin(9600);
    delay(100);
    OpticalSwitch_Setup();
    delay(500);
    OpticalSwitchChannel(3);
}

void loop() 
{
}

You can directly call these two functions in the test program. If the module is set successfully, you can see the last printed "3" in the serial monitor.


 

 

 

Posted by micheal_newby on Sun, 25 Sep 2022 01:49:50 +0930