Hi again, I am trying to get this gauge code to work. I am having difficulty understanding why the gauge behaves the way it does when I make changes in the void loop(void)...
one question is why is it void loop (void) and not void loop()?
Library used
clearwater SwitecX25 from github
Parts used:
Adafruit stepper x27.168 gauge and needle
Adafruit DC/Stepper Motor Driver Breakout Board
Nano
Code from the library example, tweaked to work as intended, use serial monitor, type in value, needle moves to typed value
//----------------------------------------------------------------------
// https://github.com/clearwater/SwitecX25
//
// This is an example of using the SwitchX25 library.
// It zero's the motor, sets the position to mid-range
// and waits for serial input to indicate new motor positions.
//
// Open the serial monitor and try entering values
// between 0 and 944.
//
// Note that the maximum speed of the motor will be determined
// by how frequently you call update(). If you put a big slow
// serial.println() call in the loop below, the motor will move
// very slowly!
//----------------------------------------------------------------------
#include <SwitecX25.h>
#define STEPS (180*2) // reduced to little over 90* + few degrees below 0 tick on gauge
SwitecX25 motor1(STEPS,4,5,6,7); // For motors connected to digital pins 4,5,6,7
void setup(void)
{
// run the motor against the stops
delay (500);
motor1.zero();
delay (300);
motor1.zero(); // Found consistently on reset the needle does not reset to zero, doing this 2nd reset() it always goes to left endstop and back to zero
motor1.setPosition(60); // Moves needle towards the "Empty" (first) tick mark on gauge on startup or reset
Serial.begin(9600);
Serial.print("Enter a step position from 0 through ");
Serial.print(STEPS-1);
Serial.println(".");
Serial.println("60 = 0, 123 = 1/4, 186 = 1/2, 249 = 3/4, 312 = Full");
}
void loop(void)
{
static int nextPos = 0; // Sets gauge to bottom tick mark on gauge after zero() and motor1.setPosition(60) commands
motor1.update(); // the motor only moves when you call update
if (Serial.available())
{
char c = Serial.read();
if (c==10 || c==13) // I dont understand how this statement works
{
motor1.setPosition(nextPos);
nextPos = 0;
}
else if (c>='0' && c<='9') // I dont understand how this statement works
{
nextPos = 10*nextPos + (c-'0');
}
}
}
/*
Values to enter to match marks printed on gauge Face to match with current needle installed
312 = full +63
249 = 3/4 +63
186 = 1/2 +63
123 = 1/4 +63
60 = empty
*/
So this works just fine, next thing I've been trying for hours is how to get the gauge to perform a "demo" ... go to step 60, wait period of time, go to step 123, wait period of time, go to 249.... then go backwards...
I cannot figure out how to do this...
Next code below it will reset the gauge needle, and goes to position 60 (which is the 1st tick mark on the gauge. tick mark I'm referring to is on the gauge, theirs 4 of them. 1st mark represents empty, 2nd mark represents 1/4, 3rd mark represents 3/4 and 4th mark represents Full.
#include <SwitecX25.h>
#define STEPS (180*2) // reduced to little over 90* + few degrees below 0 tick on gauge
SwitecX25 motor1(STEPS,4,5,6,7); // For motors connected to digital pins 4,5,6,7
void setup(void)
{
// run the motor against the stops
delay (500);
motor1.zero();
delay (300);
motor1.zero(); // Found consisantly on reset if gauge is past 1/2 needle does not reset to zero, doing this it always goes to left endstop and back to zero
motor1.setPosition(60); // Moves needle towards the "Empty" (first) tick mark on gauge on startup or reset
Serial.begin(9600);
Serial.print("Enter a step position from 0 through ");
Serial.print(STEPS-1);
Serial.println(".");
Serial.println("60 = 0, 123 = 1/4, 186 = 1/2, 249 = 3/4, 312 = Full");
}
void loop(void)
{
static int nextPos = 0; // Sets gauge to bottom tick mark on gauge after zero() and motor1.setPosition(60) commands
motor1.update(); // the motor only moves when you call update
}
if i add delay(500); and then request new position, it moves the needle at 1 step at a time till it reaches the value:
#include <SwitecX25.h>
#define STEPS (180*2) // reduced to little over 90* + few degrees below 0 tick on gauge
SwitecX25 motor1(STEPS,4,5,6,7); // For motors connected to digital pins 4,5,6,7
void setup(void)
{
// run the motor against the stops
delay (500);
motor1.zero();
delay (300);
motor1.zero(); // Found consisantly on reset if gauge is past 1/2 needle does not reset to zero, doing this it always goes to left endstop and back to zero
motor1.setPosition(60); // Moves needle towards the "Empty" (first) tick mark on gauge on startup or reset
Serial.begin(9600);
Serial.print("Enter a step position from 0 through ");
Serial.print(STEPS-1);
Serial.println(".");
Serial.println("60 = 0, 123 = 1/4, 186 = 1/2, 249 = 3/4, 312 = Full");
}
void loop(void)
{
static int nextPos = 0; // Sets gauge to bottom tick mark on gauge after zero() and motor1.setPosition(60) commands
motor1.update(); // the motor only moves when you call update
delay (500); // causes gauge to microstep 1 at a time till it reaches position 123
motor1.setPosition(nextPos);
nextPos = 123;
motor1.update();
}
end goal would be to understand this step, then use a potentiometer and learn the map() to make the gauge move according the analog input, just like the Stepper/Knob Control example. Side note, when I upload that sketch, the stepper motor is very noisy when compared to switecx25 library.
Any pointers or links will be much appreciated, I think I've searched and came full circle lol..
Thanks
Clint