05: microcontroller programming

Tinkering with Arduino

This week we were tasked with building something with an Arduino. I used a few different inputs and outputs but the premise was simple:

  • I'd wave infront of a gesture sensor and the LCD would display "motion detected" and the servo motor would rotate.
  • If I continued waving, the LCD would display "Motion continued"
  • If I stopped waving, the LCD would display "Motion ended" and the servo motor would cease rotating.
  • Equipment

  • 1x Adafruit Metro 328
  • 1x PIR motion sensor HC-SR501
  • 1x LCD 16x2
  • 1x Breadboard trim potentiometer - 10K
  • 1x Micro servo
  • Image
    PIN 5 - SERVO MOTOR
    PIN 7,8,9,10,11,12 - LCD DISPLAY
    V0 OF LCD DISPLAY - MIDDLE TERMINAL OF POTENTIOMETER TO ADJUST SCREEN BRIGHTNESS

    Arduino code

    
    #include "LiquidCrystal.h"
    #include "Servo.h"
    
    Servo myservo; 
    
    LiquidCrystal lcd(7,8,9,10,11,12); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
    int inputPin = 3;               // choose the input pin (for PIR sensor)
    int pirState = LOW;             // we start, assuming no motion detected
    int val = 0;                    // variable for reading the pin status
    int pos = 0;                    // start position for servo motor
    
    void setup() {
      pinMode(inputPin, INPUT);     // declare sensor as input
      lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
      myservo.attach(5);  // attaches the servo on pin 5 to the servo object
      Serial.begin(9600);
    }
     
    void loop(){
      val = digitalRead(inputPin);  // read input value
      if (val == HIGH) {            // check if the input is HIGH
        if (pirState == LOW) {
          Serial.println("Motion detected!");      
          lcd.clear(); // Clears the display 
          lcd.setCursor(0,1); 
          lcd.print("Motion detected!");  // print on LCD display
          for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree
            myservo.write(pos);  
            delay(15);  
            }
          for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            }
          pirState = HIGH;
        }
    
        else {
          Serial.println("Motion continued"); 
          lcd.clear(); // Clears the display 
          lcd.setCursor(0,1); 
          lcd.print("Motion continued!"); // prnit on LCD display
          for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree
            myservo.write(pos);  
            delay(15);  
            }
          for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
            }
          }
          pirState = HIGH;
        }
      
      else {
        if (pirState == HIGH){
          Serial.println("Motion ended!");
          lcd.clear(); // Clears the display 
          lcd.setCursor(0,1); 
          delay(150);// Sets the location at which subsequent text written to the LCD will be displayed 
          lcd.print("Motion ended!");// print on LCD display
          pirState = LOW;
        }
      }
    }