c_cpp cs_stepper_01.ino
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp cs_stepper_01.ino相关的知识,希望对你有一定的参考价值。
// bind stepper with schmalzhaus.com easydriver to poti
#define trigPin 13
#define echoPin 12
#define DIR_PIN_01 10
#define STEP_PIN_01 11
#define DIR_PIN_02 8
#define STEP_PIN_02 9
#define DIR_PIN_03 7
#define STEP_PIN_03 6
#define DIR_PIN_04 4
#define STEP_PIN_04 5
int servopin = 3;
long dist = 0;
int dir = LOW;
int steps = 0;
int sesormax = 0;
int sensormin = 10000;
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
void setup() {
Serial.begin(9600);
pinMode(DIR_PIN_01, OUTPUT);
pinMode(STEP_PIN_01, OUTPUT);
pinMode(DIR_PIN_02, OUTPUT);
pinMode(STEP_PIN_02, OUTPUT);
pinMode(DIR_PIN_03, OUTPUT);
pinMode(STEP_PIN_03, OUTPUT);
pinMode(DIR_PIN_04, OUTPUT);
pinMode(STEP_PIN_04, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(servopin, OUTPUT);
}
void loop() {
// read from the poti
// map the value to the steps of the stepper
soundsensor();
if (dist > sensormax ){
sensormax = dist;
}
if(dist < sensormin){
sensormin = dist;
}
float max_rotation_speed = 0.5;
float min_rotation_speed = 0.01;
float speeed = map(dist, sensormin, sensormax, min_rotation_speed, max_rotation_speed);
rotate_from_sensor(steps, speeed, LOW);
rotate_continous(steps, 0.1, LOW);
steps++;
}
void rotate_from_sensor(int steps, float speed, int dir) {
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
digitalWrite(DIR_PIN_01, dir);
digitalWrite(DIR_PIN_02, dir);
float usDelay = (1 / speed) * 70;
for (int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN_01, HIGH);
digitalWrite(STEP_PIN_02, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN_01, LOW);
digitalWrite(STEP_PIN_02, LOW);
delayMicroseconds(usDelay);
}
}
void rotate_continous(int steps, float speed, int dir) {
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
digitalWrite(DIR_PIN_03, dir);
digitalWrite(DIR_PIN_04, dir);
float usDelay = (1 / speed) * 70;
for (int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN_03, HIGH);
digitalWrite(STEP_PIN_04, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN_03, LOW);
digitalWrite(STEP_PIN_04, LOW);
delayMicroseconds(usDelay);
}
}
//void rotate(int steps, float speed, int dir, int dirpin, int steppin) {
// //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//
//
// digitalWrite(dirpin, dir);
//
// float usDelay = (1 / speed) * 70;
//
// for (int i = 0; i < steps; i++) {
//
// digitalWrite(steppin, HIGH);
// delayMicroseconds(usDelay);
//
// digitalWrite(steppin, LOW);
// delayMicroseconds(usDelay);
// }
//}
void soundsensor() {
long duration, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
dist = cm
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
以上是关于c_cpp cs_stepper_01.ino的主要内容,如果未能解决你的问题,请参考以下文章