|
Step motorlar va Arduino - misol kodlari
|
bet | 5/13 | Sana | 09.02.2024 | Hajmi | 12,35 Mb. | | #153839 |
Bog'liq stepper word Step motorlar va Arduino - misol kodlari
Shunga qaramay, endi biz Arduino-ni dasturlashda davom etishimiz yoki Arduino platasi bilan step motorini boshqarish uchun bir nechta misol kodlarini ko'rib chiqishimiz mumkin.
Kutubxonadan foydalanmasdan qadam motorini boshqarishning juda oddiy misol kodidan boshlaylik.
Misol kod 1
/*
* Basic example code for controlling a stepper without library
*
* by Dejan, https://howtomechatronics.com
*/
// defines pins
#define stepPin 2
#define dirPin 5
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 800; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(700); // by changing this time delay between the steps we can change the rotation speed
digitalWrite(stepPin,LOW);
delayMicroseconds(700);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 1600; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}
Kod tavsifi:
Bu erda faqat STEP va DIR pinlari qaysi pin raqamiga ulanganligini aniqlashimiz va ularni chiqish sifatida belgilashimiz kerak. Loopda, avvalo, Yo'nalish pin holatini YUQOR qilib, dvigatelning aylanish yo'nalishini o'rnatamiz. Keyin "for" halqasidan foydalanib, biz STEP piniga 200 ta impuls yuboramiz, bu esa vosita to'liq bosqichli rejimda ishlashini hisobga olsak, to'liq aylanishni amalga oshiradi. Impulslar oddiygina STEP pinining YUQORI holatini PASTga almashtirish orqali hosil bo'ladi, ular orasida biroz kechikish mavjud. Bu vaqtni kechiktirish aslida aylanish tezligini belgilaydi. Agar biz uni tushirsak, aylanish tezligi oshadi, chunki qadamlar tezroq sodir bo'ladi va aksincha.
Keyin biz aylanish yo'nalishini o'zgartiramiz va boshqa "for" pastadiridan foydalanib, biz dvigatelni ikkita to'liq aylanish aylanishiga olib keladigan 400 ta impuls yuboramiz. Biroq, agar biz drayverning mikrosteplash rejimini o'zgartiradigan bo'lsak, deylik, chorak qadam, ya'ni motor hozirda 800 qadamga ega bo'ladi, birinchi halqa dvigatelni faqat 90 daraja, ikkinchi halqa esa faqat yarim aylanishga olib keladi.
Misol kod 2
Mana yana bir oddiy misol, potansiyometr yordamida step vosita tezligini boshqarish.
Buning uchun potentsiometrni Arduino-ga ulash va analogRead() funktsiyasidan foydalanib uning qiymatini o'qish kifoya.
/*
Basic example code for controlling a stepper without library
by Dejan, https://howtomechatronics.com
*/
// defines pins
|
| |