I'm trying to make a nRf24L01 network with multitasking, but no matter how i redo the codes for master and slaves and replace the millis(), they don't want to work properly.
What I want to do is a program that sends to slave an information about time interval which is selected before starting program1() and after that I want to turn on LED's on slaves for that interval and then turn them off.
The previous versions at first attempt were sending information about selected interval but the LED's were turning on at the second program run. Now the whole master program just ends immediately.
Here are the current codes:
Master:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <SPI.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
String screen[5]={"Run program","Choose Program","Reaction time","Shoots","Score"};
int screenIndex=0;
//button addresses
const int pinLeft=4;
const int pinRight=5;
const int pinUp=6;
const int pinDown=7;
//Modes
String difficultyLevels[5][5] = {{""},{"Rookie","Plus & minus","Risk","Speed"},{"1 sec","2 sec","5 sec","7 sec","10 sec"},{""},{""}};
int difficultyIndex = 0;
//Indexes
int optionsIndex[5]={0,0,0,0,0};
//Utils
bool screenWasChaned = true;
bool intputWasChaged_Left = false; //
bool intputWasChaged_Right = false;
bool intputWasChaged_Up = false;
bool intputWasChaged_Down = false;
byte LED = 0;
unsigned long currentTime;
unsigned long interval = 1000;
RF24 radio(10, 9); //nRF24L01 pins (CE, CSN)
const uint64_t rAddress[] = {0xB00B1E50D2LL, 0xB00B1E50C3LL, 0xB00B1E50E5LL};
const uint64_t wAddress[] = {0xB00B1E50B1LL, 0xB00B1E50A4LL, 0xB00B1E50F6LL};
void setup()
{
Serial.begin(57600);
Serial.println("Program start");
radio.begin();
radio.openReadingPipe(1, rAddress[0]); //open read pipe
radio.openReadingPipe(2, rAddress[1]);
radio.openReadingPipe(3, rAddress[2]);
radio.stopListening();
pinMode(pinLeft, INPUT);
pinMode(pinRight, INPUT);
pinMode(pinDown, INPUT);
pinMode(pinUp, INPUT);
digitalWrite(pinLeft, HIGH); //button responds to 0
digitalWrite(pinRight, HIGH);
digitalWrite(pinDown, HIGH);
digitalWrite(pinUp, HIGH);
lcd.init();
lcd.backlight();
lcd.clear();
currentTime = millis(); //
}
void loop()
{
if(screenWasChaned){
if (screenIndex==3)
{
displayScreen(screenIndex,String(optionsIndex[screenIndex]));
screenWasChaned = false;
}
else{
displayScreen(screenIndex,difficultyLevels[screenIndex][optionsIndex[screenIndex]]);
screenWasChaned = false;
}
}
inputControl();
inputResolve();
}
void displayScreen(int screenIndex, String value)
{
lcd.clear();
lcd.print(screen[screenIndex]);
lcd.setCursor(0,1);
lcd.print(value);
}
void inputControl()
{
int readingLeft=digitalRead(pinLeft);
int readingRigth=digitalRead(pinRight);
int readingUp=digitalRead(pinUp);
int readingDown=digitalRead(pinDown);
if (readingLeft == LOW)
{
if (screenIndex>0 && !intputWasChaged_Left)
{
screenIndex = screenIndex-1;
screenWasChaned = true;
intputWasChaged_Left = true; //czeka na resolve przyciku aby nie było podwójnych przeskoków przy trzymaniu klawisza;
}
}
if (readingRigth == LOW)
{
if (screenIndex < 4 && !intputWasChaged_Right)
{
screenIndex = screenIndex+1;
screenWasChaned = true;
intputWasChaged_Right = true;
}
}
if (readingUp == LOW)
{
if(screenIndex==0 && !intputWasChaged_Up)
{
StartProgram(optionsIndex[1]);
intputWasChaged_Up = true;
}
if(screenIndex==3 && optionsIndex[screenIndex]>=3 && !intputWasChaged_Up)
{
optionsIndex[screenIndex] = optionsIndex[screenIndex]+1;
screenWasChaned = true;
intputWasChaged_Up = true;
}
if (optionsIndex[screenIndex]<3 && !intputWasChaged_Up)///lock opcji w góre
{
optionsIndex[screenIndex] = optionsIndex[screenIndex]+1;
screenWasChaned = true;
intputWasChaged_Up = true;
}
}
if (readingDown == LOW)
{
if (optionsIndex[screenIndex]>0 && !intputWasChaged_Down)
{
optionsIndex[screenIndex] = optionsIndex[screenIndex]-1;
screenWasChaned = true;
intputWasChaged_Down = true;
}
}
}
void inputResolve()
{
int readingLeft=digitalRead(pinLeft);
int readingRigth=digitalRead(pinRight);
int readingUp=digitalRead(pinUp);
int readingDown=digitalRead(pinDown);
if (readingLeft == HIGH && intputWasChaged_Left)
{
intputWasChaged_Left = false;
}
if (readingRigth == HIGH && intputWasChaged_Right)
{
intputWasChaged_Right = false;
}
if (readingUp == HIGH&&intputWasChaged_Up)
{
intputWasChaged_Up = false;
}
if (readingDown == HIGH&&intputWasChaged_Down)
{
intputWasChaged_Down = false;
}
}
void StartProgram(int ProgramIndex)
{
switch (ProgramIndex)
{
case 0:
program1();
break;
case 1:
program2();
break;
case 2:
program3();
break;
case 3:
program4();
break;
default:
break;
}
}
void program1()
{
unsigned long sendLED;
displayScreen(0, "Rookie start");
if(sendTime(optionsIndex[2]))
{
sendLED = currentTime;
for (int i = 0; i < optionsIndex[3]; i++)
{
byte targetIndex = random(0,3);
LED = 1;
if(currentTime - sendLED >= interval)
{
SendToSlave(LED,targetIndex);
sendLED = currentTime;
}
}
}
displayScreen(0,"END");
}
void program2(){displayScreen(0,"Program 2");}
void program3(){displayScreen(0,"Program 3");}
void program4(){displayScreen(0,"Program 4");}
bool sendTime(int recTime) //send the interval to slaves
{
bool done = false;
unsigned long sendRecTime;
sendRecTime = currentTime;
if(currentTime - sendRecTime>=1000)
{
radio.openWritingPipe(wAddress[0]);
radio.write(&recTime, sizeof(recTime));
Serial.println("Time SL 1");
}
if(currentTime - sendRecTime>=2000)
{
radio.openWritingPipe(wAddress[1]);
radio.write(&recTime, sizeof(recTime));
Serial.println("Time SL 2");
}
if(currentTime - sendRecTime>=3000)
{
radio.openWritingPipe(wAddress[2]);
radio.write(&recTime, sizeof(recTime));
Serial.println("Time SL 3");
}
done = true;
return done;
}
void SendToSlave(byte LED,byte adressIndex)
{
radio.stopListening();
Serial.print("Send to slave ");
Serial.println(adressIndex);
radio.openWritingPipe(wAddress[adressIndex]);
radio.write(&LED, sizeof(LED));
}
Slave
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 radio(10, 9); //nRF24L01 pins (CE, CSN)
const uint64_t rAddress = 0xB00B1E50B1LL;
const uint64_t wAddress[1] = {0xB00B1E50D2LL};
int recTime = 0;
bool Hit = false; //sensor state
byte LED = 0;
byte pipe = 0;
unsigned long currentTime;
unsigned long baseTime = 0;
unsigned long timer1 = 1000;
unsigned long timer2 = 2000;
void setup()
{
Serial.begin(57600);
Serial.println("Listening");
radio.begin();
radio.openReadingPipe(1, rAddress);
radio.startListening();
pinMode(3, OUTPUT); //LED green
pinMode(4, OUTPUT); //LED red
pinMode(5, OUTPUT); //LED blue
pinMode(8, INPUT); //vibration sensor
currentTime = millis();
}
void loop()
{
radio.startListening();
while (radio.available(&pipe))
{
if(recTime == 0)
catchTime();
radio.startListening();
radio.read(&LED, sizeof(LED));
if (LED == 1 && recTime == 1)
{
recTime_1();
}
else if (LED == 1 && recTime == 2)
{
recTime_2();
}
}
}
void catchTime()
{
radio.read(&recTime, sizeof(recTime));
Serial.print("Czas: ");
Serial.println(recTime);
}
void recTime_1()
{
baseTime = currentTime;
Serial.println("Light");
digitalWrite(3, HIGH);
if(currentTime - baseTime >= timer1)
{
digitalWrite(3, LOW);
Serial.println("Darkness");
}
}
void recTime_2()
{
baseTime = currentTime;
Serial.println("Light");
digitalWrite(3, HIGH);
if(currentTime - baseTime >= timer2)
{
digitalWrite(3, LOW);
Serial.println("Darkness");
}
}