Recently I have been starting to work with Arduino, in one of my projects using Arduino Mega 2560, I have stuck on a issue where my code seems to work fine but when I open my serial monitor all my sonar readings are zero.
I have check all the connections, change the cables, check the voltage, replace the sonar but my readings are always zero, does anyone have an idea on what could be happening? I don't know if it is problem on my board or if could be any error on my code.
I would appreciate the help, at this point I run out of ideas
recently we had it fix by starting everything for scratch, this is the second time I have fix this issue. This usually happens after I don't run the system for a few days and my Sonar start to behave weird, reading either 0 or 0.94cm
We also have a second issue after we thought that we fix the sonar issue the other day. We get stuck on a voltage issue. We are using digital Pin 3 for our LED, our goal is use turn if off when distance is bellow SafetyDistance.
By running the code and printing the raw values (when the Sonar decided to work) I have been having a weird voltage, first it was .2 volts when we use PIN 5 and when we change for PIN 3 we have some fluctuation on the voltage between 1.3V and 3.43V, even decrease our resistance for 200 to 100 wasn't enough to light our LED
const int trigPin = 7;
const int echoPin = 8;
const int ledPin = 3;
float duration, distance;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
if (distance <= 15){
digitalWrite(ledPin, HIGH);
Serial.print("ON");
}
else{
digitalWrite(ledPin, LOW);
Serial.print("LOW");
}
delay(100);
}
recently we had it fix by starting everything for scratch, this is the second time I have fix this issue. This usually happens after I don't run the system for a few days and my Sonar start to behave weird, reading either 0 or 0.94cm
We also have a second issue after we thought that we fix the sonar issue the other day. We get stuck on a voltage issue. We are using digital Pin 3 for our LED, our goal is use turn if off when distance is bellow SafetyDistance.
By running the code and printing the raw values (when the Sonar decided to work) I have been having a weird voltage, first it was .2 volts when we use PIN 5 and when we change for PIN 3 we have some fluctuation on the voltage between 1.3V and 3.43V, even decrease our resistance for 200 to 100 wasn't enough to light our LED
const int trigPin = 7;
const int echoPin = 8;
const int ledPin = 3;
float duration, distance;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
if (distance <= 15){
digitalWrite(ledPin, HIGH);
Serial.print("ON");
}
else{
digitalWrite(ledPin, LOW);
Serial.print("LOW");
}
delay(100);
}
Update: We fix, power cable have snap but we still have a voltage issue. Our signal come for the cable at 5 v, but when measure at the LED the voltage is 1.5V which makes the LED dim, we try different LED and different resistors (100 and 330 Ohms).
So far we also try different lines on the bread board to check for possible flaws on the bread board
Update: We fix, power cable have snap but we still have a voltage issue. Our signal come for the cable at 5 v, but when measure at the LED the voltage is 1.5V which makes the LED dim, we try different LED and different resistors (100 and 330 Ohms).
So far we also try different lines on the bread board to check for possible flaws on the bread board