I have two arduinos I'm trying to get to communicate with one another. And it works... Sort of.
The data speed is so slow, it's unusable.
The intended behavior is:
Turn potentiometer on one arduino, stepper on the other one rotates at the same time.
However, instead, the stepper moves to the potentiometer position maybe once every 45 seconds or so.
Now, the data is correct, when it's recieved at all.
Code for the transmitter:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <printf.h>
const int pinCE = 7;
const int pinCSN = 8;
double Array[32];
int counter = 0;
uint16_t md1;
uint16_t md2;
uint8_t mode = 1;
RF24 wirelessSPI(pinCE, pinCSN);
byte pAddress[][6] = {"1Node", "2Node"};
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
int flag = 0;
void setup()
{
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(9, INPUT);
Serial.begin(9600);
printf_begin();
wirelessSPI.begin();
wirelessSPI.setAutoAck(true);
wirelessSPI.enableAckPayload();
wirelessSPI.setPALevel(RF24_PA_LOW);
wirelessSPI.setRetries(5, 15);
wirelessSPI.openWritingPipe(pipes[0]);
wirelessSPI.openReadingPipe(1, pipes[1]);
wirelessSPI.stopListening();
wirelessSPI.printDetails();
}
void loop()
{
Array[0] = analogRead(A0);
if (!wirelessSPI.write( &Array[0], sizeof(Array))) {
Serial.println("Error 0x04 - Error sending data");
Serial.println();
}
else {
Serial.println("Send successful.");
Serial.println();
}
delayMicroseconds(10000);
}
Code for the receiver:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <printf.h>
const int pinCE = 4;
const int pinCSN = 5;
double gotArray[32];
const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};
RF24 wirelessSPI(pinCE, pinCSN);
byte pAddress[][6] = {"2Node", "1Node"};
unsigned long lastMillis;
int steps = 0;
int currentSteps = 0;
double stepsPerDeg = 1.8;
double microSteps = 0.25;
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
double val = 0;
int deg2steps(double deg) {
return (deg / stepsPerDeg) * (1 / microSteps);
}
void setup() {
Serial.begin(9600);
wirelessSPI.begin();
printf_begin();
wirelessSPI.setAutoAck(1);
wirelessSPI.enableAckPayload();
wirelessSPI.setPALevel(RF24_PA_LOW);
wirelessSPI.setRetries(5, 15);
wirelessSPI.openWritingPipe(pipes[1]);
wirelessSPI.openReadingPipe(1,pipes[0]);
wirelessSPI.startListening();
wirelessSPI.printDetails();
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
lastMillis = millis();
}
double recvdval;
void loop() {
while(wirelessSPI.available()) {
wirelessSPI.read( &gotArray, sizeof(gotArray) );
Serial.println(gotArray[0]);
recvdval = gotArray[0];//((double) gotArray[0] * 0.94) / (1.00-0.94);
}
if (val - recvdval < -5 or val - recvdval > 5) {
val = recvdval;
}
steps = deg2steps(map(val, 0, 1023, 0, 360));
if (currentSteps > steps) {
currentSteps -= 1;
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delayMicroseconds(20);
digitalWrite(3, LOW);
delayMicroseconds(800 * microSteps);
}
if (currentSteps < steps) {
currentSteps += 1;
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delayMicroseconds(20);
digitalWrite(3, LOW);
delayMicroseconds(800 * microSteps);
}
}
Serial monitor of the receiver, after letting it run for about 10 minutes and occasionally turning the potentiometer:
SPI Speedz = 10 Mhz
STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xf0f0f0f0d2 0xf0f0f0f0e1
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xf0f0f0f0d2
RX_PW_P0-6 = 0x20 0x20 0x20 0x20 0x20 0x20
EN_AA = 0x3f
EN_RXADDR = 0x02
RF_CH = 0x4c
RF_SETUP = 0x03
CONFIG = 0x0f
DYNPD/FEATURE = 0x03 0x06
Data Rate = 1 MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_LOW
ARC = 0
401.00
401.00
402.00
712.00
711.00
711.00
710.00
444.00
444.00
444.00
445.00
446.00
445.00
445.00
445.00
445.00
26.00
0.00
Serial monitor of the transmitter, under the same conditions:
Error 0x04 - Error sending data
Error 0x04 - Error sending data
Error 0x04 - Error sending data
Send successful.
Error 0x04 - Error sending data
Error 0x04 - Error sending data
Error 0x04 - Error sending data
... ("Error 0x04 - Error sending data" X over 100) ...
Error 0x04 - Error sending data
Error 0x04 - Error sending data
Error 0x04 - Error sending data
Send successful.
Error 0x04 - Error sending data
Error 0x04 - Error sending data
Error 0x04 - Error sending data
I have no idea what's causing this problem, as the radios are communicating, just doing so very slowly.