Помогите с написанием кода, я чайник в арудино, возможно у вас получится лучше посмотреть в чем же ошибка, жду профессионального взгляда.
вот сам код
[code]
#include <Servo.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <IRremoteTools.h>
volatile int left_IR_Sensor;
volatile int right_IR_Sensor;
//
volatile int left_light;
volatile int right_light;
//
#include <Servo.h>
Servo myservo;
//
volatile int DL;
volatile int DM;
volatile int DR;
volatile int distance;
volatile int flag;
long ir_rec;
float checkdistance() {
digitalWrite(12, LOW);
delayMicroseconds(2);
digitalWrite(12, HIGH);
delayMicroseconds(10);
digitalWrite(12, LOW);
float distance = pulseIn(13, HIGH) / 58.00;
delay(10);
return distance;
}
void Detect_obstacle_distance() {
myservo.write(160);
for (int i = 1; i <= 3; i = i + (1)) {
DL = checkdistance();
delay(100);
}
myservo.write(20);
for (int i = 1; i <= 3; i = i + (1)) {
DR = checkdistance();
delay(100);
}
}
//
IRremote_h float checkdistanse
{
float checkdistance () {
})
digitalWrite(12, LOW);
delayMicroseconds(2);
digitalWrite(12, HIGH);
delayMicroseconds(10);
digitalWrite(12, LOW);
float distance = pulseIn(13, HIGH) / 58.00;
delay(10);
return distance;
}
//
long ir_rec;
IRrecv irrecv(3);
decode_results results;
//
char bluetooth_val;
//
char bluetooth_data;
void Infrared_Obstacle_Avoidance() {
flag = 0;
while (flag == 0) {
left_IR_Sensor = digitalRead(A1);
right_IR_Sensor = digitalRead(A2);
if (left_IR_Sensor == 0 && right_IR_Sensor == 0) {
stopp();
delay(500);
backward(220, 220);
delay(500);
if (random(1, 10) > 5) {
rotate_left(220, 220);;
} else {
rotate_right(220, 220);
}
delay(500);
} else if (left_IR_Sensor == 0 && right_IR_Sensor == 1) {
backward(220, 220);
delay(500);
rotate_right(220, 220);
delay(500);
} else if (left_IR_Sensor == 1 && right_IR_Sensor == 0) {
backward(220, 220);
delay(500);
rotate_left(255, 255);
delay(500);
} else {
forward(150, 150);
}
if (Serial.available())
{
bluetooth_data = Serial.read();
if (bluetooth_data == 'S') {
flag = 1;
}
}
}
float checkdistance() {
digitalWrite(12, LOW);
delayMicroseconds(2);
digitalWrite(12, HIGH);
delayMicroseconds(10);
digitalWrite(12, LOW);
float distance = pulseIn(13, HIGH) / 58.00;
delay(10);
return distance;
}
void Ultrasonic_Obstacle_Avoidance() {
flag = 0;
while (flag == 0) {
DM = checkdistance();
if (DM < 20) {
stopp();
delay(1000);
myservo.write(160);
for (int i = 1; i <= 3; i = i + (1)) {
DL = checkdistance();
delay(100);
}
myservo.write(20);
for (int i = 1; i <= 3; i = i + (1)) {
DR = checkdistance();
delay(100);
}
if (DL < 50 || DR < 50) {
if (DL > DR) {
myservo.write(90);
rotate_left(255, 255);
delay(500);
forward(200, 200);
} else {
myservo.write(90);
rotate_right(255, 255);
delay(500);
forward(200, 200);
}
} else {
if (random(1, 10) > 5) {
myservo.write(90);
rotate_left(255, 255);
delay(500);
forward(200, 200);
} else {
myservo.write(90);
rotate_right(255, 255);
delay(500);
forward(200, 200);
}
}
} else {
forward(200, 200);
}
if (Serial.available())
{
bluetooth_data = Serial.read();
if (bluetooth_data == 'S') {
flag = 1;
}
}
}
}
IRrecv irrecv(3);
decode_results results;
void Infrared_Remote_Control() {
flag = 0;
while (flag == 0) {
if (irrecv.decode(&results)) {
ir_rec = results.value;
irrecv.resume();
}
if (ir_rec == 0xFF629D) {
forward(240, 240);
}
if (ir_rec == 0xFFA857) {
backward(240, 240);
}
if (ir_rec == 0xFF22DD) {
turn_left(100, 255);
}
if (ir_rec == 0xFFC23D) {
turn_right(255, 100);
}
if (ir_rec == 0xFF30CF) {
rotate_left(240, 240);
}
if (ir_rec == 0xFF7A85) {
rotate_right(240, 240);
}
if (ir_rec == 0xFF02FD) {
stopp();
}
if (Serial.available())
{
bluetooth_data = Serial.read();
if (bluetooth_data == 'S') {
flag = 1;
}
}
}
}
void setup() {
Serial.begin(9600);
left_IR_Sensor = 0;
right_IR_Sensor = 0;
pinMode(A1, INPUT);
pinMode(A2, INPUT);
//
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(6, OUTPUT);
//
left_light = 0;
right_light = 0;
pinMode(3, INPUT);
pinMode(10, INPUT);
//
myservo.attach(10);
DL = 0;
DM = 0;
DR = 0;
myservo.write(90);
//
pinMode(12, OUTPUT);
pinMode(13, INPUT);
//
distance = 0;
//
irrecv.enableIRIn();
//
flag = 0;
}
void loop() {
// put your main code here, to run repeatedly:
forward();
delay(1000);
backward();
delay(1000);
rotate_left();
delay(1000);
rotate_right();
delay(1000);
stopp();
delay(1000);
//
left_IR_Sensor = digitalRead(A1);
right_IR_Sensor = digitalRead(A2);
Serial.print("left_light_value = ");
Serial.println(left_IR_Sensor);
Serial.print("right_light_value = ");
Serial.println(right_IR_Sensor);
Serial.println("");
delay(500);
//
left_light = digitalRead(A1);
right_light = digitalRead(A2);
// Serial.print("left_light_value = ");
// Serial.println(left_light);
// Serial.print("right_light_value = ");
// Serial.println(right_light);
if (left_light == 0 && right_light == 0) {
stopp();
delay(500);
backward();
delay(500);
if (random(1, 10) > 5)
{
rotate_left();
}
else
{
rotate_right();
}
delay(500);
} else if (left_light == 0 && right_light == 1) {
backward();
delay(500);
rotate_right();
delay(500);
} else if (left_light == 1 && right_light == 0) {
backward();
delay(500);
rotate_left();
delay(500);
} else {
forward();
}
//
for (int POS = 0; POS <= 180; POS = POS + (1)) {
myservo.write(POS);
delay(15);
}
for (int POS = 180; POS >= 0; POS = POS - 1) {
myservo.write(POS);
delay(15);
}
//
Serial.print(checkdistance());
Serial.println("cm");
delay(100);
//
DM = checkdistance();
if (DM < 20) {
stopp();
delay(1000);
Detect_obstacle_distance();
if (DL < 50 || DR < 50) {
if (DL > DR) {
myservo.write(90);
rotate_left();
delay(500);
forward();
} else {
myservo.write(90);
rotate_right();
delay(500);
forward();
}
} else {
if (random(1, 10) > 5) {
myservo.write(90);
rotate_left();
delay(500);
forward();
} else {
myservo.write(90);
rotate_right();
delay(500);
forward();
}
}
} else {
forward();
}
//
distance = checkdistance();
if (distance >= 20 && distance <= 60)
{
forward();
} else if (distance > 10 && distance < 20) {
stopp();
} else if (distance <= 10) {
backward();
} else {
stopp();
}
//
if (irrecv.decode(&results)) {
ir_rec = results.value;
irrecv.resume();
} else {
}
if (ir_rec == 0xFF629D) {
forward();
}
if (ir_rec == 0xFFA857) {
backward();
}
if (ir_rec == 0xFF22DD) {
turn_left();
}
if (ir_rec == 0xFFC23D) {
turn_right();
}
if (ir_rec == 0xFF30CF) {
rotate_left();
}
if (ir_rec == 0xFF7A85) {
rotate_right();
}
if (ir_rec == 0xFF02FD) {
stopp();
}
//
if (Serial.available())
{
bluetooth_val = Serial.read();
Serial.println(bluetooth_val);
}
//
if (Serial.available())
{
bluetooth_data = Serial.read();
Serial.println(bluetooth_data);
}
switch (bluetooth_data) {
case 'F':
forward(250, 250);
break;
case 'G':
backward(240, 240);
break;
case 'H':
turn_left(100, 255);
break;
case 'I':
turn_right(255, 100);
break;
case 'J':
rotate_left(240, 240);
break;
case 'K':
rotate_right(240, 240);
break;
case 'S':
stopp();
break;
case 'E':
Infrared_Obstacle_Avoidance();
break;
case 'C':
Ultrasonic_Obstacle_Avoidance();
break;
case 'D':
Infrared_Remote_Control();
break;
}
}
//
}
void forward()
{ digitalWrite(2, LOW);
analogWrite(5, 255);
digitalWrite(4, HIGH);
analogWrite(6, 255);
}
void backward()
{ digitalWrite(2, HIGH);
analogWrite(5, 255);
digitalWrite(4, LOW);
analogWrite(6, 255);
}
void rotate_left()
{ digitalWrite(2, HIGH);
analogWrite(5, 255);
digitalWrite(4, HIGH);
analogWrite(6, 255);
}
void rotate_right()
{ digitalWrite(2, LOW);
analogWrite(5, 255);
digitalWrite(4, LOW);
analogWrite(6, 255);
}
void turn_left(int a, int b)
{ digitalWrite(2, LOW);
analogWrite(5, a);
digitalWrite(4, HIGH);
analogWrite(6, b);
}
void turn_right(int a, int b)
{ digitalWrite(2, LOW);
analogWrite(5, a);
digitalWrite(4, HIGH);
analogWrite(6, b);
}
void stopp()
{ digitalWrite(2, HIGH);
analogWrite(5, 0);
digitalWrite(4, LOW);
analogWrite(6, 0);
}
[/code]