Hey, this is my first post.
I need an end switch for a project involving a ultrasonic range finder for controlling a stepper motor, and im kind of troubled about the programing part.
The end switch would be for stopping the stepper once it reaches it and holding its position for as long as the reading is = to certain value or higher.
the electronics are a MaxBotix Ultrasonic Range Finder - MB1010 LV-MaxSonar-EZ1, a TB6560 stepper driver, and an arduino uno
this is the code as far as i've got it
float cm = 0.00;
float pcm = cm; //cm previo = cm actual
int SonarPin = A0;
int sensorValue;void setup() {
pinMode(SonarPin, INPUT);
Serial.begin(9600);
// Stepper config
stepper.setMaxSpeed(5000.0);
stepper.setSpeed(1000);
stepper.setAcceleration(9000.0);
}void loop() {
readSonar();
if (abs(cm - pcm > 5)) { //calcula delta entre lectura actual y lectura anterior
gotoPosition(cm); //si es menor a 10, va a la posición leída
}
}void readSonar() {
pcm = cm;
sensorValue = analogRead(SonarPin); //lee el valor de entrada del sensor
delay(10);
Inch = (sensorValue * 0.497); //calculo en pulgadas
cm = Inch * 2.54; //calculo en cms
}void gotoPosition(float target) {
float newPos = map(target, 0, 100, 0, 8000);
Serial.print("cm = ");
Serial.print(cm);
Serial.print(" - new position = ");
Serial.println(newPos);
stepper.runToNewPosition(newPos);
}