As a newbie, I have been working on this sketch for over a week and I think I need help.
I have 2 boards connected via nRF24L01. I want the Transmitter to send a signal to the Receiver when a tilt switch is changed. This will set a LED on the Receiver to blink. I have succeeded this with the help of the examples on the web.
What I cant do is keep the LED blinking and reset it with a button push on the Receiver end.
I have enclosed the sketches but I removed the button part of the Receiver sketch for clarity.
I would appreciate any help.
Many thanks
Transmitter sketch:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001"; //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
int button_pin = 2;
int transbuttonCounter = 0;
int lastButtonState = 0;
boolean button_state = 0;
void setup() {
pinMode(button_pin, INPUT);
radio.begin(); //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening(); //This sets the module as transmitter
}
void loop()
{
button_state = digitalRead(button_pin);
//if (transbuttonCounter != lastButtonState) {
if(button_state == HIGH) {
// transbuttonCounter++;
{
const char text[] = "Button State Trans is HIGH/Flash";
radio.write(&text, sizeof(text)); //Sending the message to receiver
}
} else
{
const char text[] = "ButtonState Transmit is LOW";
radio.write(&text, sizeof(text)); //Sending the message to receiver
}
radio.write(&button_state, sizeof(button_state)); //Sending the message to receiver
delay(1000);
if (transbuttonCounter % 1 == 0) {
digitalWrite (button_pin, HIGH);
} else {
digitalWrite(button_pin, LOW);
}
}
// }// new IF Counter
Receiver sketch:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
// new varibles for button section
boolean button_state = 0;
boolean button_2 = 0;
int led_pin = 3;
int buttonPin = 7;
// new varibles from millis
unsigned long previousMillis = 0;
const long interval = 1000;
int ledState = LOW;
void setup() {
pinMode(3, OUTPUT);
pinMode(7, INPUT); // initialize the pushbutton pin as an input:
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening(); //This sets the module as receiver
} //endSET UP
void loop() {
//digitalWrite(buttonPin, HIGH);
if (radio.available()) { //Looking for the data.
char text[32] = ""; //Saving the incoming data
radio.read(&text, sizeof(text)); //Reading the data
radio.read(&button_state, sizeof(button_state)); //Reading the data
if(button_state == HIGH) { /// from Transmitter {
Serial.println(text); // text from Transmitter
// flashes the LED
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(led_pin, ledState);
}
} else
{
digitalWrite(3, LOW);
//digitalWrite(7, LOW);
Serial.println("button on TRANS not pressed");
//digitalWrite(button_2, LOW);
}
} // if RAdio..
delay(50);
} // void loop