Welcome to my PS70 portfolio! Here you will find weekly progress of my digital fabrication journey.
This week we were tasked with building something with an Arduino. I used a few different inputs and outputs but the premise was simple:
#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;
}
}
}