Go Down

Topic: Arduino UNO - changing settings without showing code (Read 929 times) previous topic - next topic

Kamool

Hello!

Is there a possibility to change Arduino UNO ints without showing rest of the code. I mean that if I will give my project to someone he shouldn't be able to see rest of the code or mess with that. I would like him to just change values he should.

Here is simple code which demonstrates what I mean:

Code: [Select]

const int A = 70;
const int B = 80;
const int Button = 12;
const int led = 13;
int ButtonState = 0;
int lastButtonState = 0;


void setup() {
  pinMode (led, OUTPUT);
  pinMode (9, OUTPUT);
  pinMode (Button, INPUT);
}

void loop() {

  ButtonState = digitalRead(Button);

  if (ButtonState != lastButtonState) {
    if (ButtonState == HIGH) {
      digitalWrite(9, HIGH);
      delay(A);
      digitalWrite(9, LOW);
      delay(B);
      digitalWrite(9, HIGH);
      delay(A);
      digitalWrite(9, LOW);
    }
    else {}
    delay(50);

  }
  lastButtonState = ButtonState;

}


There is const int A and B which sets delay time, I would like someone to be able to just change 70 and 80 numbers without seeing other code. Is it possible to hide that?


Robin2

I assume you want the new values to remain after the Uno is switched off.

You can store values in the EEPROM memory and you can include code in your program that would allow a user to change the stored values without needing to see the program code

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Kamool

Sorry for late response.

Could You please guide me how to do such thing?
I have no clue at all.

rpt007

You will need a user interface (e.g. LCD display).

In the setup you read the current values being stored in EEPROM and restore them into variables.
In loop those variables do whatever they are planned to do.

In loop you will continuously read a trigger button (I prefer rotary encoder knob with built-in switch to make it easy for the user) and when a user presses the trigger button, this will be treated as trigger for a setup menu where the user can then scroll through menus by rotating the encoder knob and modify parameters. When the user has confirmed the modified parameter (e.g. by pressing the switch), this value will be stored at the same location in EEPROM, where setup has previously read the adequate variable value.

I always read back the modified EEPROM value into the variable to make sure that the value really was changed to the new one and then display for 1-2 sec the new value to the user so he knows exactly that this is the new operations value permanently stored in EEPROM to be used by next start.

To do all this, you will need to understand:
- how to control LCD displays
- how to connect and drive a rotary encoder
- how to debounce buttons
- how to setup menus / submenus
- how to read and write (preferably update) EEPROM
- how to merge all those in a slim user interface and not delaying or interfering with your main application

That said - you will have a lot to do if you are just starting with programming an Arduino.
But, it is worth while doing it, you will learn a lot - but don't expect that there are ready made solutions for that. My experience says that each project has its challenges on its own. You will find a lot of examples which others have created and you can try to copy and paste - but if you don't understand how to use those copied pieces and how they fit together, it won't work.
Before you ask:
Did you really read and understand How to use this forum ?
AND:
Do you have already some solution or is a part of the problem sitting in front of the screen?  :)

sterretje

And the simple version of above:

Use serial monitor (or other terminal program) to let the user enter the values.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Kamool

Thank You for reply and help :)

I made something like this, will it work?

Code: [Select]

#include <EEPROM.h>
#include <EEPROMAnything.h>

void(* resetFunc) (void) = 0;
const int Button = 12;
const int menuButton = 11;
const int led = 13;
int ButtonState = 0;
int lastButtonState = 0;
int menuButtonState = 0;
int lastmenuButtonState = 0;
int menuvar;

//EEPROM stored variables:
int A;
int B;

void setup() {
  pinMode (led, OUTPUT);
  pinMode (9, OUTPUT);
  pinMode (Button, INPUT);
  pinMode (menuButton, INPUT);
  getEEPROM();
  check_first_startup();
}

void loop() {

  ButtonState = digitalRead(Button);
  menuButtonState = digitalRead(menuButton);

  if ((ButtonState != lastButtonState) || (menuButtonState != lastmenuButtonState)) {
    if (ButtonState == HIGH) {
      digitalWrite(9, HIGH);
      delay(A);
      digitalWrite(9, LOW);
      delay(B);
      digitalWrite(9, HIGH);
      delay(A);
      digitalWrite(9, LOW);
    }
    else if (menuButtonState == HIGH) {
      menuvar = 1;
      menu()
    }
    else {}
    delay(50);

  }
  lastButtonState = ButtonState;
  lastmenuButtonState = menuButtonState
}

void getEEPROM() {
  A = EEPROM.read(0);
  B = EEPROM.read(1);
}
void check_first_startup() {
  if (A == 0) {
    Serial.println("LOADING DEFAULTS");
    A = 70;
    B = 170;
    writeEEPROM();
    resetFunc();
  }
}

void writeEEPROM() {
  EEPROM.write(0, A);
  EEPROM.write(1, B);
}

void menu() {
  while (menuvar == 1) { ///this keeps in the menu
    Serial.println("Enter A to change A value, B to change B value, D to check A and B values, R to set value to defaults or X to exit menu and save changes");

    if (Serial. available ()) {
      char answer = Serial.read();
      if (answer == 'A') {
        Serial.println("Enter A number value");
        if (Serial. available ()) {
          A = Serial.read()
              delay(100);
        }
      }
      else if (answer == 'B') {
        Serial.println("Enter B number value");
        if (Serial. available ()) {
          B = Serial.read()
              delay(100);
        }
      }
      else if (answer == 'D') {
        Serial.println("A value: ");
        Serial.println(A);
        Serial.println("B Value: ");
        Serial.println(B);
        delay(10000);

      }
      else if (answer == 'X') {
        Serial.println("Saving changes and exiting menu");
        delay(100);
        writeEEPROM();
        getEEPROM();
        menuvar = 0;

      }
      else if (answer == 'R') {
        Serial.println("RESET!");
        delay(100);
        for (int i = 0; i < 512; i++) {
          EEPROM.write(i, 0);
        }
        resetFunc();
      }


    }
  }
}

Robin2

I made something like this, will it work?
Try it and find out. The Arduino system is great for learning-by-doing

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Kamool

I will, but right now I don't have Arduino... It just died... I gave it to my friend and He is trying to make it work :)

So what do You think about that code?

Robin2

So what do You think about that code?
I find it almost impossible to assess whether a program will work without trying it.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sterretje

It will not work because your variables are integers (2 bytes). You can use EEPROM.put and EEPROM.get to read the values. Also take into account that you have integers, so your indices in the eeprom are 0 and 2 (not 0 and 1).

Code: [Select]

#include <EEPROM.h>

void setup()
{
  Serial.begin(115200);
  
  int A = 0x0123;
  int B = 0x4567;
  EEPROM.put(0, A);
  EEPROM.put(2, B);

  delay(1000);

  int C;
  EEPROM.get(0, C);
  int D;
  EEPROM.get(2, D);

  Serial.println(C, HEX);
  Serial.println(D, HEX);
}


There is something more to polish; something like
Code: [Select]
       if (Serial. available ())
        {
          A = Serial.read()
              delay(100);
        }

will only allow one digit in A so if you send the text 10 from serial monitor, the value for A will be 0x30.

I suggest that you read Serial Input Basics - updated to get an idea how to receive data.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up