الكود البرمجي :
قم بكتابة الكود البرمجي كما يلي :
#define MOTOR_IN1 12
#define MOTOR_IN2 13
void motor_forward(void); // a function that will be called to rotate it clockwise
void motor_reverse(void); // a function that will be called to totate it counter-clockwise
void motor_stop(void); // a function that will be called to stop the rotation
void setup() {
pinMode(MOTOR_IN1, OUTPUT); // set the first pin of the relay as output
pinMode(MOTOR_IN2, OUTPUT); // set the 2nd pin of the relay as output
}
void loop() {
motor_forward(); // move forward/clockwise
delay(3000); // keep rotating cw for 3 seconds
motor_stop(); // stop rotating
delay(3000); // stand still for 3 seconds
motor_reverse(); // reverse the rotation direction/ccw
delay(3000); // keep rotating ccw for 3 seconds
motor_stop(); // stop rotating
delay(3000); // stand still for 3 seconds
}
void motor_forward(void) // the function that will cause the motor to rotate cw
{
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
}
void motor_reverse(void) // the function that will cause the motor to rotate ccw
{
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, HIGH);
}
void motor_stop(void) // the function that will cause the motor to stop rotating
{
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, LOW);
}
شرح الكود البرمجي :
تدور المروحة في الإتجاه الأول لمدة 3 ثوان، ثم تتوقف لمدة 3 ثوان. وبعد ذلك، تدور في الاتجاه المعاكس لمدة 3 ثوان ثم تتوقف لمدة 3 ثوان اخرى، وهكذا حتى يتم فصل التيار.
الشرح مفصلا :
يتم ضبط المنافذ الموصله مع المرحل IN1، IN2 كمخرج :
void setup() {
pinMode(MOTOR_IN1, OUTPUT); // set the first pin of the relay as output
pinMode(MOTOR_IN2, OUTPUT); // set the 2nd pin of the relay as output
}
في دالة الـ ()loop ، نقوم باستدعاء الدالة ()motor_forward لتشغيل المروحة مع اتجاه عقارب الساعة. ثم نقوم باستخدام الدالة (delay(3000 لإضافة تأخير زمني مدته 3 ثوان تظل المروحة خلاله تدور في نفس الاتجاه. ثم يتم استخدام الدالة ()motor_stop لإيقاف المروحة عن العمل لمدة 3 ثوان. ثم بإستدعاء الدالة ()motor_reverse نقوم بعكس اتجاه الحركة للمروحة. يتم تكرار هذه العملية حتى يتم فصل التيار.
void loop() {
motor_forward(); // move forward/clockwise
delay(3000); // keep rotating cw for 3 seconds
motor_stop(); // stop rotating
delay(3000); // stand still for 3 seconds
motor_reverse(); // reverse the rotation direction/ccw
delay(3000); // keep rotating ccw for 3 seconds
motor_stop(); // stop rotating
delay(3000); // stand still for 3 seconds
}
دالة ()motor_forward تقوم بتشغيل المروحة مع اتجاه عقارب الساعة. فهي تقوم بجعل أحد الأطراف HIGH والطرف الأخر LOW فتدور المروحة في هذا الإتجاه.
void motor_forward(void) // the function that will cause the motor to rotate cw
{
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
}
تعمل الدالة ()motor_reverse بشكل مشابه للدالة السابقة motor_forward ، ولكن تعكس المخرجات على الأطراف فالطرف الذي تم إخراج قيمة HIGH عليه يتم جعله LOW والطرف الأخر HIGH . فينتج دوران باتجاه معاكس لإتجاه عقارب الساعة.
void motor_reverse(void) // the function that will cause the motor to rotate ccw
{
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, HIGH);
}
تقوم الدالة motor_stop() بإيقاف المروحة تماما عن الحركة، عن طريق جعل كلا الطرفان LOW فلا يصل تيار إلى المروحة فتتوقف.
void motor_stop(void) // the function that will cause the motor to stop rotating
{
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, LOW);
}

