173 lines
3.4 KiB
C++
173 lines
3.4 KiB
C++
|
|
/*-----( Import needed libraries )-----*/
|
|
#include <SPI.h>
|
|
//#include <nRF24L01.h>
|
|
//#include <RF24.h>
|
|
#include "nRF24L01.h"
|
|
#include "RF24.h"
|
|
#include "printf.h"
|
|
|
|
/*-----( Declare Constants and Pin Numbers )-----*/
|
|
#define CE_PIN 2
|
|
#define CSN_PIN 0
|
|
|
|
#define red 5
|
|
#define green 10
|
|
#define blue 9
|
|
|
|
#define white 6 // arduino shield
|
|
|
|
#define DEBUG 1
|
|
#define TEST 1
|
|
|
|
|
|
// NOTE: the "LL" at the end of the constant is "LongLong" type
|
|
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
|
|
|
|
|
|
/*-----( Declare objects )-----*/
|
|
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
|
|
/*-----( Declare Variables )-----*/
|
|
uint8_t rgb[4]; // 2 element array holding Joystick readings
|
|
|
|
|
|
|
|
float average(int anaPin) {
|
|
int lecturas = 100;
|
|
long total = 0;
|
|
float average = 0;
|
|
for(int i=0; i<lecturas; i++)
|
|
{
|
|
//delay(1);
|
|
total = total + analogRead(anaPin);
|
|
}
|
|
average = (float)total / lecturas;
|
|
return(average);
|
|
}
|
|
|
|
|
|
|
|
void setup() /****** SETUP: RUNS ONCE ******/
|
|
{
|
|
|
|
if(DEBUG)
|
|
Serial.begin(57600);
|
|
|
|
analogWrite(red, 0);
|
|
analogWrite(green, 0);
|
|
analogWrite(blue, 0);
|
|
|
|
delay(1000);
|
|
|
|
if(DEBUG)
|
|
Serial.println("Nrf24L01 Receiver Starting");
|
|
|
|
radio.begin();
|
|
printf_begin();
|
|
radio.setAutoAck(0);
|
|
radio.setPayloadSize(4);
|
|
radio.openReadingPipe(1,pipe);
|
|
radio.startListening();
|
|
radio.printDetails();
|
|
|
|
delay(2000);
|
|
|
|
|
|
}//--(end setup )---
|
|
|
|
|
|
void loop() /****** LOOP: RUNS CONSTANTLY ******/
|
|
{
|
|
|
|
if(TEST)
|
|
{
|
|
float sensorValueDefault = average(A7);
|
|
analogReference(INTERNAL);
|
|
delay(10);
|
|
float sensorValueInternal = average(A7);
|
|
analogReference(DEFAULT);
|
|
delay(10);
|
|
|
|
for (int i=0; i<256; i++) {
|
|
analogWrite(red, i);
|
|
delay(10);
|
|
}
|
|
for (int i=254; i>=0; i--) {
|
|
analogWrite(red, i);
|
|
delay(10);
|
|
}
|
|
for (int i=0; i<256; i++) {
|
|
analogWrite(green, i);
|
|
delay(10);
|
|
}
|
|
for (int i=254; i>=0; i--) {
|
|
analogWrite(green, i);
|
|
delay(10);
|
|
}
|
|
for (int i=0; i<256; i++) {
|
|
analogWrite(blue, i);
|
|
delay(10);
|
|
}
|
|
for (int i=254; i>=0; i--) {
|
|
analogWrite(blue, i);
|
|
delay(10);
|
|
}
|
|
// print out the value you read:
|
|
|
|
float Vcc = (sensorValueInternal/sensorValueDefault)*2560;
|
|
Serial.print("Vcc: ");
|
|
Serial.print(Vcc);
|
|
Serial.print(" Internal: ");
|
|
Serial.print(sensorValueInternal*2*(2560/1023.));
|
|
Serial.print(" Default: ");
|
|
Serial.println(sensorValueDefault*2*(3300/1023.));
|
|
delay(1); // delay in between reads for stability
|
|
}
|
|
else
|
|
{
|
|
if ( radio.available() )
|
|
{
|
|
if(DEBUG)
|
|
Serial.println("radio available");
|
|
// Read the data payload until we've received everything
|
|
bool done = false;
|
|
while (!done)
|
|
{
|
|
// Fetch the data payload
|
|
done = radio.read( rgb, sizeof(rgb) );
|
|
//analogWrite(6, rgb[1]); //arduino shield
|
|
analogWrite(red, rgb[1]);
|
|
analogWrite(green, 0);
|
|
analogWrite(blue, 0);
|
|
|
|
if(DEBUG)
|
|
{
|
|
Serial.print("id = ");
|
|
Serial.print(rgb[0]);
|
|
Serial.print("\t");
|
|
Serial.print(rgb[1]);
|
|
Serial.print("\t");
|
|
Serial.print(rgb[2]);
|
|
Serial.print("\t");
|
|
Serial.println(rgb[3]);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
//else
|
|
//{
|
|
//Serial.println("No radio available");
|
|
//}
|
|
|
|
}//--(end main loop )---
|
|
|
|
/*-----( Declare User-written Functions )-----*/
|
|
|
|
//NONE
|
|
//*********( THE END )***********
|
|
|
|
|
|
|
|
|