Here is my entire code, as it is currently written.
This is a controller for lighting on a remote control car.
What is supposed to happen, is that the user turns the wheel on the remote to both extremes during setup.
The coding should permit both a central deadband, where neither turn signal is active, along with high/low values set during cal, and trigger points which are just under max/min values.
The cal process, will be turning the steering wheel. As an example: Right turn "Max" reads at 3000, Left turn "Min" reads at 2000, so the max and min get set as such during cal procedure.
The Steer on Above/Below variables, will cause the light blinking condition to occur before max/min value is met.
The issue with the standard deadband setup, is that there are quite a few times when the steering channel max/min values are changed, trimming for straight driving, limiting servo throw, etc.
The neutral position, is also altered, so I would appreciate, if there was a way to assign the neutral position, based on the current neutral position, and when the steering wheel is moved a certain amount in either direction, the left or right led blinking program is activated.
// constants won't change. Used here to
// set pin numbers:
const int led = 13; //Internal LED
const int steer_read = 2; //Steering read
const int throt_read = 3; //Throttle read
const int aux_read = 4; //Aux Channel read
const int left_ts = 5; //Left turn signal
const int right_ts = 6; //Right turn signal
const int break_lt = 7; //Brake Light
const int backup_lt = 8; //Backup Light
// Variables will change:
int ledState_leftts = LOW; // ledState used to set the left ts
int ledState_rightts = LOW; // ledState used to set the right ts
long previousMillis = 0; // will store last time LED was updated
//CalibrationVariables
//Steer Cal
int SteerValue = 0; // the steer sensor value
int SteerMin = 1023; // minimum steer sensor value
int SteerMax = 0; // maximum steer sensor value
const int SubFromSteer = 200; //Create deadband where wheel is not at max/min, but max/min trigger is activated.
int SteerOnBelow = (SteerMin + 200); //////////////////////////////////////////////////////////////ISSUES WITH THESE LINES
int SteerOnAbove = (SteerMax - 200); /////////////////////////////////////////////////////////////////////////////////////
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval_for_flashers = 667; // interval_for_flashers at which to blink (milliseconds)
void setup()
{
pinMode(led, OUTPUT);
pinMode(steer_read, INPUT); //Steering read
pinMode(throt_read, INPUT); //Throttle read
pinMode(aux_read,INPUT); //Aux 3 read
pinMode(left_ts,OUTPUT);//Left Turn Signal
pinMode(right_ts,OUTPUT);//Right Turn Signal
// pinMode (break_lt, OUTPUT);//Brake light
//pinMode (backup_lt, OUTPUT);//Backup light
//CALIBRATION ROUTINE
// turn on LED to signal the start of the calibration period:
digitalWrite(led, HIGH);
// calibrate during the first five seconds
while (millis() < 5000) {
//assign inputs to read
int SteerValue = pulseIn(steer_read, HIGH);
//int ThrotValue = pulseIn(throt_read, HIGH);
//int AuxValue = pulseIn(aux_read, HIGH);
// record the maximum steer sensor value
if (SteerValue > SteerMax) {SteerMax = SteerValue;}
// record the minimum steer sensor value
if (SteerValue < SteerMin) { SteerMin = SteerValue;}
//Calibrate Throt
// record the maximum throt sensor value
// if (ThrotValue > ThrotMax) { ThrotMax = ThrotValue;}
// record the minimum throt sensor value
// if (ThrotValue < ThotMin) { ThrotMin = ThrotValue;}
//Calibrate Aux
// record the maximum aux sensor value
// if (AuxValue > AuxMax) { AuxMax = AuxValue;}
// record the minimum throt sensor value
// if (AuxValue < AuxMin) { AuxMin = AuxValue;}
// signal the end of the calibration period
digitalWrite(13, LOW);
}
}
void loop() {
int strduration = pulseIn(steer_read, HIGH);
if (strduration <= SteerOnBelow || strduration >= SteerOnAbove){
if (strduration <= SteerOnBelow){
//Turn Left
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval_for_flashers) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on
if (ledState_leftts == LOW)
ledState_leftts = HIGH;
else
ledState_leftts = LOW;
// set the LED to the variable:
digitalWrite(led, ledState_leftts);
digitalWrite(left_ts, ledState_leftts);
}
}//close if below
if (strduration >= SteerOnAbove){
//Turn Right
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval_for_flashers) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState_rightts == LOW)
ledState_rightts = HIGH;
else
ledState_rightts = LOW;
// set the LED with the ledState of the variable:
digitalWrite(led, ledState_rightts);
digitalWrite(right_ts, ledState_rightts);
}
}
//close if > above
}
else {
digitalWrite (left_ts, LOW);
digitalWrite (right_ts, LOW);
digitalWrite (led, LOW);
}
//close the loop
}