#include <LiquidCrystal.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 53
LiquidCrystal lcd(12, 11, 5, 4, 3, 10);
int trigPin = 6;
int echoPin = 7;
int greenLed = 38;
int redLed = 39;
int m;
int s;
int dc;
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[10];
bool newData = false;
void setup() {
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
digitalWrite(greenLed, LOW);
pinMode(53, OUTPUT);
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
void loop() {
int duration;
int distance;
digitalWrite(redLed, HIGH);
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 100 && distance >= 0) {
digitalWrite(greenLed, HIGH);
while (digitalRead(greenLed) == HIGH) {
tempo();
getData();
showData();
}
}
}
void tempo() {
lcd.setCursor(0,1);
dc = dc + 1;
lcd.print(m);
lcd.print(":");
lcd.print(s);
lcd.print(".");
lcd.print(dc);
delay(100);
if (dc == 9) {
dc = 0;
s = s + 1;
}
if (s == 60) {
s = 0;
m = m + 1;
}
}
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
digitalWrite(ledVerde, LOW);
newData = false;
}
}
Comments
Please log in or sign up to comment.