Using Data As set value and as offset

Hi all, quick question. i'm building a tiller pilor project with magnetometer compass control, a PID feedback loop for an old broken tillerpilot.

I have the seosor outputting a heading i degrees. with this value I want to press a button, this stores the heading as i fixed value to be used as the set point. And updates from the heading then used as error offset for correction.

for expample. boat is moving on a course of 270degrees. i press the button, arduino saves the current heading value as "Setpoint" the boat moves off course to 265, the error change in heading is then used as the error feedback in a pid control loop. (Hope i explained that clearly)

My problem is (probably a very basic one) that I cant work out how i would stop the setpoint updating with the heading as they are both using the same initial value.

Thanks in advance

Post the formatted code in code tags of your best effort and perhaps an other can see what's the thing and lend to? Without the code I can only guess what's the problem and only guess a solution.

uberroborman:
My problem is (probably a very basic one) that I cant work out how i would stop the setpoint updating with the heading as they are both using the same initial value.

If the setpoint only updates when you press a button I don't understand the problem.

We need to see your program (the complete program, please).

...R

there should be a "targetHeading" variable set by the button which is compared to an "actualHeading" that determines an error and subsequent correction.

make the corrections somewhat proportional to the error so that as the error becomes small, corrections don't cause oscillations

Slow reply from me, apologies. i'll upload the basic sketch i'm trying to get working (with a simple number counter) to then add into my full code after i've got the heading working steady.

Problem i'm having is when i press the button the value updates, but each following count, keeps updating again, or as in this attempt everything stops. I need to separate it to run the button press loop a single time (or update on another press). just failing to work out how...

int count = 25;
int pause;
int buttonpin = 2;
int button;
int j;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(buttonpin, INPUT);
  digitalWrite(buttonpin, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:


  for (j = 1; j <= count; j = j + 1) {

    Serial.print(j);
    Serial.print(",");
    Serial.print (pause);
    Serial.println (",");




    button = digitalRead(buttonpin);
    Serial.print(button);
    Serial.println(",");

    delay (1000);



    while (button == 0) pause = j; {

      //Serial.println(pause);
      //Serial.println( );
    }
  }
}

again, thanks for your time

uberroborman:
Problem i'm having is when i press the button the value updates,

What value updates?

How is your button wired? If it is not wired correctly the button will be floating at an indeterminate value when the button is not pressed. The usual arrangement is to use pinMode(pin, INPUT_PULLUP) and connect the button so that it connects the pin to GND when pressed. In that case it will be HIGH when not-pressed and LOW when pressed.

...R

Thanks for the reply,

 pinMode(buttonpin, INPUT);
  digitalWrite(buttonpin, HIGH);

This sets the internal resistor so the button isn't floating. I got a stable 0 & 1 when the button is pressed and unpressed (shown through the serial monitor )

As a test i'm getting the serial monitor to display the count to 25, when i press the button it would show the value (named pause in the code) as whatever number the count was on, but would increase with the count instead of staying at the paused value.

e.g
count =20, button pressed
count =21, pause value = 20
count =22, pause value = 21 so on

when i'm trying to get the pause value to stay at the initial value when the button was pressed (hoping that's clear)

thanks again

int count = 25;
int pause;
int buttonpin = 2;
int button;
int j;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(buttonpin, INPUT);
  digitalWrite(buttonpin, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:


  for (j = 1; j <= count; j = j + 1) {

    Serial.print(j);
    Serial.print(",");
    Serial.print (pause);
    Serial.println (",");




    button = digitalRead(buttonpin);
    Serial.print(button);
    Serial.println(",");

    delay (1000);



    if (button == 0){ pause = j;
    
    
     
    }
       
  }
}

Got this working in this test sketch. hopefully will manage to with the full code.

Cheers

the internal pullup is enabled by configuring the pin as INPUT_PULLUP not by setting the pin HIGH

pinMode(buttonpin, INPUT);
  digitalWrite(buttonpin, HIGH);

your code continually recognizes when a button is de-pressed instead of recognizing a single event when the button changes from not pressed to pressed

the following code reliably reads a button, checking for a change and performing an action, toggling a led when pressed.

// recognize multiple button presses; tgl LED when pressed

byte butPin = A1;
byte ledPin = 10;

byte butLst;


// -----------------------------------------------------------------------------
void setup (void)
{
    digitalWrite (ledPin, HIGH);
    pinMode      (ledPin, OUTPUT);

    pinMode      (butPin, INPUT_PULLUP);
    butLst      = digitalRead (butPin);
}

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);

    if (butLst != but)  {
        butLst = but;

        if (LOW == but)     // button pressed
            digitalWrite (ledPin, ! digitalRead (ledPin));
    }

    delay (10);         // debounce
}

gcjr:
the internal pullup is enabled by configuring the pin as INPUT_PULLUP not by setting the pin HIGH

pinMode(buttonpin, INPUT);

digitalWrite(buttonpin, HIGH);

The OP's code is valid - I just did not recognise it as I always use INPUT_PULLUP. You can do it either way.

...R