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);
}