so i'm doing a project with 3 distance sensors. The right one dosn't work in my code and i have no idea why. I've tested it on another code and it worked perfectly
In the start function i've checked the values of the sensor and always the value for the right one is 0.
int v[4];
//initializari pentru senzorii de distanta
int right_trigPin = 12;
int right_echoPin = A2;
int front_trigPin = A3;
int front_echoPin = 5;
int left_trigPin = A4;
int left_echoPin = 7;
//initializari pentru schimbarea functiilor
const int pushButton = 2;
const int ledrosu = 3;
const int ledverde = 4;
boolean semnal;
int nr=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
pinMode(right_trigPin, OUTPUT);
pinMode(right_echoPin, INPUT);
//digitalWrite(right_trigPin, LOW);
pinMode(front_trigPin, OUTPUT);
pinMode(front_echoPin, INPUT);
//digitalWrite(fata_trigPin, LOW);
pinMode(left_trigPin, OUTPUT);
pinMode(left_echoPin, INPUT);
//digitalWrite(left_trigPin, LOW);
}
void Start()///afiseaza valorile senzorilor si pune frana pe motoare
{
v[1] = distance(left_trigPin, left_echoPin);
v[2] = distance(front_trigPin, front_echoPin);
v[3] = distance(right_trigPin, right_echoPin);
Serial.print(v[3]);
Serial.print(' ');
Serial.print(v[2]);
Serial.print(' ');
Serial.print(v[1]);
Serial.print('\n');
Start();
}
float distance(int trigPin, int echoPin) {
//float viteza,timp,distanta;
float timp;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
timp = pulseIn(echoPin, HIGH);
//int temperatura=20; ///Celsius
//viteza=(331.5+0.607temperatura);//vSunet_m/s
//distanta=timpviteza/2*100;///m->cm
return timp / 58; //The SRF04 provides an echo pulse proportional to distance.us - This website is for sale! - distance Resources and Information. //restul despre senzori pe SRF05 Technical Documentation
}
float error, lastError = 0;
int motorSpeed;
int kp = 4, kd = 5; //fara integrale
int minSpeed = 130, maxSpeed = 210;
int leftSpeed, rightSpeed;
void loop() {
Start();
}