Week 12: Output Devices

Group assignment: group page

Individual assignment:


Individual assignment:

For this week I used 'Servo motor' for the Individual assignment & Group assignment since I went to learn more about it to use it in my final project.


  1. I connect the servo motor to my microcontroller board that I have designed.

  2. I used Arduino IDEto program the servo motor I program it to change the angle from 0 degree to 90 degree to 180 degree.

  3. Servo motor Code:

    #include 
    
    Servo myservo;  // create servo object to control a servo
    // twelve servo objects can be created on most boards
    
    int pos = 0;    // variable to store the servo position
    
    void setup() {
      myservo.attach(D0,540,2400);  // attaches the servo on pin 9 to the servo object
    }
    
    void loop() {
        myservo.write(0);              // tell servo to go to position in variable 'pos'
        delay(1000);                       // waits 15ms for the servo to reach the position
        myservo.write(90);    
        delay(1000);                       // waits 15ms for the servo to reach the position
        myservo.write(180); 
        delay(1000);  
      }
     

    How it's work with the code & the reading from Multimeter & oscilloscope:

    First let's know that the the length of the puls call --> • Pulse width

    • Pulse width: at 0 degree

    • Pulse width: at 90 degree

    • Pulse width: at 180 degree

    as we can see as angle increase the 'Pulse width' increase beacue the time increase when increasing the angle of the servo motor I program it to change the angle from 0 degree to 90 degree to 180 degree, so now we can caulculat the Power need to change the angle.

    Final Project::

    Finaly I used 2 servo motor in my final project to track the sun light by changing the X & Y axis

    Final Project: Servo motor::


    Code: To control all the Output in the Final project.

    #include 
    
    Servo horizontal; // horizontal servo
    int servoh = 180;
    int servohLimitHigh = 175;
    int servohLimitLow = 5;
    
    Servo vertical; // vertical servo
    int servov = 0;
    int servovLimitHigh = 60;
    int servovLimitLow = 0;
    
    // LDR pin connections
    // name = analogpin;
    int ldrlt = A3;
    int ldrrt = A0;
    int ldrld = A2;
    int ldrrd = A1;
    
    const int button1 = 7;
    const int button2 = 6;
    const int motorA = D8;
    const int motorB = D7;
    int buttonStateA;
    int buttonStateB;
    
    int pos = 0;
    int pos2 = 0;
    int oldvalue;
    int oldvalue2;
    
    void setup() {
      horizontal.attach(3);
      vertical.attach(4);
      horizontal.write(180);
      vertical.write(0);
      pinMode(motorA, OUTPUT);
      pinMode(motorB, OUTPUT);
      pinMode(button1, INPUT);
      pinMode(button2, INPUT);
      delay(2500);
    }
    
    void loop() {
      int ldrStatus = analogRead(ldrlt);
      if (ldrStatus > 30) {
        buttonStateA = digitalRead(button1);
        if (buttonStateA == LOW) {
          digitalWrite(motorA, 255); // Counter-clockwise
          digitalWrite(motorB, LOW);
        } else {
          digitalWrite(motorA, LOW);
          digitalWrite(motorB, LOW);
        }
    
        int lt = analogRead(ldrlt);
        int rt = analogRead(ldrrt);
        int ld = analogRead(ldrld);
        int rd = analogRead(ldrrd);
        int dtime = 10;
        int tol = 90; // dtime = difference time, tol = tolerance
        int avt = (lt + rt) / 2; // average value top
        int avd = (ld + rd) / 2; // average value down
        int avl = (lt + ld) / 2; // average value left
        int avr = (rt + rd) / 2; // average value right
        int dvert = avt - avd; // check the difference of up and down
        int dhoriz = avl - avr; // check the difference of left and right
    
        if (-tol > dvert || dvert > tol) {
          if (avt > avd) {
            servov++;
            if (servov > servovLimitHigh) {
              servov = servovLimitHigh;
            }
          } else if (avt < avd) {
            servov--;
            if (servov < servovLimitLow) {
              servov = servovLimitLow;
            }
          }
          vertical.write(servov);
        }
    
        if (-tol > dhoriz || dhoriz > tol) {
          if (avl > avr) {
            servoh--;
            if (servoh < servohLimitLow) {
              servoh = servohLimitLow;
            }
          } else if (avl < avr) {
            servoh++;
            if (servoh > servohLimitHigh) {
              servoh = servohLimitHigh;
            }
          } else if (avl == avr) {
            delay(10);
          }
          horizontal.write(servoh);
        }
        delay(dtime);
      } else {
        oldvalue = horizontal.read();
        oldvalue2 = vertical.read();
        for (pos = oldvalue; pos <= 180; pos += 1) {
          horizontal.write(pos);
          delay(15);
        }
        for (pos2 = oldvalue2; pos2 <= 0; pos2 += 1) {
          vertical.write(pos2);
          delay(15);
        }
        buttonStateB = digitalRead(button2);
        if (buttonStateB == LOW) {
          digitalWrite(motorA, LOW); // Clockwise
          digitalWrite(motorB, 255);
        } else {
          digitalWrite(motorA, LOW);
          digitalWrite(motorB, LOW);
        }
      }
    }

    Group Assignment: