자율주행 모드를 테스트 하려고 하는데 자꾸 에러가 나네요 ㅜㅜ
오류메세지는
아두이노:1.6.12 (Mac OS X), 보드:"Arduino Uno"
/var/folders/qf/3xbcp_353d93lrwj5y4jnxl00000gn/T/arduino_modified_sketch_865481/MotorParty.pde: In function 'void loop()':
MotorParty:37: error: 'Ultrasonic' was not declared in this scope
Ultra_d = Ultrasonic();
^~~~~~~~~~
/var/folders/qf/3xbcp_353d93lrwj5y4jnxl00000gn/T/arduino_modified_sketch_865481/MotorParty.pde:37:13: note: suggested alternative: 'Ultra_d'
Ultra_d = Ultrasonic();
^~~~~~~~~~
Ultra_d
MotorParty:53: error: 'Servo_con' was not declared in this scope
val = Servo_con();
^~~~~~~~~
/var/folders/qf/3xbcp_353d93lrwj5y4jnxl00000gn/T/arduino_modified_sketch_865481/MotorParty.pde:53:13: note: suggested alternative: 'Servo_h'
val = Servo_con();
^~~~~~~~~
Servo_h
/var/folders/qf/3xbcp_353d93lrwj5y4jnxl00000gn/T/arduino_modified_sketch_865481/MotorParty.pde: In function 'void motor_role(int, int, int)':
MotorParty:85: error: a function-definition is not allowed here before '{' token
int Ultrasonic(){
^
MotorParty:96: error: a function-definition is not allowed here before '{' token
int Servo_con() {
^
MotorParty:113: error: expected '}' at end of input
}
^
exit status 1
'Ultrasonic' was not declared in this scope
라고 뜨네요
예제를 보고 그대로 쓰긴했는데 밑에는 입력한 예제입니다
#include <Servo.h>
Servo EduServo;
int trigPin = 13;
int echoPin = 12;
int Ultra_d = 0;
int RightMotor_E_pin = 5;
int LeftMotor_E_pin = 6;
int RightMotor_1_pin = 8;
int RightMotor_2_pin = 9;
int LeftMotor_3_pin = 10;
int LeftMotor_4_pin = 11;
int motor_s = 160;
int val = 0;
void setup() {
EduServo.attach(2);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(RightMotor_E_pin, OUTPUT);
pinMode(RightMotor_1_pin, OUTPUT);
pinMode(RightMotor_2_pin, OUTPUT);
pinMode(LeftMotor_3_pin, OUTPUT);
pinMode(LeftMotor_4_pin, OUTPUT);
pinMode(LeftMotor_E_pin, OUTPUT);
Serial.begin(9600);
Serial.println("Welcome Eduino!");
}
void loop() {
Ultra_d = Ultrasonic();
Serial.println(Ultra_d);
motor_role(HIGH, HIGH, motor_s);
if(Ultra_d < 250) {
if (Ultra_d < 150) {
Serial.println("150 이하");
motor_role(LOW, LOW, motor_s);
delay(1000);
motor_role(HIGH, HIGH, 0);
delay(200);
}
else {
motor_role(HIGH, HIGH, 0);
delay(200);
Serial.println("150 이상.");
val = Servo_con();
if (val == 0) {
Serial.println("우회전.");
motor_role(HIGH, HIGH, 0);
delay(500);
motor_role(LOW, LOW, motor_s);
delay(500);
motor_role(LOW, HIGH, motor_s);
delay(800);
}
else if (val == 1) {
Serial.println("좌회전.");
motor_role(HIGH, HIGH, 0);
delay(500);
motor_role(LOW, LOW, motor_s);
delay(500);
motor_role(HIGH, LOW, motor_s);
delay(800);
}
}
}
}
void motor_role(int R_motor, int L_motor, int Speed){
digitalWrite(RightMotor_1_pin, R_motor);
digitalWrite(RightMotor_2_pin, !R_motor);
digitalWrite(LeftMotor_3_pin, L_motor);
digitalWrite(LeftMotor_4_pin, !L_motor);
analogWrite(RightMotor_E_pin, Speed);
analogWrite(LeftMotor_E_pin, Speed);
int Ultrasonic(){
long duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = ((float) (340 * duration) / 1000) / 2;
return distance;
}
int Servo_con() {
Eduservo.write(30);
int Ult_30 = Ultrasonic();
delay(1000);
Eduservo.write(150);
int Ult_150 = Ultrasonic();
delay(1000);
if(Ult_30 > Ult_150) {
val = 1;
}
else{
val = 0;
}
EduServo.write(90);
return val;
}