Hello!
I am sorry if this seems like a silly question but I don’t really understand what I’m doing (I’m more of a hardware guy and don’t know that much about programming).
This is my project, I’m trying to build a wireless controller for my PC (mainly to learn about multiplexing and such).
What I don’t really know is how I should approach to send my data through a NRF24L01+. My idea was that I just put all variables and put them into an array and then send all button states and joystick states through integers. This means that for now the array is 22 integer long (all with values below 255).
I read that the NRF24L01+ can only handle 32 bytes of data and wondering if that’s my problem?
The problem I’m facing is that the nano gets stuck at radio.write(sendData, sizeof(22)); I believe since when I troubleshoot I get to that point and not further (hence the random Serial.println(“x”); numbers).
Again, I am not a programmer at no means and my code is messy but I just want to try and understand why I’m failing. Sorry for the true eye-sore.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int pin_Out_S0 = 5;
int pin_Out_S1 = 4;
int pin_Out_S2 = 3;
int pin_In_Mux1 = A6;
int Mux1_State[8] = {0};
int latchPin = 6;
int dataPin = 10;
int clockPin = 9;
byte switchVar1 = 72; //01001000
byte switchVar2 = 159; //10011111
int sendData[22] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0};
int HY;
int VX;
int VY;
int HX;
int R1;
int R2;
RF24 radio(8, 7); // CE, CSN
const byte address[6] = "00001";
void setup() {
pinMode(pin_Out_S0, OUTPUT);
pinMode(pin_Out_S1, OUTPUT);
pinMode(pin_Out_S2, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
//pinMode(pin_In_Mux1, INPUT);
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
Serial.println("0");
updateMux1();
//Pulse the latch pin:
//set it to 1 to collect parallel data
digitalWrite(latchPin,1);
//set it to 1 to collect parallel data, wait
delayMicroseconds(20);
//set it to 0 to transmit data serially
digitalWrite(latchPin,0);
//while the shift register is in serial mode
//collect each shift register into a byte
//the register attached to the chip comes in first
switchVar1 = shiftIn(dataPin, clockPin);
switchVar2 = shiftIn(dataPin, clockPin);
Serial.println("1");
for (int n=0; n<=7; n++)
{
//so, when n is 3, it compares the bits
//in switchVar1 and the binary number 00001000
//which will only return true if there is a
//1 in that bit (ie that pin) from the shift
//register.
if (switchVar1 & (1 << n) ){
//print the value of the array location
sendData[n] = 1;
}
}
Serial.println("2");
for (int n=0; n<=7; n++)
{
//so, when n is 3, it compares the bits
//in switchVar1 and the binary number 00001000
//which will only return true if there is a
//1 in that bit (ie that pin) from the shift
//register.
if (switchVar2 & (1 << n) ){
//print the value of the array location
int a = 8;
int y = n + a;
sendData[y] = 1;
}
}
Serial.println("3");
for(int g = 0; g <= 21; g++)
{
Serial.println(sendData[g]);
}
Serial.println("4");
radio.write(sendData, sizeof(22));
delay(20);
Serial.println("5");
int sendData[22] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0};
}
void updateMux1 () {
for (int i = 0; i < 8; i++){
digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
Mux1_State[i] = analogRead(pin_In_Mux1);
if(i == 0) {sendData[16] = map(Mux1_State[i], 0, 1023, 0, 255);}
if(i == 1) {sendData[17] = map(Mux1_State[i], 0, 1023, 0, 255);}
if(i == 4) {sendData[18] = map(Mux1_State[i], 0, 1023, 0, 255);}
if(i == 6) {sendData[19] = map(Mux1_State[i], 0, 1023, 0, 255);}
if(i == 5) {sendData[20] = map(Mux1_State[i], 0, 1023, 0, 255);}
if(i == 7) {sendData[21] = map(Mux1_State[i], 0, 1023, 0, 255);}
}
}
byte shiftIn(int myDataPin, int myClockPin) {
int i;
int temp = 0;
int pinState;
byte myDataIn = 0;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, INPUT);
//we will be holding the clock pin high 8 times (0,..,7) at the
//end of each time through the for loop
//at the begining of each loop when we set the clock low, it will
//be doing the necessary low to high drop to cause the shift
//register's DataPin to change state based on the value
//of the next bit in its serial information flow.
//The register transmits the information about the pins from pin 7 to pin 0
//so that is why our function counts down
for (i=7; i>=0; i--)
{
digitalWrite(myClockPin, 0);
delayMicroseconds(2);
temp = digitalRead(myDataPin);
if (temp) {
pinState = 1;
//set the bit to 0 no matter what
myDataIn = myDataIn | (1 << i);
}
else {
//turn it off -- only necessary for debuging
//print statement since myDataIn starts as 0
pinState = 0;
}
//Debuging print statements
//Serial.print(pinState);
//Serial.print(" ");
//Serial.println (dataIn, BIN);
digitalWrite(myClockPin, 1);
}
//debuging print statements whitespace
//Serial.println();
//Serial.println(myDataIn, BIN);
return myDataIn;
}
Before anyone makes a comment about it, I have tried googling, and the answers I find (or well, might be answers) are too advanced for me to understand and adapt to my code. I’m way out of my league of this but I truly want to learn and need help.
Thank you all in advance!