Hey guys,
as part of my first real in depth project i want to incorporate an analog looking gauge that will show the angle of a hydrofoil (aka 'wedge') on my boat.
I started with the 'motorknob' example in the stepper library and am modifying it to meet my needs.
The hydrofoil moves from a stowed position out of the water(0 degrees) to a 'functional' angle that is about 75-90 degrees. those 15 degrees is what i want to precisely control and display it's position on my gauge. That leaves 74 degrees that is relatively useless on my gauge (not completely useless as i do want to know it's approx position while it's getting deployed). I am measuring the angle of my hydrofoil with a linear 10k pot atached to it's pivot point.
This is the hydrofoil:
and here is how it'll be mounted on my boat:
Here is a video of the actual gauge's layout (the plastic piece is just a proxy for the real wedge):
the 'deploy' area is only useful to show me that the wedge is moving from 0 degrees to 75. there is no need for me know the precise angle. So in my code i figured i could remap my sensor's values so that when the pot is is more the 75 degrees it'd 'sweep' a larger area on the gauge, increasing accuracy for those 15 degrees. On the flip side, when it's less the 75 it is represented by a much smaller area of the overall sweep so i adjusted the sensor output with another map function. The problem is, i start to lose 'sync' when the pot sweeps back and fourth.
/*
* WedgeGauge
*/
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS (315)
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 4, 5, 6, 7);
// the previous reading from the analog input
int previous = 0;
const int numReadings = 10;
int encoderbutton =0; //for future use
int mtcsetup =0; //for future use
int speedthreshold =31; //for future use
int wedgemin=480;
int wedgemax =520;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int averageold = 0;
int inputPin = A0;
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sweep =0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(80);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
stepper.step(-650); //sets the initial home position of the gauge
//Serial.begin (9600);
}
void loop()
{
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
sweep = analogRead(inputPin);
if(sweep >= wedgemin){ //functional wedge angle, this is where the pot is when the hydrofoil is at 75 degrees
sweep = map(sweep,wedgemin,wedgemax, 214, 483); //the remapped values are representative of where the gauge's needle is.
// at 214 it is where the 'deploy' sweep transitions to the 'wedge size' position
}
else { //wedge depoly and retract. non functional angle
sweep = map(sweep, 0,wedgemin, 0,214);
}
readings[index] = sweep;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// get the sensor value
int val = average;
if((average >= (averageold+3)) || (average <= (averageold-3))) { //reduces the amount of needle jitter by only move the stepper if the interger changes by 3 or more
// sensor reading
stepper.step(val - previous);
// remember the previous value of the sensor
previous = val;
averageold =average;
}
//Serial.print ("average: ");
//Serial.print (average);
//Serial.print (" actual: ");
//Serial.println (analogRead(inputPin));
/* //wedgeSetup below
if(mtcsetup == 1){ // record the maximum sensor value
if (sweep > wedgemax) {
wedgemax = sweep;
}
if (encoderbutton == HIGH){ // pressing the encoder button will set the wedges highest functional angle
wedgemin =sweep;
}
}
//*/
}
I hope i was clear enough.
i know the issue is in the remap portion but i just don't know an alternative to fix the sync issue. any ideas on how to correctly code for something like this?
thanks in advance,
'scott