Hi, I need help cleaning up my code for a DC motor with Arduino Mega 2560, with Monster Moto shield from Sparkfun as my motor controller to read, store, and increment a value each rotation. I am using a regular potentiometer attached to the end of the DC motor to read its position. (I know this is a very crude way to read this) My goal is to type in a number and have my motor rotate a certain number of times until the position is reached. I have successfully done it in the CW direction and CCW direction, however they are separate codes and I need to integrate them. CurrentPoint is the tag i've assigned to my potentiometer and i'm using an analogread command to convert it to 0-1023 so as my motor rotates the potentiometer and any number below is read it will increment "Rotations." I hope i've been descriptive enough.
My question is there a better way to increment "Rotations" other than using this bit of code below:
[if (CurrentPoint == 10 || CurrentPoint == 11 || CurrentPoint == 12 || CurrentPoint == 13 ||
CurrentPoint == 14 || CurrentPoint == 15 || CurrentPoint == 16 || CurrentPoint == 17 ||
CurrentPoint == 18 || CurrentPoint == 19 || CurrentPoint == 20 || CurrentPoint == 21 ||
CurrentPoint == 22 || CurrentPoint == 23 || CurrentPoint == 24 || CurrentPoint == 25 ||
CurrentPoint == 26 || CurrentPoint == 27 || CurrentPoint == 28 || CurrentPoint == 29)
{
Rotations = Rotations + 1;// Numbers are specified as the count numbers.
//Meaning (Rotations) will increment when counter hits #s on every rotation.
digitalWrite(LEDpin, LOW);
delay(100);
digitalWrite(LEDpin, HIGH);
delay(200);
digitalWrite(LEDpin, LOW);
delay(100);
Serial.println("OOOOOOOOOOOOOOOOOOOOOO"); // The O's here are so I can see my "Rotations"
Serial.println("OOOOOOOOOOOOOOOOOOOOOO"); // tag as it prints to the Serial Monitor better.
Serial.println("OOOOOOOOOOOOOOOOOOOOOO");
Serial.println("OOOOOOOOOOOOOOOOOOOOOO");
Serial.print(" # ");
Serial.println(Rotations);
Serial.println("OOOOOOOOOOOOOOOOOOOOOO");
Serial.println("OOOOOOOOOOOOOOOOOOOOOO");
Serial.println("OOOOOOOOOOOOOOOOOOOOOO");
Serial.println("OOOOOOOOOOOOOOOOOOOOOO");
}
]
Attached is my full code if this isn't descriptive enough.
PaulS:
My question is there a better way to increment "Rotations" other than using this bit of code below
Yes.
if(CurrentPoint >= 10 && CurrentPoint <= 29)
{
Thanks! That is better than my current coding but I am having problems when I integrate my CW coding with my CCW coding. They look nearly identical coding wise just different numbers. I can attach my CCW coding if you like. For both codes though I use the CurrentPoint >=10 && CurrentPoint <= 29 to add/subtract to my Rotations tag as the motor makes a complete rotation. When I integrate them and type in a desired position it will make 1 rotation and when it reaches the numbers between 10-29 it prints #1 and immediately prints #0 afterwards. I know why it's doing this I just need to know what I could possibly replace (CurrentPoint >= 10 && CurrentPoint <= 29) with so that it can add/subtract based on what direction it is going. Right now it is stuck in an infinite loop of rotating CW and printing #1 to serial monitor and rotating CCW and printing #0 right after.
You need to keep track of the direction of rotation independent of the current position. The current position being in some range tells you that you need to increment or decrement the number of rotations. The direction of rotation tells you whether to increment or decrement.
Now, you can use the current point and the previous point to know the direction of rotation.
What I don't understand is why the range from 10 to 29 means that a rotation has been completed, regardless of which way the shaft is rotating.
How is the potentiometer connected to the shaft? Does the value climb from 0 to 1023 and then drop again as the shaft rotates one way? Does the value drop from 1023 to 0 and then jump back to 1023 as it rotates the other way?
Now, you can use the current point and the previous point to know the direction of rotation.
What I don't understand is why the range from 10 to 29 means that a rotation has been completed, regardless of which way the shaft is rotating.
How is the potentiometer connected to the shaft? Does the value climb from 0 to 1023 and then drop again as the shaft rotates one way? Does the value drop from 1023 to 0 and then jump back to 1023 as it rotates the other way?
Question: Do I need to declare "RotatingClockwise" as a new variable, or is that a built in command for Arduino?
I had to use the range of 10-29 because my potentiometer is very junky and only reads 0-1023 over a 90 degree span and when the motor is moving fast and is printing the location of the potentiometer to the serial monitor it had a hard time keeping up so it skips numbers. The faster you move, the more numbers skipped.
The potentiometer is connected to the motor by way of a rubber tubing that shrinks when heat is applied. The motor is attached to one end, and the potentiometer to the other. It is quite crude like I said. It's just a simple mock up of a bigger project that I do not have the parts for yet.
Yes the number will climb from either 0-1023 or 1023-0 depending on the direction of the motor's movement. If it ends on 0 it will pick back up with 1023. When it reaches beyond the 90 degree turn limit on the pot it drops until it turns the remaining 270 degrees and reads nothing and then picks up again when it reaches the beginning of the 90 degrees again. It's a junky set up, I know. My employer set this up so I could learn Arduino.