So for the school project i had to make the ultrasonic sensor measure distance which he will write on serial monitor and lcd ( i did those two and they work perfectly) but also add wireless module to it that will send it to another serial monitor adn i keep having problems with it. This time it keeps constantly writing distances on lcd and serial monitor but theyre always zero??
#include <LCD.h>
#include <NewPing.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm;
int distance;
RF24 radio(9, 10);
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin(16,2);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
distanceCm= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
radio.write (&distance, sizeof(distance));
}
I wired my Uno with an LCD and HCSR04 and changed trigPin to 7 and ehcoPin to 8 and ran the code. Writes correct range to both the LCD and serial. You should, I think, slow it down a bit as you may get false echos and maybe flood the serial port.
You changed the wiring on the rangefinder, too, right?
I changed the wiring only on the nRf24 (code too).
What do you mean by slow it down?
I manged to make LCD and sensor work together without adding nRF to the code but whenever i do something turns out wrong ( especially at the void loop part)
I would advise to make pins 9 and 10 for the radio module and 2 other pins for the HCSR04. The reason being is that pin 10 must be an OUTPUT pin for SPI to work. See the SPI reference.
To slow the code, you can add a delay() to the loop() or, preferably, use the blink without delay method to slow loop() a bit.
Like so:
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 100;
if (millis() - timer >= interval)
{
timer = millis();
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
distanceCm = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
radio.write (&distance, sizeof(distance));
}
}
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
it should be
LiquidCrystal_I2C lcd(0x27,16,2);
if you have no i2c then you will be right the way you wrote it with out the positive by memory , i will run a sketch and get back to you