updating individual variables without disrupting the rest of the code?

Hi, I am using an Arduino Uno to sense several beam-break systems and perform certain functions when those beam-breaks happen (LEDs switch on, valves open etc.).

If I want to change a particular variable (e.g., the amount of time a valve opens), I currently make the change in the code, then re-upload the entire code on the Arduino.

I would like to know if there is a way of uploading the single variable change that I have made WITHOUT disrupting the running of the rest of the code? I would like the changing of the variable to be as seamless as possible with the running of the rest of the code.

Thanks for your help,

mtc_brown

would like to know if there is a way of uploading the single variable change that I have made WITHOUT disrupting the running of the rest of the code?

No there is not.

If you think you want to do this then you have to input the new value some how. Like through the serial port of some of the I/O pins. You have to write code that does this.

Thanks Mike, do you (or anyone else) know of any example code out there that I could use to find out how to upload only through some of the serial port i/o pins?

Thanks,

mtc_brown

Rather than changing constants in the code, you could store these values in EEPROM instead.

mtc_brown: Thanks Mike, do you (or anyone else) know of any example code out there that I could use to find out how to upload only through some of the serial port i/o pins?

Thanks,

mtc_brown

Here is an extremely bad example

const byte ledPin = 13;
boolean ledState = 0;
unsigned long period = 5;
unsigned long startTime;

void setup() 
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  startTime = millis();
}

void loop() 
{
  if (Serial.available())
  {
    char adjustPeriod = Serial.read();
    if (adjustPeriod == '+')
    {
      period++;
      Serial.println("up");
    }
    else
      if (adjustPeriod == '-')
      {
        period--; 
        Serial.println("down");
      }
      Serial.print("Period now : ");
      Serial.println(period);
  }

  if (millis() - startTime > period * 1000)
  {
    ledState = !ledState;
    startTime = millis();
  }
  digitalWrite(ledPin, ledState);
}

It is bad not least because there is no bounds checking. I will leave you to determine what happens when period goes below zero if you enter too many minus signs. Don't forget to make sure that your baud rate matches that in the code or vice versa.

any example code out there that I could use to find out how to upload only through some of the serial port i/o pins?

You have to be more specific than that. What sort of input device do you want? How many variables do you want to change?

In a situation like this, I would use a potentiometer to input the value in real-time. If the value is not tweaked very often, you could use a small trim-pot. If it's more often tweaked, you could use a larger pot with a nice knob.

Here is some sample code.

const byte valveOpenTimePotPin= A0;

// Potentiometer will vary between these two values
// values are assumed to be millis() for time to keep the valve open or whatever
const int maxValveOpenMillis= 5000;
const int minValveOpenMillis = 1000;


void setup()
{
}

void loop()
{
  int a = analogRead(valveOpenTimePotPin);
  int valveOpenTime = map(a, 0, 1023, maxValveOpenMillis, minValveOpenMillis);
  
  // do the rest of your code here.
}

Just reading the original question - and the answer is "anything". A single button, (already suggested) potentionmeter, a small keyboard, a LCD-encoder-menu system, ... it all depends on how complex the variables are that you need to modify, and what sort of (visual) feedback you have on entering a new "value".

Without gettig into code specifics, a single button could be used to step between low, medium, high delay of the valve opening. Either you have to wait until a valve cycle to see if the button push took the desired effect, or you have a LED than blinks something to tell that it has understood the button command. A second button could choose which valve to open close. Many digital clocks use just two buttons to set the clock, switch between display mode, set an alarm and so on - there is a source of inspiration But there are visual cues on where you are in the "state" of what you are changing. A single button can also input 3 "states" by using tricks like short press, long press, double press.

"without disrupting the rest of the code" - presumes that you have written all the other code to be non-blocking and that the input button handler (with whatever visual feedback you decide on) also is non blocking. (A sort of primitive multitask, so to speak)

I've coded such single button tricks in seperate projects. Difficult? Well, not really if you understood how to write nonblocking code (intro: blink without delay)

Brilliant, thank you all for your advice. I think I will try the potentiometer idea and see how that goes.

Thanks again!

mtc_brown