Transmitter Code:
//This goes onto the Transmitter Arduino!!
//This code is mostly written by user MaB from efoil.builders. Thanks to him! I adapted it for my needs on my board. So I probably have some extra buttons or whatnot.
/*
Ramped Hall sensor Transmitter for EFoil with 4 LEDs, calibration, remote battery low alarm and link status
Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <Servo.h>
RF24 radio(3, 2); // Connect the CE pin on the RF24 to pin D3 of your arduino, CSN pin 2
bool connected = false;
short failCount;
const uint64_t pipe = 0xE8E8F0F0E1LL; // If you change the pipe, you will need to update it on the receiver to.
unsigned long lastTransmission;
//Define throttle pin and values
const int hallPin = 0; // Hall sensor pin A0 analog pin //Max here. I am using a sliding potentiometer instead but I keep calling it the hall pin line MaB. Note that analoge pins start with A0 not A1.
int batteryPin = A3; //battery voltage //Max here, I don´t monitor battery voltage.
//variables for throttle and hall calibration
int valread = 0;
int valabs = 0;
int valsend = 0;
int val = 0;
Servo esc;
int text;
int valMin = 1023;
int valMax = 0;
int numofCalibratinsamples = 600; // number of calibration samples
int calibrationtime = 5000; // Calibrationtime in ms
int batteryval = 0;
float Voltage = 0.00000000;
//Define the ramp to soften the throttle reaction
#define MIN_PULSE_WIDTH 1500
#define MAX_PULSE_WIDTH 2000
int throttle_current = 0;
int throttle_sp = 0;
int step_size_us = 13;
// Loop update | Step Size | Time to full throttle (1000 - 2000us)
// 20ms | 10 | 2.0s
// 20ms | 15 | 1.3s
// 20ms | 25 | 0.8s
void setup() {
//At startup,swing pull the trigger few times, min to max, to calibrate the sensor.
while (millis() < 5000)
{
valread = analogRead(hallPin);
if (valread > valMax) //set the Max
{
valMax = valread;
}
//set the Min
if (valread < valMin)
{
valMin = valread;
}
}
//Open radio pipe, set level, channel etc.
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.setChannel(111);
radio.setAutoAck(true);
radio.enableAckPayload();
radio.enableDynamicPayloads();
radio.openWritingPipe(pipe);
delay(1000);//I dont know what for, probably to give the chips time to connect.
Serial.begin(9600); //needed for serial monitor.
}
void loop()
{
valread = analogRead(hallPin); //Read input from analog hallPin and store in val
Serial.print("valread: ");
Serial.print(valread);
// Scale throttle analog value to ESC PWM min and max values (1000us to 2000us)
throttle_sp = map(valread, (valMin + 10), (valMax - 10), MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); //mapping val to minimum and maximum(Change if needed)
{ //throttle_current = (throttle_sp); //era disattivato
if ( throttle_current > throttle_sp )
{
// Decreasing throttle faster due to slow response of ESC on decel
throttle_current -= (step_size_us * 2) ;
throttle_current = max(throttle_current, throttle_sp);
}
else if ( throttle_current < throttle_sp )
{
// Increasing throttle slowly
throttle_current += (step_size_us / 2);
throttle_current = min(throttle_current, throttle_sp);
}
}
// Final min checks
{
throttle_current = max(throttle_current, MIN_PULSE_WIDTH);
// Final max checks
throttle_current = min(throttle_current, MAX_PULSE_WIDTH);
}
Serial.print(" throttle SP:" );
Serial.println(throttle_current);
{
esc.write(throttle_current); // Ramp throttle to desired target
}
// Send ramped throttle value to receiver
int time = millis() - lastTransmission;
//DEBUG_PRINT(time);
if (time >= 0) {
lastTransmission = millis();
boolean sendSuccess = false;
// Transmit the speed value (0-255).
sendSuccess = radio.write(&throttle_current, sizeof(throttle_current));
// Listen for an acknowledgement reponse
while (radio.isAckPayloadAvailable())
{
radio.read(&lastTransmission, sizeof(lastTransmission));
}
if (sendSuccess == true)
{
// Transmission was a succes
failCount = 0;
sendSuccess = false;
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
Serial.print("Transmission succes");
}
else
{
// Transmission was not a succes
failCount++;
Serial.print("Failed transmission");
}
// If lost more than 5 transmissions, we can assume that connection is lost.
if (failCount < 6)
{
connected = true;
} else {
connected = false;
}
}
}