Hi!
My problem is that the ping sensor doesn´t sending signals, but there is voltage on it.
For some time it worked properly, but after i changed the code (just added a part to it), it doesn´t work at all and even the ACT LED isn´t blinking.
Could you help me pls?
thanks
the code:
#include "hangok.h"
#include <Servo.h>
int melody[] = {
NOTE_D3, NOTE_E3, NOTE_F3, NOTE_E3, NOTE_C3, NOTE_F2, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_E3, NOTE_C3,NOTE_A3, NOTE_G3, NOTE_D3, NOTE_E3, NOTE_F3, NOTE_E3, NOTE_C3,NOTE_G2, NOTE_F2
};
int noteDurations[] = {
4, 4,1,2,4,1,4, 4,1,2,4,2,1,4, 4,1,2,4,1,1 };
const int motor1Pin1 = 4;
const int motor1Pin2 = 5;
const int motor2Pin1 = 6;
const int motor2Pin2 = 7;
int leftDistance, rightDistance;
const int pingPin = 8;
Servo pingServo;
long duration,cm;
void setup()
{
motorstop();
delay(500);
for (int thisNote = 0; thisNote < 20; thisNote++) {
int noteDuration = 1000/noteDurations[thisNote];
tone(12, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
Serial.begin(9600);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pingServo.attach(9);
pingServo.write(90);
}
long ping()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = duration / 29 / 2;
}
int motorgo()
{
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(motor2Pin1, 255);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor1Pin1, LOW);
analogWrite(motor2Pin1, 255);
}
int motorstop()
{
analogWrite(motor1Pin2, 0);
analogWrite(motor2Pin1, 0);
}
int turnLeft()
{
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(motor2Pin1, 200);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin1, HIGH);
analogWrite(motor1Pin2, 200);
delay(500);
}
int turnRight()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
analogWrite(motor1Pin2, 200);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
analogWrite(motor2Pin1, 200);
delay(500);
}
void loop()
{
ping();
if (cm>=50){
motorgo();
delay(250);
}
else
{
motorstop();
pingServo.write(140);
delay(200);
rightDistance = ping(); //scan to the right
delay(200);
pingServo.write(40);
delay(350);
leftDistance = ping(); //scan to the left
delay(200);
pingServo.write(90); //return to center
delay(80);
compareDistance();
}
}
void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
turnLeft();
}
else {
turnRight(); //if right is less obstructed
}
}