I am creating a lighting controller for a remote control car.
I have the code working, while the Arduino is running, but the way it works right now, it needs to be calibrated every time you want to use it.
I would like to store data created by calibration into EEPROM, and call the data from there during boot,. unless a button is pressed. Pressing a button, should start the calibration routine, after the first boot. During the first boot, I would like to go right into the setup routine --sort of a new device initialization.
The issue I have, is that I found that these values can range anywhere from 0 to 10,000, and from what I can see, EEPROM values can only go from 0 - 255.
Would someone please assist me with my EEPROM programming? I have never done anything like this before, and just bought my first Arduino --the nano, and started my first program only a few days ago.
For setup calibration checking, I was thinking about running through setup, and after it is done, write something into EEPROM, that could be checked with an if statement that checks for that value, or if a button is pressed while the setup loop is running --5 seconds.
I will be adding 2 more channels in the future, throttle, and 3rd channel.
The variables that need values stored, are
SteerOnBelow
SteerMin
SteerOnAbove
SteerMax
Any of these variables, can range from 0 to 10,000.
// 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 = 10000; // minimum steer sensor value
int SteerMax = 0; // maximum steer sensor value
int SteerOnBelow = 0;
int SteerOnAbove = 0;
// 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()
{
Serial.begin(9600);
Serial.println("Hello world!");
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
// calibrate during the first five seconds
while (millis() < 5000) {
// turn on LED to signal the start of the calibration period:
digitalWrite(led, HIGH);
//assign inputs to read
int SteerValue = pulseIn(steer_read, HIGH);
// record the maximum steer sensor value
if (SteerValue > SteerMax) {SteerMax = SteerValue;}
SteerOnAbove = (SteerMax - 200);
// record the minimum steer sensor value
if (SteerValue < SteerMin) { SteerMin = SteerValue;}
SteerOnBelow = (SteerMin + 200);
// 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 and vice-versa:
if (ledState_leftts == LOW)
ledState_leftts = HIGH;
else
ledState_leftts = LOW;
// set the LED with the ledState of 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);
}
}
//clost if > above
}
else {
digitalWrite (left_ts, LOW);
digitalWrite (right_ts, LOW);
digitalWrite (led, LOW);
}
//close the loop
Serial.println(SteerOnBelow);
Serial.println(SteerMin);
Serial.println(SteerOnAbove);
Serial.println(SteerMax);
}