int throttleDutyCycle(int throttleVal, int mode){
int defaultMode = 275;
switch(mode){
case 0:
return map(throttleVal, 0, 1023, 100, defaultMode);
break;
case 1:
return map(throttleVal, 0, 1023, 100, 425);
break;
case 2:
return map(throttleVal, 0, 1023, 100, 575);
break;
case 3:
return map(throttleVal, 0, 1023, 100, 725);
break;
case 4:
return map(throttleVal, 0, 1023, 100, 875);
break;
case 5:
return map(throttleVal, 0, 1023, 100, 1025);
break;
default:
return map(throttleVal, 0, 1023, 100, defaultMode);
break;
}
}#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(5,6);
const byte address[6] = "00001";
const uint8_t modePin = 7;
const uint8_t throttleInPin = 3;
const uint8_t throttleOutPin = 2;
const uint8_t relayPin = 0;
const uint16_t pwmFreq = 5000;
const int inputVals[5] = {100,300,500,700,900};
void setup() {
pinMode(modePin, OUTPUT);
pinMode(throttleInPin, OUTPUT);
pinMode(throttleOutPin, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
//delay(5000);
digitalWrite(relayPin, HIGH);
//Start RX code
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_250KBPS);
radio.startListening();
}
void loop() {
//Radio Code
bool buttonState;
if(radio.available()){
radio.read(&buttonState, sizeof(buttonState));
digitalWrite(LED_BUILTIN, !buttonState);
}
if (!buttonState){
//Throttle code
int opMode = operatingMode(analogRead(modePin));
int throttleVal = analogRead(throttleInPin);
int dutyCycle = throttleDutyCycle(throttleVal, opMode);
pwm(throttleOutPin, pwmFreq, dutyCycle);
}
else{
digitalWrite(throttleOutPin, LOW);
delay(5000);
}
buttonState = LOW;
}
// for default analog resolution of 10-bit
int operatingMode (int modeVal){
// 900, 700, 500, 300, 100, 0
// int mode;
// int inputVals[5] = {100,300,500,700,900};
if(modeVal < inputVals[0])
return 0;
else if (modeVal < inputVals[1])
return 1;
else if (modeVal < inputVals[2])
return 2;
else if (modeVal < inputVals[3])
return 3;
else if (modeVal < inputVals[4])
return 4;
else if (modeVal >= inputVals[4])
return 5;
}
Here's some code I have. It's going to control a 0-5V output (using level-shifting, pwm, and a low-pass filter) which will be a throttle input to a motor controller for an electric go-kart I built. I have a 6-position switch in which I installed 1k resistors to make a voltage divider to use as a selector switch. That selector switch will essentially result in a reduction of throttle output, effectively functioning as a sort of speed limiter. In addition to that, I'm using 2.4GHz transceiver modules to act as a remote kill switch, as well. I've used INPUT_PULLUP and reset the value to of the variable to LOW so if there's a disconnection of any sort, then it should default to essentially shutting off. (Sidenote: this code is currently untested and may be unfinished.)
So basically, just asking if anyone sees a good way to improve my code, make it more efficient/streamlined, or any other improvements anyone can suggest?