Hi everyone.
I'm currently working on a small project for my room. If i push a button (later CapSense) i want to increase / decrease a value(ledBrightness) and send the Value to the room-lamp
Here's a simple schematic of my hardware:
Master: Sensor/Pixels (installed on the bed): Room-Lamp:
[Iboard+NRF24l01]-->[ATMEGA328P+NRF24l01+CapSensor+WS2801-Pixels]--->[ATMEGA328P+NRF24l01+Mosfet->LED]
The ATMEGA's are currently placed on a breadboard with a external 16Mhz-clock crystal, later the will get on a PCB.
I'm still a beginner, so i have to be honest that i don't really understand what exactly happens in the line:
radio.write( &ledBrightness, sizeof(uint8_t) );
...especially all the things in the background...
My question is about the communication between the NRF24l01's. The code below works, but he does not work properly. Sometimes Data (ledBrightness) get's transferred without any problems, but sometimes the receiver stops receiving data. Even when i release the switch and wait 20s and press it again, the receiver seems to be freezed. I noticed that the Serial-ouput from the receiver gets slower as bigger the ledBrightness-value is. Is the sendig-rate too high? Or did i messed something with the datatypes? Can anyone point me in the right direction? Thank you
I had to remove some code because the max. of 9500 characters, so it wont compile.
Code of the Sensor/Pixels (installed on the bed)
#include <CapacitiveSensor.h>
#include <CapacitiveSensor.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#include "WS2801.h"
uint8_t ledBrightness = 0; // Value that holds the brightness of the LED
uint8_t factor= 30; // Factor for the sample-resolution of cap-sense
uint8_t red,blue,green; // RGB values of the LED-Strip
uint8_t pipe_num; // Value that holds the Pipenumber of the incoming NRF-Data
uint8_t dataPin = 2; // SPI DataPin for the WS2801
uint8_t clockPin = 3; // SPI ClockPin for the WS2801
uint8_t StripDelay = 50; // Delay for the LED-Pixels
boolean increase = true; // When variable is true, ledBrightness gets increased, otherwise decreased
boolean exitWhileTouch = false; // Variable to exit WhileTouch loop
char inCharNRF; // Char used receiving the NRF-Data
// Set up WS2801 LED-Strip
WS2801 strip = WS2801(9, dataPin, clockPin);
// Configure RF Radio pins
RF24 radio(9,10);
// Radio pipe addresses for the nodes to communicate
const uint64_t pipes[5] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL, 0xF0F0F0F0D3LL, 0xF0F0F0F0D4LL, 0xF0F0F0F0D5LL };
// Configure capacitive sensor-input
CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6);
//#define SERIAL_DEBUG_BOOL 1 // serial debug on for all boolean variables
#define SERIAL_DEBUG_INT 1 // serial debug on for all int variables
//#define SERIAL_DEBUG_OTHER 1 // serial debug on for other serial prints
void setup()
{
pinMode(8, INPUT); //button pin for increase / decrease ledBrightness
Serial.begin(115200);
strip.begin();
radio.begin();
radio.setRetries(15,15);
radio.setPayloadSize(2);
readingMode();
}
void loop ()
{
// if (long total = cs_4_6.capacitiveSensor(factor)> 200)
// !!!currently working with a simple digital-read to avoid problems with the capsense sensor
if (digitalRead(8) == HIGH)
{
writingMode(); // Set the NRF24L01 in the writing-mode
sensorRead(); // Read the Capsense
readingMode(); // Set the NRF24L01 in the reading-mode
}
/*************************************Waiting for Data from NRF24l01***************************************************/
if (radio.available(&pipe_num) )
{
#ifdef SERIAL_DEBUG_OTHER
Serial.println("Received data from master");
#endif
...removed code here...should not be importand..
} // end of: if (radio.available(&pipe_num))
}
} // end of void loop()
/************************************* detection of the Capsensor ***********************************************/
void sensorRead()
{
#ifdef SERIAL_DEBUG_OTHER
Serial.println("I'm now in void sensorRead");
#endif
// while (long total = cs_4_6.capacitiveSensor(factor) > 300 and exitWhileTouch == false)
// !!!currently working with a simple digital-read to avoid problems with the capsense sensor
while (digitalRead(8) == HIGH and exitWhileTouch == false)
{
if (increase == true)
{
ledBrightness += 5;
}
else
{
ledBrightness -= 5;
}
#ifdef SERIAL_DEBUG_INT
Serial.print("ledBrightness: ");
Serial.println(ledBrightness);
#endif
if (ledBrightness <= 0) // ledBrightness is equal / less than 0 --> exit and change direction
{
exitWhileTouch = true;
delay(1000); // Delay do give time to take your hand away from the sensor
#ifdef SERIAL_DEBUG_OTHER
Serial.println("Exit while touch true because brightness == 0");
#endif
}
else if (ledBrightness >= 255) // ledBrightness is equal / greater than 255 --> exit and change direction
{
exitWhileTouch = true;
delay(1000); // Delay do give time to take your hand away from the sensor
#ifdef SERIAL_DEBUG_OTHER
Serial.println("Exit while touch true because brightness == 255");
#endif
}
radio.write( &ledBrightness, sizeof(uint8_t) );
delay(100); // Delay for the decrease / increase speed
} // end of: while loop
exitWhileTouch = true; // sensor is inactive now or ledBrightness was 255 or zero
if (exitWhileTouch == true) // change the direction whenever we run through the while-loop
{
increase == true ? increase = false : increase = true;
exitWhileTouch = false;
}
} // end of: void sensorRead()
/***************************************** Modes for the NRF24l01 ************************************************/
void readingMode()
{
radio.openReadingPipe(2,pipes[1]);
radio.openReadingPipe(3,pipes[2]);
radio.openReadingPipe(4,pipes[3]);
radio.openReadingPipe(5,pipes[4]);
radio.startListening();
#ifdef SERIAL_DEBUG_OTHER
Serial.println("NRF is now in reading-mode");
#endif
}
void writingMode()
{
radio.stopListening();
radio.openWritingPipe(pipes[0]);
#ifdef SERIAL_DEBUG_OTHER
Serial.println("NRF is now in writing-mode");
#endif
}
Code of Room-Lamp
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#include "WS2801.h"
#include <SPI.h>
uint8_t optocoupler = 0; // Value that holds the analogvalue of the optokoppler
uint8_t optocouplerPin = 0; // Number of the Pin where the optocoupler is attached to
uint8_t ledBrightness; // Value that holds the brightness of the LED
uint8_t ledPin = 3; // Number of the Pin where the LED is attached to
boolean b_optocoupler = false; // Variable that holds the state of the optocoupler
boolean b_lastState = true; // Variable that holds the last state of the optocoupler
// Configure RF Radio pins
RF24 radio(9,10);
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[5] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL, 0xF0F0F0F0D3LL, 0xF0F0F0F0D4LL, 0xF0F0F0F0D5LL };
//#define SERIAL_DEBUG_BOOL 1 // serial debug on for all boolean variables
#define SERIAL_DEBUG_INT 1 // serial debug on for all int variables
//#define SERIAL_DEBUG_OTHER 1 // serial debug on for other serial prints
void setup()
{
Serial.begin(115200);
pinMode(optocouplerPin, INPUT); // Analoginput used for the optocoupler
pinMode(ledPin, OUTPUT); // PWM-Ouput used for the LED
radio.begin();
radio.setRetries(15,15);
radio.setPayloadSize(2);
radio.openReadingPipe(1,pipes[0]);
radio.startListening();
}
void loop ()
{
if (radio.available())
{
// Dump the payloads until we've gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( &ledBrightness, sizeof(unsigned long) );
}
#ifdef SERIAL_DEBUG_INT
Serial.print("ledBrightness: ");
Serial.println(ledBrightness);
#endif
analogWrite(ledPin,ledBrightness);
} // end of: if (radio.available())
/************************************* State change detection ***************************************************/
analogRead(optocoupler) >= 500 ? b_optocoupler = true : b_optocoupler = false;
#ifdef SERIAL_DEBUG_BOOL 1
Serial.print("optocoupler-state: ");
Serial.println(b_optocoupler);
delay(500);
#endif
...removed code here...should not be importand..
b_lastState = b_optocoupler ; // store the current state
} // end of void loop