10524| 6
|
[求助] 向大佬们求助~!步进电机与超声波传感器如何同时使用 |
我的步进电机用的是42步进电机FIT0349,型号为42BYGH40-18-22A-C5.18。使用了步进电机驱动扩展板DRI0023。 超声波传感器使用的是SRF05。 想要的效果是超声波测得的距离值,控制灯带亮度与步进电机转速。但发现无法将步进电机程序与超声波传感器编入一个文件中:'(向大佬们求助,看看我哪里代码错了。 超声波测距仪程序:(使用的是MODE2,trig与echo同pin) [mw_shl_code=cpp,true]//import #include <NewPing.h> //sonar #include <Adafruit_NeoPixel.h> //LED //define pins #define N_TRIG_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define N_ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. #define SONAR_NUM 1 #define PING_INTERVAL 33 #define LED_PIN 3 // Pin tied to LED boolean human = 0; int pingTimer; // Holds the times when the next ping should happen for each sensor. int cm; // Where the ping distances are stored. uint8_t currentSensor = 0; // Keeps track of which sensor is active. //set NewPing sonar = NewPing(N_TRIG_PIN, N_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. Adafruit_NeoPixel strip = Adafruit_NeoPixel(20, LED_PIN, NEO_GRB + NEO_KHZ800); void setup(){ Serial.begin(115200); pinMode(LED_PIN, OUTPUT); pingTimer = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting. strip.begin(); strip.show(); } void loop(){ if (millis() >= pingTimer) { // Is it this sensor's time to ping? pingTimer += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged. sonar.timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance). cm = 0; // Make distance zero in case there's no ping echo for this sensor. sonar.ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo). } } //handle functions void echoCheck() { // If ping received, set the sensor distance to array. if (sonar.check_timer()) { cm = sonar.ping_result / US_ROUNDTRIP_CM; pingResult(); } } void pingResult() { // Sensor got a ping, do something with the result. // The following code would be replaced with your code that does something with the ping result. if(cm > 100){ cm = 0; } human = cm; Serial.println(human); if(human == 0) { for(int i=0;i<20;i++){ strip.setPixelColor(i, 100, 100, 100); strip.show(); } } else{ for(int i=0;i<5;i++){ int d = int(255 - cm*1.55); strip.setPixelColor(i, d, d, d); strip.show(); } for(int j=5;j<20;j++){ if(cm < 3){ cm = 0; } strip.setPixelColor(j, cm, cm, cm); strip.show(); } } Serial.println(cm); } [/mw_shl_code] 步进电机程序: [mw_shl_code=cpp,true]int Mdirpin = 7; //电机Y方向引脚 int Msteppin = 6; //电机Y驱动引脚 int Men= 8; //电机Y使能引脚 void setup() { pinMode(Mdirpin,OUTPUT); pinMode(Msteppin,OUTPUT); pinMode(Men,OUTPUT); digitalWrite(Men, 0); //低电平使能 } void loop() { stepper(); } void stepper(){ digitalWrite(Mdirpin, 1); for(int j=0;j<=10000;j++){ digitalWrite(Msteppin, 1); //上升沿步进 digitalWrite(Msteppin, 0); delay(1); } }[/mw_shl_code] 将两个程序放在一起时,就会有个不运行。估计是因为delay的缘故,但不知道怎么改:'(再次求助大佬们!!多谢~~ |
步进电机
步进电机拓展板
超声波距离传感器
在一个群里问的,有人说用状态机,是能把两个程序勉强放在一起。但是会出现步进电机噪音很大,会失步,有时还会跳过传感器的程序 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed