Dear All
I was built a good working DC(wiper) motor position control with encoder in a past.
This sketch allows you to have a multiple positions, but this time I use only 2.
#include <Button.h>
#include <Encoder.h>
const int PWM0 = 6;
const int PWM1 = 5;
const int BTN_MEM_PIN[] = {7,8};
const int READY = 13;
int currentPos = 0;
int newPosition = 0;
int runningSpeed = 0;
long distanceTogo = 0;
Encoder myEnc(2, 3);
long oldPosition = -999;
long targetPosition = 0;
#define ACCURACY 7
#define DEBOUNCE_MS 20
#define PULLUP true
#define INVERT true
#define motorSpeed 255
#define motorSpeed1 70
#define motorSpeed2 30
Button btnPos1(BTN_MEM_PIN[0], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos2(BTN_MEM_PIN[1], PULLUP, INVERT, DEBOUNCE_MS);
long memPosition[] = {0,0};
void setup() {
pinMode(PWM0, OUTPUT);
pinMode(PWM1, OUTPUT);
analogWrite(PWM0, 0);
analogWrite(PWM1, 0);
pinMode(READY, OUTPUT);
Serial.begin(9600);
}
void loop() {
memPosition[0] = 0;
memPosition[1] = -550;
btnPos1.read();
btnPos2.read();
if(btnPos1.wasReleased()) {
Serial.println("btnPos1");
targetPosition = memPosition[0] ;
}
if(btnPos2.wasReleased()) {
Serial.println("btnPos2");
targetPosition = memPosition[1] ;
}
long newPosition = myEnc.read();
distanceTogo = (abs(targetPosition - newPosition));
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
if( newPosition != targetPosition) {
Serial.print("Target/Actual:");Serial.print(targetPosition);Serial.print(" / ");Serial.print(newPosition);Serial.print(" [");Serial.print(abs(targetPosition - newPosition));Serial.println("]");
Serial.println(distanceTogo);
if(targetPosition < newPosition) {
retractActuator();
}
if(targetPosition > newPosition) {
extendActuator();
}
if( (targetPosition == newPosition) || abs(targetPosition - newPosition) <= ACCURACY) {
stopActuator();
}
if(distanceTogo <= 50 ) {
runningSpeed = motorSpeed2;
}
if (51 <= distanceTogo && distanceTogo <= 150) {
runningSpeed = motorSpeed1;
}
if(distanceTogo >= 151){
runningSpeed = motorSpeed;
}
}
}
void retractActuator() {
analogWrite(PWM0, 0);
analogWrite(PWM1,runningSpeed);
digitalWrite(READY, LOW);
}
void extendActuator() {
analogWrite(PWM0,runningSpeed);
analogWrite(PWM1, 0);
digitalWrite(READY, LOW);
}
void stopActuator() {
analogWrite(PWM0, 0);
analogWrite(PWM1, 0);
digitalWrite(READY, HIGH);
}
Now I have a new idea to change the encoder on hall effect sensor 360 degrees. My new project is the tool turret for lathe. I need to divide a circle on 8 positions. I find some sketch wich is able to convert analog input from hall sensor on digital(1024).
Summary the things. I need to have 8 buttons. Each button have specific angle like 45, 90, 135 etc.
If I push(or release) a button the dc motor run to target, like in a previous sketch with encoder.
/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue ;
// print out the value you read:
Serial.println(voltage);
}
Please help to me how can I use the hall sensor instead of encoder.
Thanks in advance.