Go Down

Topic: Slotcar speed controller with display and electronic memory project (Read 6 times) previous topic - next topic

DuaneB

Hi,

Thats standard behaviour for an UNO, to reset on serial connection. I am sure there arensimple work arounds if you search for 'UNO disable auto reset'

Duane B

rcarduino.blogspot.com

backbone

OK,

As the serialmonitor is only for debuggung now I leave it as it is.

Now further with a more serious issue.
The sensorMin and sensorMax value need to be stored in the EEPROM so once a first calibration of the speedsensor has been performed at a new startup it should read the values for sensorMin and sensorMax from the EEPROM.
Simple.

For some odd reason it does not do that.
In the serial monitor I see the values 275 and 441 as lowest and highsest vales after calibration.
I perform a manual write by a pushbutton to the EEPROM for address 0 and 1
But after sending this data to the EEPROM and restart the saved data is not read.

For the brakevalue I perform same write/read and that is working nicely.

Paco

Code: [Select]

#include <EEPROM.h>



// constants that will not change:

const int displaybuttonPin = A0;        // pin that the 5 pushbutton from display is attached to
const int sensorPin = A1;               // pin that the sensor is attached to
const int speedcurveupbuttonPin = A2;    // the number of the input pin
const int speedcurvedownbuttonPin = A3;  // the number of the input pin
const int speedstartupbuttonPin = A4;        // the number of the input pin
const int speedstartdownbuttonPin = A5;      // the number of the input pin
const int pwmoutspeedPin = 2;           // the number of the output pin
const int pwmoutbrakePin = 3;           // the number of the output pin
const int calibratebuttonPin = 4;       // the number of the input pin
const int brakeupbuttonPin = 5;        // the number of the input pin
const int brakedownbuttonPin = 6;      // the number of the input pin
const int ledPin = 13;                  // the number of the LED output pin
const int writebuttonPin = 10;
const int led2Pin = 12;
// pin 7,8,9,10,11,12 are reserved for the display communication lines

// variables that will change:
int calibratebuttonState = 0;         // variable for reading the pushbutton status
int brakeupbuttonState = 0;
int brakedownbuttonState = 0;
int writebuttonState = 0;
int sensorValue = 0;                  // the speed sensor value
int sensorMin = 1023;                 // minimum sensor value
int sensorMax = 0;                    // maximum sensor value
int deathBand = 10;                   // setting for deathband around zero point of trigger
int speedcurveValue = 0;              // setting for speedcurve (exponentional)
int speedstartValue = 0;              // setting for starting point of speed
int brakecurveValue = 0;              // setting for brakecurve (exponentional)
int brakeValue = 125;                 // Brake value set
int modelName = 9;                    // Model name in number raging from 1 to 10 for later recall
int displaybuttonCount = 0;           // Counter for display button
int sensorMinold = 0;
int sensorMaxold = 0;
int speedcurveValueold = 0;
int speedstartValueold = 0;
int brakecurveValueold = 0;
int brakeValueold = 0;
int modelNameold = 0;
int sensorMin1 = 0;
int sensorMax1 = 0;

void setup()
{
  Serial.begin(9600);                       // Output to serial writing
 
  pinMode(ledPin, OUTPUT);                  // initialize the led as a output control pin
  pinMode(calibratebuttonPin, INPUT);       // initialize the pushbutton pin as an input control pin
  pinMode(brakeupbuttonPin, INPUT);         // initialize the pushbutton pin as an input control pin
  pinMode(brakedownbuttonPin, INPUT);       // initialize the pushbutton pin as an input control pin
  pinMode(speedstartupbuttonPin, INPUT);    // initialize the pushbutton pin as an input control pin
  pinMode(speedstartdownbuttonPin, INPUT);  // initialize the pushbutton pin as an input control pin
  pinMode(speedcurveupbuttonPin, INPUT);    // initialize the pushbutton pin as an input control pin
  pinMode(speedcurvedownbuttonPin, INPUT);  // initialize the pushbutton pin as an input control pin
  pinMode(writebuttonPin, INPUT);
  pinMode(led2Pin, OUTPUT);
  //+++++++++++ EEprom read +++++++++++++
     

  sensorMin1 = EEPROM.read(0);        // read a byte from the current address of the EEPROM
  sensorMax1 = EEPROM.read(1);        // read a byte from the current address of the EEPROM 
//speedcurveValue = EEPROM.read(2);  // read a byte from the current address of the EEPROM
//speedstartValue = EEPROM.read(3);  // read a byte from the current address of the EEPROM
//brakecurveValue = EEPROM.read(4);  // read a byte from the current address of the EEPROM
brakeValue = EEPROM.read(5);       // read a byte from the current address of the EEPROM
//modelName = EEPROM.read(6);        // read a byte from the current address of the EEPROM
delay (500);

}

void loop()
{
       // manual write to the EEProm of address 0 and 1
       writebuttonState = digitalRead(writebuttonPin);
       if (writebuttonState == HIGH)
       {EEPROM.write(0,sensorMin); EEPROM.write(1,sensorMax); digitalWrite(led2Pin, HIGH);}
       else
       {digitalWrite(led2Pin, LOW);}
       
  //send data to serial port
      Serial.print("SPD=" );                       
      Serial.print(sensorValue);
      Serial.print(" BRK=" );                       
      Serial.print(brakeValue);
      Serial.print(" MIN=" );                       
      Serial.print(sensorMin);
      Serial.print(" MAX=" );                       
      Serial.print(sensorMax);
      Serial.print(" MINWR=" );                       
      Serial.print(sensorMin1);
      Serial.print(" MAXWR=" );                       
      Serial.println(sensorMax1);
     

      delay(20);

 
  calibratebuttonState = digitalRead(calibratebuttonPin); // read the state of the pushbutton value:

  if (calibratebuttonState == HIGH) // check if the calibrationpushbutton is pressed.
   {
     // calibration mode for the full throw of the speedsensor trigger
    digitalWrite(ledPin, HIGH); sensorValue = analogRead(sensorPin); // turn LED on:
    analogWrite(pwmoutspeedPin, 0); analogWrite(pwmoutbrakePin, 0);
    if (sensorValue > sensorMax) {sensorMax = sensorValue;} // record the maximum sensor value
    if (sensorValue < sensorMin) {sensorMin = sensorValue;} // record the minimum sensor value
   }
  else
   {
     //regular speed / brake mode
    digitalWrite(ledPin, LOW); // turn LED off:
   
    sensorValue = analogRead(sensorPin); // read the sensor:
    sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);// apply the calibration to the speed sensor reading
    sensorValue = constrain(sensorValue, 0, 255); // in case the sensor value is outside the range seen during calibration
    brakeValue = constrain(brakeValue, 0, 255); // in case the sensor value is outside the range
         // control the outputs
      if (sensorValue > deathBand) {analogWrite(pwmoutspeedPin, sensorValue); analogWrite(pwmoutbrakePin, 0  ); }
        else
      {analogWrite(pwmoutspeedPin, 0); analogWrite(pwmoutbrakePin, brakeValue); digitalWrite(ledPin, HIGH);}
     

      //     +++++ check button activities +++++++
      brakeupbuttonState = digitalRead(brakeupbuttonPin);
      brakedownbuttonState = digitalRead(brakedownbuttonPin);
     
      if (brakeupbuttonState == HIGH) {brakeValue = brakeValue + 1;}
      if (brakedownbuttonState == HIGH) {brakeValue = brakeValue - 1;}
      //if (speedstartupbuttonPin == HIGH) {speedstartValue= speedstartValue + 1;}
      //if (speedstartdownbuttonPin == HIGH) {speedstartValue= speedstartValue - 1;}
      //if (speedcurveupbuttonPin == HIGH) {speedcurveValue = speedcurveValue +1;}
      //if (speedcurvedownbuttonPin == HIGH) {speedcurveValue = speedcurveValue -1;}



       //++++++++++++ EEprom write ++++++++++++
      //if (sensorMin != sensorMinold)
      //{EEPROM.write(0,sensorMin); sensorMinold == sensorMin;}
      //if (sensorMax != sensorMaxold)
      //{EEPROM.write(1,sensorMax); sensorMaxold == sensorMax;}
      //if (speedcurveValue != speedcurveValueold)
      //{EEPROM.write(2,speedcurveValue); speedcurveValueold == speedcurveValue;}
      //if (speedstartValue != speedstartValueold)
      //{EEPROM.write(3,speedstartValue); speedstartValueold == speedstartValue;}
      //if (brakecurveValue != brakecurveValueold)
      //{EEPROM.write(4,brakecurveValue); brakecurveValueold == brakecurveValue;}
      if (brakeValue != brakeValueold)
      {EEPROM.write(5,brakeValue); brakeValueold == brakeValue;}
      //if (modelName != modelNameold)
      //{EEPROM.write(6,modelName); modelNameold == modelName;}
     

      //sensorMinold = sensorMin;
      //sensorMaxold = sensorMax;
      //speedcurveValueold = speedcurveValue;
      //speedstartValueold = speedstartValue;
      //brakecurveValueold = brakecurveValue;
      brakeValueold = brakeValue;
      //modelNameold = modelName;
     

       
   }

}
Never to old to learn and I learn every day

wildbill

EEPROM write takes a byte parameter. Your 275 and 441 values are too big. You will need to write two bytes per calibration value - take a look at the lowbyte and highbyte functions.

DuaneB

Hi Backbone,

Looking at all the sections of your code that are commented out, it looks like you should start using versions.  Strip out all the commented out code and save your project as SCSC_V1. If you add another feature, make it version 2 and so on. If you get to version 7 and something doesn't work its great to have six earlier versions that do to fall back on.

I do this all the time and it saves so much time.

Duane B.

rcarduino.blogspot.com




backbone

Thanks Duane,

I will have a look at low and high byte.
I thought 1024 was the limit not 255.
bits,bytes,niblles........confusing.

I am used of commeting things out for debugging.
This is already version 9. :-)

Paco
Never to old to learn and I learn every day

Go Up