Thankyou both for the help.
How is your project going zhomeslice?
I added the changes you suggested zhomeslice and it seems to stop the interference from the radio.
However, when I reduce txIntervalMillis below about 300, the Motor start is very delayed and if I reduce it to 100, the motor will not start at all. 400 is about the minimum usable interval.
/*
Arduino Nano
Pin allocation:
D1
D2 = speed sensor (interrupt 0)
D3 = Ping trigger
D4 = Ping Echo
D5 = ESC relay
D6 = ESC PWM out
D7 = Radio CE
D8 = Radio CSN
D9 =
D10 =
D11 = Radio
D12 = Radio
D13 = Radio
A0 = Current sensor
A1 =
A2 =
A3 =
A4 =
A5 =
A6 =
A7 =
*/
//*********Height Sensor Stuff*********
#include <NewPing.h>
#define TRIGGER_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int dist;
//*****Motor Controller Stuff******
#include <PID_v2.h>
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 0.6, 6.2, .01, DIRECT);
#define PWMpin 6
volatile unsigned long timeX = 1;
int PulsesPerRevolution = 36;
int MaxRPM = 400;
volatile int Counts = 1;
double PulsesPerMinute;
volatile unsigned long LastTime;
volatile int PulseCtr;
unsigned long Counter;
int startRamp;
unsigned long Time;
int rpm;
int printRpm; // Averaged over several readings to smooth it out.
volatile int rpmArray[5] = {0, 0, 0, 0, 0}; // For printRpm
//********Radio Stuff*******
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
// NOTE: the "LL" at the end of the constant is "LongLong" type
// These are the IDs of each of the slaves
const uint64_t slaveID[2] = {0xE8E8F0F0E1LL, 0xE8E8F0F0E2LL} ;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int radioTxArray[2];
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 100;
int txVal = 0;
int radioRxArray[6];
//0 = motor on/off, 1 = setPoint, 2 = , 3 = Height, 4 = Cal, 5 = Dist
byte radioRxArrayLen = 12; // NB this 4 is the number of bytes in the 2 ints that will be recieved
bool isStarted = false;
bool isCal = false;
bool isDist = false;
bool go = false;
bool Skip = false;
void setup() {
// note that 1666666.67 = (60 seonds * 1000000 microseconds)microseconds in a minute / (36 / 9) pulses in 1 revolution
PulsesPerMinute = (60 * 1000000) / (PulsesPerRevolution / Counts);
pinMode(2, INPUT_PULLUP);
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("PID controlled Seeder Master R0");
delay(1000);
//Digital Pin 2 Set As An Interrupt for tacho.
attachInterrupt(0, sensorInterrupt, FALLING);
startRamp = 10;//map(PulsesPerRevolution , 1, MaxRPM, MaxRPM, 2);
myPID.SetSampleTime(1);
myPID.SetOutputLimits(40, (int) 255);
PulseCtr = 0;
myPID.SetMode(AUTOMATIC);
analogWrite(PWMpin, 60);
myPID.Compute();
delay(11);
myPID.Compute();
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(3, 5); // delay, count
rf24_pa_dbm_e{RF24_PA_MAX};
}
void loop() {
// put your main code here, to run repeatedly:
exchangeData();
getSetPoint();
getStartStop();
getHeight();
readMotorCurrent();
switchOnOff();
readRpm();
debug();
static unsigned long SpamTimer;
}
void exchangeData()
{
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
radio.openWritingPipe(slaveID[0]); // calls the first slave
// there could be a FOR loop to call several slaves in turn
bool rslt;
rslt = radio.write( radioTxArray, sizeof(radioTxArray) );
if ( radio.isAckPayloadAvailable() ) {
radio.read(radioRxArray, radioRxArrayLen);
Skip = true;
}
prevMillis = millis();
}
}
void getSetPoint()
{
if (isStarted == true) {
Setpoint = radioRxArray[1];
// Setpoint = 30;
}
else
{
Setpoint = 0;
radioTxArray[0] = 0;
}
}
void getStartStop()
{
if (radioRxArray[0] == 1) //start/stop button on.
{
if (radioRxArray[3] == 0) //height switch off.
{
isStarted = true;
}
if (dist <= radioRxArray[5] && radioRxArray[3] == 1) //if ping dist is less than dist set point and height switch is on.
{
isStarted = true;
}
}
else
{
isStarted = false;
}
}
void getHeight()
{
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
dist = (uS / US_ROUNDTRIP_CM); //raw distance in cm.
}
void readMotorCurrent()
{
}
void switchOnOff()
{
if (isStarted == true)
{
digitalWrite(5, HIGH);
}
else
{
digitalWrite(5, LOW);
}
}
void sensorInterrupt()
{
static int Ctr;
unsigned long Time;
Ctr++;
if (Ctr >= Counts) { // 36 / 4 = 9 so we are taking an average of 9 readings to use in our calculations
Time = micros();
timeX += (Time - LastTime); // this time is accumulative ovrer those 9 readings
LastTime = Time;
PulseCtr ++;
Ctr = 0;
}
}
void readRpm()
{
cli (); // clear interrupts flag
Time = timeX; // Make a copy so if an interrupt occurs timeX can be altered and not affect the results.
timeX = 0;
sei (); // set interrupts flag
if (PulseCtr > 0) {
Input = (double) (PulsesPerMinute / (double)(( (unsigned long)Time ) * (unsigned long)PulseCtr)); // double has more percision
// PulseCtr = 0; // set pulse Ctr to zero
// debug();
if (!Skip) { //PID only needs an instantaneous reading and so if we skip a few no harm done since Delta T is no longer a fixed value
if (!myPID.Compute()) Serial.println();
analogWrite(PWMpin, Output);
}
else Skip = false;
analogWrite(PWMpin, Output);
//
Time = 0; // set time to zero to wait for the next rpm trigger.
Counter += PulseCtr;
PulseCtr = 0; // set pulse Ctr to zero
// we are automatically adjusting the diviser to preserve processor time after calculating last rpm
// starting at 0 RPM ~ 400+RPM we adjust the division of the pulses per revolution
// Counts = 100; constrain(map((int)Input, startRamp, MaxRPM, 1, PulsesPerRevolution), 1, PulsesPerRevolution);
PulsesPerMinute = (60.0 * 1000000.0) / (double)((double)PulsesPerRevolution / (double)Counts);
//Fill rpm array with rpm readings and average them for display.
rpmArray[0] = rpmArray[1];
rpmArray[1] = rpmArray[2];
rpmArray[2] = rpmArray[3];
rpmArray[3] = rpmArray[4];
rpmArray[4] = Input;
//Last 5 Average RPM Counts Eqauls....
printRpm = (rpmArray[0] + rpmArray[1] + rpmArray[2] + rpmArray[3] + rpmArray[4]) / 5;
radioTxArray[0] = printRpm;//average sent to hand controller
}
}
void debug()
{
/*
Serial.print("\nRSLT (1 = success) ");
Serial.println(rslt);
Serial.print("Data Sent ");
Serial.print(radioTxArray[0]);
Serial.print(" ");
Serial.println(radioTxArray[1]);
*/
/*
char S[20];
static unsigned long SpamTimer;
if ((unsigned long)(millis() - SpamTimer) >= 100) {
SpamTimer = millis();
Serial.print(" rpm: "); Serial.print(Input );
Serial.print(" motor: "); Serial.print(radioRxArray[0]);
Serial.print(" setpoint: "); Serial.print(radioRxArray[1]);
Serial.print(" height "); Serial.print(radioRxArray[3]);
Serial.print(" cal: "); Serial.print(radioRxArray[4]);
Serial.print(" dist: "); Serial.print(radioRxArray[5]);
Serial.print(" ping sensor "); Serial.println(dist);
}
*/
}