Lab_interaccio/2014/Globos-RF24/globo/sysex_RX/sysex_RX.ino

213 lines
4.4 KiB
Arduino
Raw Normal View History

2025-02-25 21:29:42 +01:00
/*-----( 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 11
#define red 5
#define green 10
#define blue 9
#define white 6 // arduino shield
#define DEBUG 1
#define TEST 0
byte idpins[8]={
0, 4, 12, 8, 7, A1, A0, 13};
int i = 0;
int row, col;
int colNum = 5; // X -> Maximum of 10 cols. Maximun packet size 32 bytes
int rowNum = 13; // Y
float plusLight = 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[32]; // 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);
}
byte id() {
byte id = 0;
for (byte i=0; i<8; i++) bitWrite(id, 7-i, digitalRead(idpins[i]));
return id;
}
void setup() /****** SETUP: RUNS ONCE ******/
{
if(DEBUG)
Serial.begin(115200);
for (byte i=0; i<8; i++) pinMode(idpins[i], INPUT);
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 0);
row = (id()-1)/colNum ; //row
col = (id()-1)%colNum ; //col
delay(5000);
radio.begin();
printf_begin();
//radio.setChannel(100);
radio.setDataRate( RF24_250KBPS );
radio.setPALevel( RF24_PA_MAX );
radio.setAutoAck(0);
radio.setPayloadSize(colNum*3 + 2);
radio.openReadingPipe(1,pipe);
radio.startListening();
radio.printDetails();
if(DEBUG)
{
Serial.println();
Serial.println("Nrf24L01 Receiver Starting");
Serial.print("Address: ");
Serial.print(id());
Serial.print("\tRow: ");
Serial.print(row);
Serial.print("\tCol: ");
Serial.println(col);
}
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() )
{
bool done = false;
while (!done)
{
// Fetch the data payload
done = radio.read( rgb, sizeof(rgb) );
if(rgb[0] == 1) //rgb[0] es el CMD, CMD = 1 => MODO STREAM
{
if(rgb[1] == row) // row from 0 to 9
{
analogWrite(red, (byte)(rgb[2+col*3]*plusLight)); // Red
analogWrite(green, (byte)(rgb[3+col*3]*plusLight)); // Green
analogWrite(blue, (byte)(rgb[4+col*3]*plusLight)); // Blue
}
}
else if(rgb[0] == 2) //CMD = 2 => plusLight CMD (apretar PWM de 127 hasta 250) -> CUIDADO!!!
{
if(rgb[1] > 250)
rgb[1] = 250;
plusLight = rgb[1] / 64. ;
//Serial.println(plusLight);
}
if(DEBUG)
{
i = 0;
for(i=0 ; i<colNum*3+2 ; i++)
{
Serial.print(rgb[i]);
Serial.print(",");
}
Serial.println();
}
}
}
}
//else
//{
//Serial.println("No radio available");
//}
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
//NONE
//*********( THE END )***********