hi.
I "successfully" created something but, well, I am not a programmer :D
What I wanted... works! But... hm... there are some issues. :D
There are 2 nRF24L01 modules with Arduinos "communicating" with each other. The aim was, to blink an LED 3 times on one side, then blink the other side LED 3 times. (kind of synchronized way).
Issues and additional functions. (+ information)
Of course delays are not good. Well, in this code where Arduino doesn't need to do so much next to the main job, could be OK. But, actually, there would be some other to do for the processor.
However, I wasn't able to use millis "inside" nRF codes. It just didn't work.
I have tried putting the non-blocking codes at the place of delay, or using a function and calling in, but it didn't matter... it just did not work.
I want a "LOCAL" blinker, that only works if "!radio.available." So if no radio signal is received, the local blinker would take place. It is not as easy, as it sounds. If there is a little gap, or I don't know, the "else" section triggered and the blinking on the side where I applied the Local blinker, started to behave strangely. Blink was odd.
I want it to start the local blink only, if the other side is "definitely" OFF. During the "normal" working condition I don't want it to be triggered.
Sometimes both sides are OFF after a time. I think the signal is disturbed by phones, wifi or whatever. I want an "auto process restart" if they stop working but they are available!
I am not sure how.
Can you please give ideas... ? Easy... I am noob
Inserted code for one board, called "transmitter"
The only differences on the "receiver" board are:
radio.stopListening();
int data = 0;
radio.write(&data, sizeof(data));
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
#define LED_PIN_1 2 // main LED
#define LED_PIN_2 3 // summy led - but can be used if needed to
#define BLINK_INTERVAL_1 100
unsigned long lastBlinkTime = 0;
bool ledState = LOW;
int currentLed = LED_PIN_1;
int blinkCount = 0;
int data = 1;
const byte addresses[][6] = {"00001", "00002"};
int button_pin = 5;
boolean button_state = 0;
void setup() {
Serial.begin(9600);
pinMode(button_pin, INPUT);
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
radio.begin();
radio.openWritingPipe(addresses[1]); // 00001
radio.openReadingPipe(1, addresses[0]); // 00002
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
radio.write(&data, sizeof(data));
}
void loop() {
//currentMillis = millis();
//onlyOnce();
delay(5);
radio.startListening();
if (radio.available()){
//Serial.println("RADIO AVAILABLE!");
int data;
Serial.print("DATA IS: ");
Serial.println(data);
radio.read(&data, sizeof(data));
delay(10);
radio.read(&data, sizeof(data)); //read again for sure!
if (data == 0) {
//instedOfDelay();
digitalWrite(LED_PIN_1, HIGH);
delay(100);
digitalWrite(LED_PIN_1, LOW);
delay(100);
digitalWrite(LED_PIN_1, HIGH);
delay(100);
digitalWrite(LED_PIN_1, LOW);
delay(100);
digitalWrite(LED_PIN_1, HIGH);
delay(100);
digitalWrite(LED_PIN_1, LOW);
delay(250);
radio.stopListening();
int data = 1;
radio.write(&data, sizeof(data));
} else {
//that code is never gonna run as data never be 1 here.
Serial.print("DATA IS in ELSE: ");
Serial.println(data);
}
} else {
//localLED_blinker???
/* FUCK :D it's NOT good...
radio.stopListening();
radio.write(&data, sizeof(data));
delay(500);
RF_Missing_LED_Blink();
*/
}
}
void RF_Missing_LED_Blink() {
if (millis() - lastBlinkTime >= BLINK_INTERVAL_1) {
// Save the current time as the last blink time
lastBlinkTime = millis();
// Change the LED state
ledState = !ledState;
digitalWrite(currentLed, ledState);
// If the LED just turned off, increment the blink count
if (ledState == LOW) {
blinkCount++;
}
// If the LED has blinked 3 times, switch to the other LED
if (blinkCount == 3) {
// Reset the blink count
blinkCount = 0;
// Switch the LED
if (currentLed == LED_PIN_1) {
currentLed = LED_PIN_2;
} else {
currentLed = LED_PIN_1;
}
}
}
}
/*
void instedOfDelay() {
if (currentMillis - previousMillis >= BLINK_INTERVAL_1) {
previousMillis = currentMillis;
blinkCount++;
if (blinkCount <= 6) { // 6 because each blink consists of turning on and off
digitalWrite(LED_PIN_1, !digitalRead(LED_PIN_1));
} else {
digitalWrite(LED_PIN_1, LOW); // Make sure the LED is off at the end
radio.stopListening();
data = 0;
radio.write(&data, sizeof(data));
}
}
}
*/
Well, it is not the best... I have no right experience yet with it, but as far as I see, it cannot handle complex codes, just like other AI for Arduino. I have tried a lot because it is very rare when I can get help from a real person. Normally, I am impatient - because I have billions to do around me.
AI fast, but not so smart just yet.
So I always try by myself, but - as I am not a programmer - I need to learn a lot, that I enjoy, but time what I cannot get... and earning money is a hard and time-consuming job...
The point is, I will touch ChatGp but I hope some one can help here too.
Thx
I am not sure if you have realised or not but I have inserted MY code so where is the code production here? LOL
I asked for some help - to learn something.... ahh, actually learning is not an issue, I can read and watch but understanding it is different. OR thinking about something that I wouldn't otherwise.
By the way, I have just sorted out again something in the code.
Otherwise, I do not like your style! Don't tell me what and where and who should I do. This is not what I can tolerate in any circumstances. If you won't or cannot help, then why do you spend time writing what you have just written? LoL
Especially, even if you are NOT RIGHT. As I above mentioned. I have a code, there is no production! I just need help!
Now, I show you how can you help!!! Easy!
Just share your code or anything that can help others! LoL
Here it is.
// this is for a simple 3 times LED blinker that is synchronized and switched on off opposite
// with an other Arduino + nRF24L01
// we can call this as Sender (TX module) and the other board is Receiver (RX)
// but the truth is, both are TX and RX as well!
// on the other board the code is same except these:
// CODE is still under development! SOme function is still not working!
// but synchronising and auto restart is works now!
/*
int data = 0;
----
radio.openWritingPipe(addresses[0]); // 00001
radio.openReadingPipe(1, addresses[1]); // 00002
----
This doesn't need there:
if (currentMillis - lastActionTime >= 1000) {
delay(1000); // Wait for a moment to allow the other board to restart its process
restartProcess();
}
----
case WAITING:
if (radio.available()) {
int receivedData;
radio.read(&receivedData, sizeof(receivedData));
if (receivedData == 0) {
state = BLINKING;
lastActionTime = currentMillis;
}
----
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
#define LED_PIN_1 2 // main LED
#define LED_PIN_2 3 // for later
int data = 1;
const byte addresses[][6] = {"00001", "00002"};
unsigned long lastActionTime = 0;
unsigned long lastBlinkTime = 0;
unsigned long interval = 100; // Interval between LED state changes (in milliseconds)
int currentLed = LED_PIN_1;
bool ledState = LOW;
int blinkCount = 0;
enum State {
WAITING,
BLINKING,
CONNECTION_LOST_BLINKING
};
State state = WAITING;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
radio.begin();
radio.setChannel(0x6f);
radio.openWritingPipe(addresses[1]); // 00001
radio.openReadingPipe(1, addresses[0]); // 00002
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
radio.write(&data, sizeof(data));
}
void loop() {
unsigned long currentMillis = millis();
delay(5);
radio.startListening();
switch (state) {
case WAITING:
if (radio.available()) {
int receivedData;
radio.read(&receivedData, sizeof(receivedData));
if (receivedData == 0) {
state = BLINKING;
lastActionTime = currentMillis;
}
}
if (currentMillis - lastActionTime >= 1000) {
delay(1000); // Wait for a moment to allow the other board to restart its process
restartProcess();
}
/*
? call CONNECTION_LOST_BLINKING: ?
*/
break;
case BLINKING:
if (currentMillis - lastActionTime >= interval) {
lastActionTime = currentMillis;
if (blinkCount < 6) { // Blink LED 3 times
digitalWrite(LED_PIN_1, !digitalRead(LED_PIN_1));
blinkCount++;
} else {
digitalWrite(LED_PIN_1, LOW); // Turn off LED after blinking 3 times
radio.stopListening();
int newData = 1;
radio.write(&newData, sizeof(newData)); //tell the other board it's time to flash the led
blinkCount = 0; // Reset blink count for next time
delay(100);
state = WAITING;
}
break;
case CONNECTION_LOST_BLINKING:
Serial.println("Local Flasher Activated!");
Local_LED_Flash();
break;
}
}
}
void Local_LED_Flash() {
if (millis() - lastBlinkTime >= interval) {
// Save the current time as the last blink time
lastBlinkTime = millis();
// Change the LED state
ledState = !ledState;
digitalWrite(currentLed, ledState);
// If the LED just turned off, increment the blink count
if (ledState == LOW) {
blinkCount++;
}
// If the LED has blinked 3 times, switch to the other LED
if (blinkCount == 3) {
// Reset the blink count
blinkCount = 0;
// Switch the LED
if (currentLed == LED_PIN_1) {
currentLed = LED_PIN_2;
} else {
currentLed = LED_PIN_1;
}
}
}
}
void restartProcess() {
Serial.println("Process Restarted!");
state = WAITING; // Reset state
blinkCount = 0; // Reset blink count
digitalWrite(LED_PIN_1, LOW); // Turn off LED
radio.stopListening(); // Stop listening
radio.write(&data, sizeof(data)); // Send data to restart the process
// Start listening again
radio.startListening();
}
Actually the problem is long before any coding is done. You need a design that will let either of the two devices cycle through sending and receiving and not caring if something is received or not. You have the problem of "who goes first" or which Arduino do you start first and then go start the second one. You must design so either one will go first and continue trying until it discovers the other Arduino. This is NOT easy to do, but must be done to BEGIN you synchronization and get it to continue until one or the other Arduino is stopped.
Thank you very much! That was helpful! I really appreciated it!
At the moment the shared code is working. If I switch off either one and switch back, they start to operate normally. Also, if the connection is lost for a very short period, for any reason, they carry on flashing as the connection is re-built by this code and restartProcess();
if (currentMillis - lastActionTime >= 1000) {
delay(1000); // Wait for a moment to allow the other board to restart its process
restartProcess();
}
This runs only on the first board, (on one board) as it is unnecessary on both. If one board won't be used at all, there is no connection at all. So it is OK.
But I will try to follow your idea and re-think the code.
It is really hard if you are not a programmer too bad, I can do a bunch of things because I love learning what I love, but sometimes this lifestyle consumes a lot of time Anyway, thx!!!