Hi, im using nrf module in my project for smart home, everything goes okay if i just testing nrf modules with some basic code, but if i type it in the one specific function in the code, module send data just for one time and if i want send it one more time it isnt able to send data again. Code seems very scary but if someone would be so nice to check it and think about it i would be so thankful. (its code for transmitter)
if you have questions about functions send me a message please
If you post your code, here, like shown in the "how to use this forum-please read" (see #7) more people will be able to see it. Members to not like to have to download code.
here is the code, and i know how the nrf works, and know library functions very well, but in this specific function i just cant send data one more time and i dont know why.
/* project yolanda *voice recognizer* of multiple slaves over the house. using nrf24L01
that provides very good range for the communication (over 20 meters in building) we can easily
managing multiple slaves for instance (room light controlling) only with the voice,
for the voice recognition we are using SPCE (geetech speech recognition module) that after every
recognized command sends message (byte) */
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int svetlo;
float volts=0;
int i=3,led=3,flag=0,j=0,flag2=0,number=1;
long pTime = 0;
int chargerPin=4;
long interval = 5000, interval2=200,interval3=150;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const float referenceVolts = 218.313253; // the default reference on a 5-volt board
const int batteryPin = A1; // battery is connected to analog pin 0
const uint64_t pipes[3] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL, 0xF0F0F0F0D3LL }; // two adresses for two devices
byte command;
RF24 radio(9,10);
void setup()
{
Serial.begin(9600);
pinMode(chargerPin,INPUT);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
radio.begin();
radio.setRetries(15,15);
radio.openReadingPipe(1,pipes[1]);
radio.startListening();
radio.printDetails();
radio.openWritingPipe(pipes[0]);
radio.openWritingPipe(pipes[2]);
radio.openReadingPipe(1,pipes[1]);
radio.stopListening();
Serial.write(0xAA);
Serial.write(0x37);
delay(1000);
Serial.write(0xAA);
Serial.write(0x21);
for(j; j<2; j++){
for(i; i<=6; i++){
if(i!=4){
digitalWrite(i, HIGH);
delay(100);
}
}
delay(100);
for(i; i>=3; i--){
if(i!=4){
digitalWrite(i, LOW);
delay(100);
}
}
delay(100);
}
}
void charging()
{
Serial.print("nabija\n");
int val = analogRead(batteryPin); // read the value from the sensor
volts = val / referenceVolts; // calculate the ratio
unsigned long Time =millis();
pTime=Time;
while(volts<=4.3)
{
val = analogRead(batteryPin); // read the value from the sensor
volts = val / referenceVolts; // calculate the ratio
if(Serial.available()){
byte command = Serial.read();
if(command==0x11){
Serial.print(command);
yolanda(command);
}
}
if(Time-pTime>=interval3)
{
i=3;
digitalWrite(i, HIGH);
}
if(Time-pTime>=interval3+150)
{
i=5;
digitalWrite(i, HIGH);
}
if(Time-pTime>=interval3+300)
{
i=6;
digitalWrite(i, HIGH);
}
if(Time-pTime>=interval3+500)
{
for(i; i>=3; i--){
if(i!=4){
digitalWrite(i, LOW);
}
pTime=Time;
}
}
if((digitalRead(chargerPin))==LOW || volts>=4.3)
{
for(i; i>=3; i--){
if(i!=4){
digitalWrite(i, LOW);
}
}
i=3;
break;
}
Time =millis();
}
}
void batterycheck()
{
int val = analogRead(batteryPin); // read the value from the sensor
volts = val / referenceVolts; // calculate the ratio
unsigned long Time =millis();
pTime=Time;
Serial.println(volts, DEC);
while(volts<=1.55)
{
command=0;
val = analogRead(batteryPin); // read the value from the sensor
volts = val / referenceVolts; // calculate the ratio
if (Serial.available()){
if((command=Serial.read())==0x11)
Serial.print(command, HEX);
yolanda(command);
}
if(Time-pTime>=interval2 && flag2==0)
{
for(i; i<=6; i++){
if(i!=4){
digitalWrite(i, HIGH);
}
pTime=Time;
flag2=1;
}
}
if(Time-pTime>=interval2 && flag2==1)
{
for(i; i>=3; i--){
if(i!=4){
digitalWrite(i, LOW);
}
pTime=Time;
flag2=0;
}
}
if((digitalRead(chargerPin))==HIGH)
{
charging();
}
Time =millis();
}
if(flag2==1)
{
for(i; i>=3; i--){
if(i!=4){
digitalWrite(i, LOW);
}
flag2=0;
}
}
}
void light()
{
j=0;
Serial.print("svetlo\n");
for(j; j<2; j++){
for(i; i<=6; i++){
if(i!=4){ // now has to be Y logo light off (without using fade)
digitalWrite(i, HIGH);
}
}
delay(200);
for(i; i>=3; i--){
if(i!=4){
digitalWrite(i, LOW);
}
}
delay(200);
}
i=3;
brightness=0;
flag=0;
delay(500);
}
void obyvacka(int command)
{
radio.openWritingPipe(pipes[0]);
radio.write( &command, sizeof(command) );
light();
}
void izba(byte command)
{
Serial.print("funkcia\n");
Serial.println(command, HEX); //just testing what value has the variable after another command have been said
radio.openWritingPipe(pipes[0]);
radio.write( &command, sizeof(command) );
delay(500);
light();
}
void garaz(int command)
{
radio.openWritingPipe(pipes[2]);
radio.write( &command, sizeof(command) );
}
void yolanda(byte command)
{
Serial.print("yolanda\n");
led=3;
while(brightness<255)
{
for(led; led<=6; led++)
if(led!=4){
analogWrite(led, brightness);
} // now has to be Y logo light on (using fade)
led=3;
brightness+=fadeAmount;
delay(40);
}
unsigned long Time =millis(); // Time initializing
pTime = Time;
while(Time-pTime<=interval) // serial reading just for the chosen interval
{
if (Serial.available()){
command = Serial.read();
Serial.print("command\n");
flag=1;
break;
}
Time =millis();
}
if(flag==1)
{
if(command>=0x12 && command <=0x15)
{
izba(command);
}
if(command>=0x16 && command <=0x20)
{
obyvacka(command);
}
if(command>=0x20 && command <=0x24)
{
garaz(command);
}
}
else
{
led =3;
while(brightness>=0)
{
for(led; led<=6; led++)
if(led!=4){
analogWrite(led, brightness);
} // now has to be Y logo light off (using fade)
led=3;
brightness-=fadeAmount;
delay(40);
}
}
}
void loop(void)
{
if(Serial.available()){
command = Serial.read();
if(command==0x11){
Serial.print(command);
yolanda(command);
}
}
batterycheck();
}
The radio cannot read (listen) and write simultaneously, and since you do not read from the pipe there is no need for opening the reading pipe at all. Change the code above to:
and I don't think the second part cannot work if the first part is satisfied
Wireless problems are always hard to debug so it is much better to get the wireless stuff working first with a very simple program and then start adding the other stuff. Just send dummy values to start with.
its hard to explain all the code, because i use also voice rocognition module, so first arduino gets some data from voice recognizer, for instance i said my room and the command for that is 0x12 hex, than know which room and which module has to call, so if it is 0x12 nrf module has to call other nrf module in my room and therefore it calls function room that opens pipe 1 to call module in my room.. and i tried just send some data from void loop, just numbers like 1 to reciever and everything was okay, but if i want to send it from function void izba(byte command), reciever gets data only once and than nothing and i have to reupload code for every arduino and than it goes one time again but than nothing.
pavolk:
its hard to explain all the code, because i use also voice rocognition module,
When you have a problem with a program simplify it.
For the moment forget about voice modules and anything else unconnected with the wireless.
Write a short program that changes the value of a variable once per second between the values that will be produced by the voice module.
And in that program use that value to send a message (the value of millis() is useful because it changes in a predictable way) to each of your slaves in turn.
If that short program does not work it will at least make it much easier to help you.
but when i use code with waiting for voice recognizer, he sends data, i see it in serial that really data is 0x11 but radio cant send it in this example
That is not typical of what you want to do. Extend that example so it can send to your 3 slaves (or is it 2?) without the complexity of any voice recognition.
And if you need more help please post the complete program - the problem is often in the other part.
and here is the code, its very simple, just wait for what data come from serial (this data send voice recognizer) and than send it, but this doesnt work. Without waiting for serial it works, like the example in previous post
pls trust me i checked everything with serialprint, i know how the code works but i dont know why in this example transmitter doesnt send data, thats all
just trust me what im saying, i did everything you advised to me, but there is still problem with this part
void loop(void)
{
if(Serial.available()){
command = Serial.read();
Serial.println(command, HEX); !! here i know that is 0x11
if(command==0x11)
{
radio.openWritingPipe(pipes[0]);
radio.write( &command, sizeof(command) );
Serial.println("if condition works"); !! here i know that transmitter has to send 0x11
}
}
}