I need help with the code for a stepper motor fuel gauge

I'm making a fuel gauge for a secondary fuel tank in my truck. I'm using a bipolar stepper out of the instrument panel of my truck. I have a switch that I hold, to transfer fuel from the secondary tank to the primary. I am going to wire my arduino so that when I press the switch, the arduino gets power, and displays the fuel level via the stepper motor in my instrument cluster.

I found some sketches that allow me to control the stepper with the float sensor on the fuel pump. What I need to know is:

  1. how to program a startup routine where the stepper will rotate some number of steps counter-clockwise to its mechanical stop, which would be the "empty" location on the gauge.

  2. have the voltage value from the analog input from the float sensor be translated into a specific number of steps. Meaning if the analog value is... 50, the stepper will always move to 100 steps clockwise. The sweep range of the stepper is roughly 320 degrees, but the gauge can ONLY travel 90degrees.

Is there a way to map a specific analog value to a specific number of steps?

Any help would be greatly appreciated.

The 'map()' function.

If both ranges start at zero, you can use simple arithmetic to scale it.

Please tell us what the actual ranges are.

Ok, so I mapped my analog values so that instead of being 343-929, they are 0-100. I can see it in the serial monitor. So how to... "assign" those one hundred values to the number of steps?

Please post the code in question.

So how to... "assign" those one hundred values to the number of steps?

Do you in fact mean, "how to make the stepper move that number of steps"?

#include <Stepper.h>  // Include the header file


// change this to the number of steps on your motor

#define STEPS 2000


// create an instance of the stepper class using the steps and pins

Stepper stepper(STEPS, 4, 5, 6, 7);


int Pval = 0;

int potVal = 0;


void setup() {

  Serial.begin(9600);

  stepper.setSpeed(800);
}


void loop() {


  potVal = map(analogRead(A0), 343, 926, 0, 100);

  if (potVal > Pval)

    stepper.step(1);

  if (potVal < Pval)

    stepper.step(-1);


  Pval = potVal;


  Serial.println(Pval);  //for debugging
}

Please edit and add code tags. Post the entire sketch. Give us a report on the behaviour of the program, as it stands.

When powered up I can move the stepper with the float. It's a little sketchy, no pun intended. If I move the float too quickly, the stepper won't always move immediately. As a result, it won't always return the to same place twice, as I move the float around.

When you move the pot, it can move multiple steps. But you only ever step by 1, -1. So it can't catch up. Try

void loop() {
  potVal = map(analogRead(A0), 343, 926, 0, 100);
  if (potVal != Pval)
    stepper.step(potVal - Pval);

  Pval = potVal;
  Serial.println(Pval);  //for debugging
}

That seems to make the movement more erratic.

Here's how I had it before:

Here's with your tweaks:

IDK, but I worry because the sketch can't tell when the steps have been completed. That would throw things out of synch. Maybe someone else has an idea. Maybe

void loop() {
  potVal = map(analogRead(A0), 343, 926, 0, 100);
  if (potVal != Pval) {
    stepper.step(potVal - Pval);
    Pval = potVal;
  }
  Serial.println(Pval);  //for debugging
}

hmm. That's what I was wondering if there was a way to lock the analog reading and the number of steps together.

When the thing is in use, It isn't going to be moving fast at all. The pump only moves 1.5gal/min. So it MAY not be an issue..... But if the fuel is sloshing around, weird things would happen.

Did my update work?

no dice

and by that I mean, it's acting like your first tweak. jumpy, and erratic.

I think it is because the program can't tell when a step is complete. Program execution continues after the command is issued, it doesn't wait for completion before processing another analog reading. That's my theory, anyway...

Does the library have a function you can call, to find out if the stepper is moving or stationary?

I wish I could duplicate your setup here to test...

hmm. Well, do you know how to make the stepper "zero" itself on startup? This particular one has no sensors in it. It just has a mechanical stop. All I would need for it to do at startup would be to go... 100 steps to counter-clockwise. Any ideas?

It's usually done with additional hardware, like a limit switch.

I don't know about the moving or stationary thing. The stepper is dumb. No way for it to tell the controller anything.

That's the point. The motor doesn't but the software does because it controls the stepping. Got a link to the library?

Here's where I found the code I've been messing with:

The library? Is that the <stepper.h> at the top of the sketch? I got that from the arduino IDE.

Really? You didn't install any library?