مشكلة لدي بالأردوينو أرجو المساعدة للضرورة القصوى

السلام عليكم ورحمة الله وبركاته

كيف حالكم ؟

واجهتني مشكلة في مشروع باستخدام الأردوينو

أريد عمل نفق عندما يمتلأ بمياه الأمطار يتم إغلاق الطريق المؤدي له ببوابة عن طريق محرك DC

الأدوات لدي حاليا :::

  • ألترا سونيك
  • مقاومة ضوئية
  • بطارية 12 فولت
  • محرك DC
  • أردوينو
  • 4 ريليه

نجحت في جلب الكود وتركيب الدائرة وأيضا في تحريك المحرك بالإتجاهين دون مشاكل
لكن عند إغلاق البوابة
لا تستجيب للمقاومة الضوئية لتغلق المحرك
إلا إذا كان النفق فارغ من الماء ومع ذلك يتوقف ثم يعمل من جديد

الدائرة مأخوذة من هذا الرابط وأيضا الكود مع بعض التعديل مني على الكود لتشغيل الترا سونيك والمقاومة الضوئية ::

http://fritzing.org/projects/relay-control-dc-motor-with-direction-control/

صورة :

الكود :::: :smile:

 /*

  This project consists of one DC motor being controlled via simple serial commands from the PC.


        The DC Motor has 2 wires, simply Positive and Negative.
 When the positive and negative of the dc motor is connected to a power source then the motor will start turing until the power is removed    .  

   A DC motor is usually able to reverse its turning direction by simply switching the positve and negative wires around.
        (Yes,as strange as it sounds... DC motors are able to actually switch the positive and negaitve the other way around to change direcitons)

    So, since we want to control the motor both direcitons, we are using 4 relays in this project.

          Say for instance, we have a DC motor with a green wire and a yellow wire, if we apply positive the green and negative to the yellow it will turn, and if we switch the green and yellow around it will go the other way.

           So lets wire up our relays and motors.

       Relay 1
         NO > Power source positive +
         COM > Green wire on Motor

      Relay VCC pin > Arduino 5V
          Relay IN (or S) pin > Arduino pin 9
         So now this one controls the positive from the power source to the motor's green wire.


        Relay 2
        NO > Power source negative -
          COM > Yellow wire on Motor

             Relay VCC pin > Arduino 5V
        Relay IN (or S) pin > Arduino pin 10

           The purpose of this relay is to control the negative from the power source to the motor

            --- As is, if you turn on relay 1 and relay 2, the motor will start spinnng until you switch the relays off.


          Relay 3
          NO > Power source positive -
         COM > Yellow wire on Motor

           Relay VCC pin > Arduino 5V
         Relay IN (or S) pin > Arduino pin 11

          Relay 4
        NO > Power source negative +
         COM > Green wire on Motor

         Relay VCC pin > Arduino 5V
          Relay IN (or S) pin > Arduino pin 12


           ---
          So now we have 4 Relays.
          If we turn on relay 1 and 2, the motor will spin i.e right
            Then if we turn on relay 3 and 4, the motor will then spin in the opposite direction i.e left

           Note: We cannot put all 4 relays on at the same time, that will cause the wires to heat up and perhaps do some not so pleasant works... ^_^

           For this reason, in the code, when the command 'right' is sent, it first turns relay 3 and 4 off (if its on) and then it turns relay 1 and 2 on.
           Likewise with the 'left' command.


        IMPORTANT Note:
             The relays that were used in the test were some funny kind of ones which were by default On, and if you want to turn the relay Off you have to                digtalWrite HIGH (yeah man..really up side down)
           So to turn it On you have to use digitalWrite LOW ..lol...


            How to control the motor:

              Once the wiring is all done, upload the code and then open the serial monitor             (press CTRL + SHIFT + M) and type in any of the following               commands and press send:

          right

          left

            stop


               Also note: be sure to make the baud rate in the serial monitor to 9600, if it is anything else it is likely not to work
            */


                /// Relay 1 and 2 ///
                          int motorRight = 11; // motor relay right
                   int motorRightGnd = 12; // motor relay left   

         /// Relay 3 and 4 ///
                     int motorLeft = 9; // motor relay right
                    int motorLeftGnd = 10; // motor relay left

                int incomingByte; /// this is the bucket of data thats carrying the serial command from the arduino software/PC to the arduino board

          #define trigPin 13
          #define echoPin 3
                  int sensorPin = A0;
                 int sensorValue = 0;

           void setup() {

               Serial.begin (9600); /// enable serial communication
              /// activate output modes for the motor relays ///
             pinMode (motorRight, OUTPUT);
             pinMode (motorLeft, OUTPUT);

               pinMode (motorRightGnd, OUTPUT);
             pinMode (motorLeftGnd, OUTPUT);


               Serial.flush(); /// don't ask me what this is...kay? i ain't know ^_^

      /// Set all 4 of the relays to be OFF by default, so that it doesn't jus start moving as soon as you power up the arduino and motors.../// 
             digitalWrite (motorRight, HIGH); 
            digitalWrite (motorLeft, HIGH);

             digitalWrite (motorRightGnd, HIGH);
              digitalWrite (motorLeftGnd, HIGH);

             pinMode(trigPin, OUTPUT);
            pinMode(echoPin, INPUT);
                 }

                 void loop() {

           // int sensorValue1 = analogRead(A0);
            // int sensorValue2 = analogRead(A1);
             // Serial.println(sensorValue1);
             // Serial.println(sensorValue2);
              int duration/*, distance*/;
                  digitalWrite(trigPin, LOW);  // Added this line
               // delayMicroseconds(2); // Added this line
                digitalWrite(trigPin, HIGH);
                 //  delayMicroseconds(1000); - Removed this line
                     // delayMicroseconds(10); // Added this line
               digitalWrite(trigPin, LOW);
              duration = pulseIn(echoPin, HIGH);
                Serial.println(duration);
               sensorValue = analogRead(sensorPin);
                // Serial.println(sensorValue);

                String input = "";
               //// Read any serial input
                    while (Serial.available() > 0)
                     {
                    input += (char) Serial.read(); // Read in one char at a time
                // delay(5); /// Delay for 5 ms so the next char has time to be received
                   }


                    if (duration <= 870 )
                    {
                   /// Then Turn the LEFT direction positive and negatve OFF so that it doesnt damage the motor///
                digitalWrite (motorLeft, HIGH);
                  digitalWrite (motorLeftGnd, HIGH);

                    /// Turn the RIGHT direction positive and negatve ON ///

                    digitalWrite (motorRight, LOW);
               digitalWrite (motorRightGnd, LOW);

              }
             /*  if (duration < 870) /// If you send the command 'right' to the serial monitor...
                 {

                       /// Then Turn the LEFT direction positive and negatve OFF so that it doesnt damage the motor///
                  digitalWrite (motorLeft, HIGH);
                   digitalWrite (motorLeftGnd, HIGH);

                   /// Turn the RIGHT direction positive and negatve ON ///

                   digitalWrite (motorRight, LOW);
                    digitalWrite (motorRightGnd, LOW);
  

                        }
                      else if (sensorValue < 300)
                      {

                     /// This command turns all the relays off, resulting in a dead stop to the motor. ///
                        digitalWrite (motorLeft, HIGH); /// turn the right motor's positive+ off
                       digitalWrite (motorLeftGnd, HIGH); /// turn the left motor's ground- off

                       digitalWrite (motorRight, HIGH); /// turn the right motor's positive+ off
                       digitalWrite (motorRightGnd, HIGH); /// turn the right motor's ground- off
   }
                            */
                          /* else if (duration >= 1000)
                          {

                       /// Turn the RIGHT direction positive and negatve OFF so that it doesnt damage the motor///

                           digitalWrite (motorRight, HIGH);
                            digitalWrite (motorRightGnd, HIGH);

                          /// Turn the LEFT direction positive and negatve ON ///
                           digitalWrite (motorLeft, LOW);
                         digitalWrite (motorLeftGnd, LOW);

                              } */


                if (input == "stop")
                  {
                    /// This command turns all the relays off, resulting in a dead stop to the motor. ///
                  digitalWrite (motorLeft, HIGH); /// turn the right motor's positive+ off
                       digitalWrite (motorLeftGnd, HIGH); /// turn the left motor's ground- off

                      digitalWrite (motorRight, HIGH); /// turn the right motor's positive+ off
                         digitalWrite (motorRightGnd, HIGH); /// turn the right motor's ground- off
                       }

                     /* distance = (duration/2) / 29.1;
                    if (distance < 4) {  // This is where the LED On/Off happens
                   digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
                 digitalWrite(led2,LOW);
                     }
                 else {
                 digitalWrite(led,LOW);
             digitalWrite(led2,HIGH);
               }
if (distance >= 200 || distance <= 0){
 Serial.println("Out of range");
}
else {
 Serial.print(distance);
 Serial.println(" cm");
}
              */
    }

@fahad :cry:

في البداية اريد ان اعلق على جملة كتبها المطور

IMPORTANT Note:
The relays that were used in the test were some funny kind of ones which were by default On

كلامة طبعا غير صحيح، اذ في الريلي المستخدم توجد 3 توصيلات في الطرف التوصيل (ليس طرف التحكم) بعذا الشكل:

ففي هذا النوع مثلا يكون الطرف 1 متصل مع 2 بشكل افتراضي (بدون توصيل كهرباء) وعند توصيل الكهرباء قان رقم 2 ينفصل عن 1 ويتصل مع 3
والسبب هو ان الكهرباء يعمل كمغناطيس يقوم بجذب موصل داخلي ويحركة للطرف الاخر وعند ايقاف الكهرباء يقوم زمبرك باعادة الموصل لمكانه الطبيعي
صورة متحركة توضح طريقة عمل الريلي relay

فكل ما عليه هو ان يستخدم 2 و 3 بدلا من استخدام 2 و 1

يمكن معرفة الاطراف المتصلة بشكل افتراضي باستخدام جهاز قياس الطاقة Multimeter

@ahmadkriez الكود لايقوم باستخدام قيمة المقاومة الضوئية ! sensorValue

النص المسؤول عنها معطل

else if (sensorValue < 300)

لقد قمت بتعديلها بالكامل
وقد نجح الأمر

لعلنا نرسل الكود إن شاء الله ونشارك تجربتنا الجميع

طريقة تشغيل والتحكم باتجاء المحرك تسمى h-bridge، وتستخدم في تطبيقات كثيرة خصوصا العاب سيارات التحكم بالريموت rc cars، ولكن بدل الريلي يستخدم 4 ترانزيزتورس وذلك لسرعتهم في الغلق والفتح وقلت استهلاك الطاقة. وتوجد كذلك شرائح الكترونية متخصصة لذلك.
سرعة الغلق والفتح تفيد في التحكم بسرعة المحرك.

توجد دوائر جاهزة للتحكم بالمحركات، تختلف عادة في كمية التيار الذي تستطيع تحملة، مثل