SWDesk/Firmware
[Arduino] 클래스(Class) 활용 예제
bizmaker
2025. 3. 16. 05:30
아두이노에서 클래스 생성자 활용하는 예제
struct 구조와 유사하지만, 초기화 방법 및 함수 활용 측면에서 많은 장점이 있음.
XIAO를 대상으로 테스트함.
[테스트 코드]
class cValueInfo{
public:
unsigned long mSecond_ThisOperation = 0; // [msec]
float VoltageValue = 0.;
float CurrentValue = 0.;
// 기본 생성자
cValueInfo() {
mSecond_ThisOperation = 0; // [msec]
VoltageValue = 0.0;
CurrentValue = -0.0001;
}
cValueInfo(float voltage, float current, unsigned long mSecond) {
this->mSecond_ThisOperation = mSecond; // [msec]
this->VoltageValue = voltage;
this->CurrentValue = current;
}
};
cValueInfo value1(0.1, 0.11, 125);
cValueInfo values[3];
cValueInfo value11, value12, value13;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
values[1] = value1;
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(values[1].VoltageValue);
delay(1000);
}
[테스트 결과]
[Seeedstudio XIAO SAMD21 프로그래밍 화면]
https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json
반응형