Skip to content

Commit 1b560d1

Browse files
add comment and fix bug
1 parent 4cd7461 commit 1b560d1

File tree

3 files changed

+34
-106
lines changed

3 files changed

+34
-106
lines changed

examples/Ultrasonic/Ultrasonic.ino

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Ultrasonic
3+
4+
Raging ultrasonic sensor distance measurement example for MakerBoard.
5+
Please connect an ultrasonic sensor to the ultrasonic sensor pins.
6+
7+
This example code is in the public domain.
8+
9+
https://github.com/YUKAI/MakerBoard/tree/develop/examples/Ultrasonic
10+
*/
11+
12+
#include <MakerBoard.h>
13+
MakerBoard board;
14+
MakerBoard::Ultrasonic ultrasonic(board.GROVE_CON1_SIG, board.GROVE_CON1_OPT);
15+
16+
void setup() {
17+
Serial.begin(9600);
18+
}
19+
20+
void loop() {
21+
long distance = ultrasonic.Ranging();
22+
Serial.print("Distance: ");
23+
Serial.print(distance);
24+
Serial.println(" cm");
25+
delay(1000);
26+
}

src/MakerBoard.cpp

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,12 @@
22
* @file MakerBoard.h
33
* @author Yoshinobu Obata
44
* @brief Standard library for メイカーボード
5-
* @version 0.1
6-
* @date 2024-09-10
5+
* @version 1.0.3
6+
* @date 2026-01-20
77
* Change log:
8+
* 2026-01-20: Added Ultrasonic class for ultrasonic sensor support.
89
*
9-
* @copyright Copyright (c) 2024 Yukai Engineering Inc.
10-
*
11-
*/
12-
#include <Arduino.h>
13-
#include "MakerBoard.h"
14-
#include <MsTimer2.h>
15-
16-
MakerBoardMotor Motor1(MakerBoard::DCM1_A, MakerBoard::DCM1_B);
17-
MakerBoardMotor Motor2(MakerBoard::DCM2_A, MakerBoard::DCM2_B);
18-
MakerBoardMotor Motor3(MakerBoard::DCM3_A, MakerBoard::DCM3_B);
19-
MakerBoardMotor Motor4(MakerBoard::DCM4_A, MakerBoard::DCM4_B);
20-
21-
/**
22-
* @brief DCモーターのピン設定.GPIO2〜GPIO9ピンが使用される.
23-
* (Configure the DC motor driver pins from GPIO2 to GPIO9.)
24-
*/
25-
void MakerBoard::motorPinSetup(){
26-
pinMode(this->DCM1_A, OUTPUT);
27-
pinMode(this->DCM1_B, OUTPUT);
28-
pinMode(this->DCM2_A, OUTPUT);
29-
pinMode(this->DCM2_B, OUTPUT);
30-
pinMode(this->DCM3_A, OUTPUT);
31-
pinMode(this->DCM3_B, OUTPUT);
32-
pinMode(this->DCM4_A, OUTPUT);
33-
pinMode(this->DCM4_B, OUTPUT);
34-
}
35-
36-
/**
37-
* @brief モーターを回転させ,パルスのトップをカウントする.このメソッドはTIMER_FREQUENCYでタイマー実行されることを想定している.
38-
* (Rotate the motor and count the top of the pulses. This method is assumed to be executed by a timer with TIMER_FREQUENCY.)
39-
*
40-
*/
41-
static void MakerBoard::motorPulseCounter()
42-
{
43-
Motor1.run();
44-
Motor2.run();
45-
Motor3.run();
46-
Motor4.run();
47-
Motor1.top_cnt++;
48-
Motor2.top_cnt++;
49-
Motor3.top_cnt++;
50-
Motor4.top_cnt++;
51-
if (Motor1.top_cnt > Motor1.PERIODIC_TIME){
52-
Motor1.top_cnt = 0;
53-
}
54-
if (Motor2.top_cnt > Motor2.PERIODIC_TIME){
55-
Motor2.top_cnt = 0;
56-
}
57-
if (Motor3.top_cnt > Motor3.PERIODIC_TIME){
58-
Motor3.top_cnt = 0;
59-
}
60-
if (Motor4.top_cnt > Motor4.PERIODIC_TIME){
61-
Motor4.top_cnt = 0;
62-
}
63-
}
64-
65-
/**
66-
* @brief DCモーターのPWMを生成する.(Set time for DC motor PWM control.)
67-
*
68-
*/
69-
void MakerBoard::motorPWMBegin(){
70-
// Set DC motor control PWM generation with timer
71-
this->motorPinSetup();
72-
MsTimer2::set(this->TIMER_FREQUENCY, this->motorPulseCounter);
73-
MsTimer2::start();
74-
}
75-
76-
/**
77-
* @brief 超音波センサのコンストラクタ.(Constructor of Ultrasonic sensor.)
78-
*/
79-
MakerBoard::Ultrasonic::Ultrasonic(int TP, int EP)
80-
{
81-
TP = TP;
82-
EP = EP;
83-
}
84-
85-
/**
86-
* @brief 超音波センサ(HC-SR04)で距離を測定する.(Measure distance with Ultrasonic sensor.)
87-
*/
88-
long MakerBoard::Ultrasonic::Ranging(){
89-
pinMode(TP,OUTPUT);
90-
pinMode(EP,INPUT);
91-
92-
digitalWrite(TP, LOW);
93-
delayMicroseconds(2);/**
94-
* @file MakerBoard.h
95-
* @author Yoshinobu Obata
96-
* @brief Standard library for メイカーボード
97-
* @version 0.1
98-
* @date 2024-09-10
99-
* Change log:
100-
*
101-
* @copyright Copyright (c) 2024 Yukai Engineering Inc.
10+
* @copyright Copyright (c) 2026 Yukai Engineering Inc.
10211
*
10312
*/
10413
#include <Arduino.h>
@@ -180,14 +89,6 @@ long MakerBoard::Ultrasonic::Ranging(){
18089
delayMicroseconds(10);
18190
digitalWrite(TP, LOW);
18291

183-
long duration = pulseIn(EP,HIGH);
184-
long distance_cm = duration /29 / 2 ;
185-
return distance_cm;
186-
}
187-
digitalWrite(TP, HIGH);
188-
delayMicroseconds(10);
189-
digitalWrite(TP, LOW);
190-
19192
long duration = pulseIn(EP,HIGH);
19293
long distance_cm = duration /29 / 2 ;
19394
return distance_cm;

src/MakerBoard.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* @file MakerBoard.h
33
* @author Yoshinobu Obata
44
* @brief Standard library for メイカーボード
5-
* @version 0.1
6-
* @date 2024-09-10
5+
* @version 1.0.3
6+
* @date 2026-01-20
77
* Change log:
8+
* 2026-01-20: Added Ultrasonic class for ultrasonic sensor support.
89
*
9-
* @copyright Copyright (c) 2024 Yukai Engineering Inc.
10+
* @copyright Copyright (c) 2026 Yukai Engineering Inc.
1011
*
1112
*/
1213
#ifndef MakerBoard_H

0 commit comments

Comments
 (0)