我已经从超声波安装了多个库,我安装的最后一个是"ultrasonic-master library"。这是图书馆的问题吗?
我有这个代码使用超声波传感器,但我得到'unsigned int ultrasonic::timing()'在这个上下文中是私有的
我能试着解决这个问题吗?如有任何帮助,不胜感激。
#include <Ultrasonic.h>
const int IN4 = 6;
const int IN3 = 7;
const int IN2 = 5;
const int IN1 = 4;
const int ENA = 3;
const int ENB = 2;
const int echoPin = 8; //digtal pin used for HC-SR04 ECHO in order to receive the signal
const int trigPin = 9; //digtal pin used for HC-SR04 ECHO in order to send the signal
Ultrasonic ultrasonic(trigPin, echoPin); //initializing the arduino pins
int distance;
String result;
int velocity = 0;
//on or off
boolean go = true;
float dist_cm = distance;
void setup() {
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
analogWrite(ENA, 145);
analogWrite(ENB, 145);
}
void loop() {
float distance = cmMsec();
dist_cm = distance;
if (dist_cm <= 10) {
decisao();
}
delay(100);
}
float cmMsec() {
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
return (cmMsec);
delay(10);
}
void go_forward()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENA, velocity);
analogWrite(ENB, velocity);
}
void freio() {
digitalWrite(IN4, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN1, LOW);
analogWrite(ENA, velocity);
analogWrite(ENB, velocity);
}
void decisao() {
// this block is responsible for change the speed of the car when the distance goes UP
if (go == true) {
if (distance > 30) {
while (velocity < 120) {
go_forward();
velocity = velocity + 10;
delay(50);
}
}
if (velocity >= 120) {
while (distance >= 60 && distance <= 70) {
siga_em_frente();
velocity = velocity + 10;
delay(50);
}
}
if (distance < 70) {
while (velocity < 145) {
go_forward();
velocity = velocity + 10;
delay(50);
}
}
if (velocity >= 145) {
while (distance >= 90 && distance <= 100) {
go_forward();
velocity = velocity + 10;
delay(50);
}
}
if (distance < 100) {
while (velocity < 220) {
go_forward();
velocity = velocity + 10;
delay(50);
}
}
}
delay(10000);
// this block is responsible for change the speed of the car when the distance goes UP
if (go == true) {
if (distance >= 90 && distance <= 100) {
while (velocity <= 145) {
go_forward();
velocity = velocity - 10;
delay(50);
}
}
if (velocity > 140 && velocity <= 145) {
while (distance >= 70 && velocity <= 60) {
go_forward();
velocity = velocity - 10;
delay(50);
}
}
if (distance <= 60 && velocity <= 30) {
while (velocity <= 120) {
go_forward();
velocity = velocity - 10;
delay(50);
}
}
if (velocity > 110 && velocity <= 120) {
while (distance < 30) {
velocity = velocity - 10;
delay(50);
}
}
if (distance < 30) {
if (velocity > 5 && velocity <= 0) {
go = !go;
}
}
}
if (go == true) {
break();
}
}
unsigned int Ultrasonic::timing()
是一个私有方法。在超声波类中,该方法被声明为私有。这意味着它只能被超声波类本身调用。它不打算供您使用
为了使用它,它必须是公共的。所以你需要编辑这个类。