Update variable while running if statement

I am working on a code to read input from a joystick, and move a linear actuator (in the actuatorPosition function) based on said input. I can get readings from the joystick, but they are not updating continuously, only once every time I reset the board. How would I get it to simultaniously update the joystick value (sticky), and run the if statements. I was thinking maybe interrupts? My loop is attached below. Thanks for any help!

void loop() { // main loop //Serial.println("pulses = "); //Serial.println(pulses); // Serial.println("goPos = "); // Serial.println(goPos);

rawStickX = analogRead(A0);
rawStickY = analogRead(A5);
//noInterrupts(); //?
sticky = map(rawStickY, 100, 900, 0, 2400);
Serial.println(sticky);
//interrupts(); //?

if (sticky < 0) {
sticky = 0;
do {
actuatorPosition(goPos);
} while (goPos != lastGoPos);
}

if (sticky > minY[0] && sticky <= maxY[0])
{
aLength = .125;
goPos = 1;
do {
actuatorPosition(goPos);
} while (goPos != lastGoPos);
}
if (sticky > minY[1] && sticky <= maxY[1])
{
aLength = .250;
goPos = 2;
do {
actuatorPosition(goPos);
} while (goPos != lastGoPos);
}
if (sticky > minY[2] && sticky <= maxY[2])
{
aLength = .375;
goPos = 3;
do {
actuatorPosition(goPos);
} while (goPos != lastGoPos);
}
if (sticky > minY[3] && sticky <= maxY[3])
{
aLength = .500;
goPos = 4;
do {
actuatorPosition(goPos);
} while (goPos != lastGoPos);
}
if (sticky > minY[4] && sticky <= maxY[4])
{
aLength = .625;
goPos = 5;
do {
actuatorPosition(goPos);
} while (goPos != lastGoPos);
}
}

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

Always show us your 'current' compete sketch.

And your COMPLETE sketch, too.

We can't see what actuatorPosition() does. I'm going to guess, though, that it does NOT call analogRead() to read the joystick position, and that that is the problem. So, fix it. It isn't rocket surgery.

  if (sticky < 0) {
    sticky = 0;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }

if the actuatorPosition function does not update goPos or lastGoPos, then this loop will never terminate. The sketch will get stuck. This is why we need the whole sketch - we don't know what actuatorPosition does, we doint know where these two variables are defined.

Oh, and the forum uses bbcode for formatting, so it's
** **[code][/code]** **
, not
** **<code></code>** **
. But at least you made the effort, unlike a few other posters here.

PaulMurrayCbr:

  if (sticky < 0) {

sticky = 0;
    do {
      actuatorPosition(goPos);
    }  while (goPos != lastGoPos);
  }

The loop calls actuatorPosition, which sets the position and then calls moveActuator to actually carry out the movement

Here is my full code:

//pulse count variables
volatile unsigned long count = 0;
unsigned long copyCount = 0;
unsigned long lastRead = 0;
unsigned long interval = 250;
volatile int pulses = 0;
//int pulses = 1250;

//pin definitions
int rawStickX = analogRead(A0);
int rawStickY = analogRead(A5);
#define encoder 2
#define button 4

// motor one
#define actuator 10
#define Tin1 9
#define Tin2 8

//other variables
float aLength = 0;
int goPos = 0;
int lastSticky = 0;
int lastGoPos = 0;

//joystick map variables
//y axis
int sticky = map(rawStickY, 100, 900, 0, 2400);  //map to usable ranges for actuator
//int sticky = 1250;// for debugging

//arrays for max and min joystick values for every .125 inch
int minY[] = { 0, 101, 201, 301, 401, 501, 601, 701, 801, 901, 1001, 1101, 1201, 1301, 1401, 1601, 1701, 1801, 1901, 2001, 2101, 2201, 2301};

int maxY[] = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400};

int actPos[] = { 0, 6, 12, 18, 23, 29, 35, 41, 46, 52, 58, 64, 69, 75, 81, 87, 92, 98, 104, 110, 115, 121, 127, 133, 138 };

void setup() {

  pinMode(encoder, INPUT_PULLUP);
  pinMode(actuator, OUTPUT);
  pinMode(Tin1, OUTPUT);
  pinMode(Tin2, OUTPUT);
  pinMode(rawStickX, INPUT);
  pinMode(rawStickY, INPUT);
  pinMode(button, INPUT);

  Serial.begin(115200);
  Serial.println("start...");

  attachInterrupt(digitalPinToInterrupt(encoder), countISR, RISING); //interrupt signal to pin 2
  pulses = 0;
  count = 0;
  //aLength = 0;
}

void countISR()
{ //increment or decrement pulses, based on actuator direction

  if (digitalRead(Tin1) == HIGH && digitalRead(Tin2) == LOW)
  {
    count++;
  }
  else if (digitalRead(Tin1) == LOW && digitalRead(Tin2) == HIGH)
    count--;
}

void pulseCount()
{ //code to read encoder pulses... about 46 per inch

  if (millis() - lastRead >= interval) //read interrupt count every 250ms
  {
    lastRead += interval;
    // disable interrupts,make copy of count,reenable interrupts
    noInterrupts();
    copyCount = count;
    // count = 0;
    interrupts();

    //Serial.println(count);
    delay(5); //buffer

    pulses = copyCount;
    return pulses; //make pulses global
  }
}

void stopActuator() {
  analogWrite(actuator, 0);
  Serial.println("STOPPING");
}

void moveActuator(int aDirect, int aSpeed) {
  if (aDirect == 1) {//forward
    digitalWrite(Tin1, HIGH);
    digitalWrite(Tin2, LOW);
    analogWrite(actuator, aSpeed);
  }
  else if (aDirect == 0) { //reverse
    digitalWrite(Tin1, LOW);
    digitalWrite(Tin2, HIGH);
    analogWrite(actuator, aSpeed);
  }
}

void actuatorPosition(int setPos) {

  //Serial.println("Running Actuator Position");
  pulseCount();
  if (button != 1) {
    if (actPos[setPos] > pulses) {
      moveActuator(1, 127);
      //Serial.println("MOVING FORWARDS");
    }
    else {
      //Serial.println("STOPPING 1");
      stopActuator();
    }
  }
  else if (actPos[setPos] < pulses) {
    moveActuator(0, 127);
    //Serial.println("MOVING BACKWARDS");
  }
  else {
   // Serial.println("STOPPING");
    stopActuator();
  }
  //Serial.print("setPos = ");
  //Serial.println(actPos[setPos]);
  //Serial.print("pulses = ");
  //Serial.println(pulses);
}

void loop()
{ // main loop

  Serial.println("pulses = ");
  Serial.println(pulses);
//  Serial.println("goPos = ");
//  Serial.println(goPos);
  
  rawStickX = analogRead(A0);
  rawStickY = analogRead(A5);
  sticky = map(rawStickY, 100, 900, 0, 2400);

  if (sticky < 0) {
    sticky = 0;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }

  if (sticky > minY[0] && sticky <= maxY[0])
  {
    aLength = .125;
    goPos = 1;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[1] && sticky <= maxY[1])
  {
    aLength = .250;
    goPos = 2;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[2] && sticky <= maxY[2])
  {
    aLength = .375;
    goPos = 3;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[3] && sticky <= maxY[3])
  {
    aLength = .500;
    goPos = 4;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[4] && sticky <= maxY[4])
  {
    aLength = .625;
    goPos = 5;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[5] && sticky <= maxY[5])
  {
    aLength = .750;
    goPos = 6;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[6] && sticky <= maxY[6])
  {
    aLength = .875;
    goPos = 7;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[7] && sticky <= maxY[7])
  {
    aLength = 1.000;
    goPos = 8;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[8] && sticky <= maxY[8])
  {
    aLength = 1.125;
    goPos = 9;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[9] && sticky <= maxY[9])
  {
    aLength = 1.250;
    goPos = 10;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[10] && sticky <= maxY[10])
  {
    aLength = 1.375;
    goPos = 11;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[11] && sticky <= maxY[11])
  {
    aLength = 1.500;
    goPos = 12;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[12] && sticky <= maxY[12])
  {
    aLength = 1.625;
    goPos = 13;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[13] && sticky <= maxY[13])
  {
    aLength = 1.750;
    goPos = 14;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
    //Serial.println("MOVING");
  }
  if (sticky > minY[14] && sticky <= maxY[14])
  {
    aLength = 1.875;
    goPos = 15;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[15] && sticky <= maxY[15])
  {
    aLength = 2.000;
    goPos = 16;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[16] && sticky <= maxY[16])
  {
    aLength = 2.125;
    goPos = 17;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[17] && sticky <= maxY[17])
  {
    aLength = 2.250;
    goPos = 18;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[18] && sticky <= maxY[18])
  {
    aLength = 2.375;
    goPos = 19;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[19] && sticky <= maxY[19])
  {
    aLength = 2.500;
    goPos = 20;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[20] && sticky <= maxY[20])
  {
    aLength = 2.625;
    goPos = 21;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[21] && sticky <= maxY[21])
  {
    aLength = 2.750;
    goPos = 22;
    do {
      actuatorPosition(goPos);
    }   while (goPos != lastGoPos);
  }
  if (sticky > minY[22] && sticky <= maxY[22])
  {
    aLength = 2.875;
    goPos = 23;
    do {
      actuatorPosition(goPos);
    }
    while (goPos != lastGoPos);
  }
  if (sticky > minY[23] && sticky <= maxY[23])
  {
    aLength = 3.000;
    goPos = 24;
    do {
      actuatorPosition(goPos);
    }
    while (goPos != lastGoPos);
  }
  else {
    //do nothing
  }
  delay(20);

  do {
    actuatorPosition(goPos);
  }
  while (goPos != lastGoPos);

  //Serial.println(goPos);
  lastGoPos = goPos;
  
  Serial.println("pulses = ");
  Serial.println(pulses);
//  Serial.println("goPos = ");
//  Serial.println(goPos); 
}
int rawStickX = analogRead(A0);
int rawStickY = analogRead(A5);

The initial values are useless, as you have no idea what will happen when the compiler generates the code to initialize those variables. It obviously can not call the functions to see what they will return. Even if the function were called when the Arduino was booted, the hardware is not ready, so you will get garbage.

int sticky = map(rawStickY, 100, 900, 0, 2400);

Mapping garbage will NOT do anything useful.

  pinMode(rawStickX, INPUT);
  pinMode(rawStickY, INPUT);

Setting the pin mode on an analog pin will NOT do what you think it will. Don't do that.

    return pulses; //make pulses global

pulses is already global, as is copyCount. They are different types, so assigning copyCount to pulses doesn't make sense. Nor does returning a global variable. Nor does the comment.

Returning a value from a void function doesn't make sense, either.

It is as we suspected. Once you start moving the actuator, you do not read the joystick(s), so moving the joystick while the actuator is moving will do no good. When the actuator stops, the the joystick is read, and the actuator lurches off to a new position.

You need to ditch all the do/while loops. Read the joystick. Move the actuator a little ways towards the joystick position, and read again, as quickly as possible. Or, start the actuator moving at some speed that is proportional to the discrepancy between the desired position and the actual position. Adjust the speed as the joystick moves the desired position.

Hi,
How do you want the actuator to react to the joystick.

  1. As you push the joystick more forward or backward, the faster forward or backward the actuator moves. If you return the joystick to the middle the actuator stops?
    OR
  2. As you push the joystick forward of backwards the actuator position track the position of the joystick. If you return the joystick to the middle the actuator returns to its middle position?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
How do you want the actuator to react to the joystick.

  1. As you push the joystick more forward or backward, the faster forward or backward the actuator moves. If you return the joystick to the middle the actuator stops?
    OR
  2. As you push the joystick forward of backwards the actuator position track the position of the joystick. If you return the joystick to the middle the actuator returns to its middle position?

Thanks.. Tom... :slight_smile:

In this sketch, I want the actuator to track with joystick position, farthest forward joystick position being 3 inches of extension. Farthest back being zero extension, but I only want to allow the actuator to retract from an extended position when the button is pressed.

Eventually I want to control 2 actuators, 1 on the +y axis, the other on the -y axis with both actuators being fully retracted when the joystick is in the center.

Thanks for your help Tom :slight_smile:

spyguy518:
In this sketch, I want the actuator to track with joystick position, farthest forward joystick position being 3 inches of extension. Farthest back being zero extension, but I only want to allow the actuator to retract from an extended position when the button is pressed.

Does that mean that you can move the joystick to a new position but nothing actually happens until a button is pressed?

I suspect that will make a system that is very difficult to control because the user gets no feedback about where he has moved the joystick to.

...R

Robin2:
Does that mean that you can move the joystick to a new position but nothing actually happens until a button is pressed?

No, only limit movement in the reverse direction, from a previously extended position. Further extension is not limited. To retract, the user simply presses the button, and then moves the joystick backwards.

So, move the joystick forward, and the actuator follows. Move the joystick back, and the servo does not follow. Move the joystick forward again, and the actuator extends more. Is THAT what you want?

@PaulS why pinmode on analog pins is not good idea?

surepic:
@PaulS why pinmode on analog pins is not good idea?

An answer to Reply #10 is far more important.

...R

PS.. apologies to all. I thought @surepic was the OP.

PaulS:
So, move the joystick forward, and the actuator follows. Move the joystick back, and the servo does not follow. Move the joystick forward again, and the actuator extends more. Is THAT what you want?

Kinda, If you move back, actuator only follows if button is pressed.

Hi,

In this sketch, I want the actuator to track with joystick position, farthest forward joystick position being 3 inches of extension. Farthest back being zero extension, but I only want to allow the actuator to retract from an extended position when the button is pressed.

Eventually I want to control 2 actuators, 1 on the +y axis, the other on the -y axis with both actuators being fully retracted when the joystick is in the center.

Can you read that again?
If the joystick is fully back with the actuator zero/fully back.
Then fully retracted with joystick in centre is not logical.

Center joystick is center actuator position.

S0 I understand the button is a safety that must be pressed to allow the USER to operate the joystick to RETACT the actuator, it doesn't cause an automatic retraction.

What is the application?

Tom... :slight_smile:

spyguy518:
Kinda, If you move back, actuator only follows if button is pressed.

"Kinda" is a concept that is totally alien to Arduino programming. You need to be precise if you want to get a program that works.

I suggest you write a new description of all the behaviours that you want your system to have - leaving nothing out. It is pointless starting to write a program before you have a very clear idea of what it should do.

If you are not sure what you want then write some short test programs to explore the options.

...R