uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

door.ino (1974B)


      1 #include <Encoder.h>
      2 
      3 #define PIN_ENC_CHAN1 2
      4 #define PIN_ENC_CHAN2 3
      5 #define PIN_L293D_IN 4
      6 #define PIN_L293D_OUT 5
      7 #define PIN_ULTRASONIC_ECHO 6
      8 #define PIN_ULTRASONIC_TRIGGER 7
      9 #define PIN_DCMOTOR 9
     10 #define PIN_PIR 10
     11 #define PIN_LED 13
     12 #define PIN_TEMP A0
     13 
     14 static Encoder enc(PIN_ENC_CHAN1, PIN_ENC_CHAN2);
     15 
     16 void
     17 setup()
     18 {
     19 	pinMode(PIN_ENC_CHAN1, INPUT);
     20 	pinMode(PIN_ENC_CHAN2, INPUT);
     21 	pinMode(PIN_L293D_IN, OUTPUT);
     22 	pinMode(PIN_L293D_OUT, OUTPUT);
     23 	pinMode(PIN_DCMOTOR, OUTPUT);
     24 	pinMode(PIN_PIR, INPUT);
     25 	pinMode(PIN_LED, OUTPUT);
     26 	pinMode(PIN_ULTRASONIC_ECHO, INPUT);
     27 	pinMode(PIN_ULTRASONIC_TRIGGER, OUTPUT);
     28 	Serial.begin(9600);
     29 }
     30 
     31 void
     32 loop()
     33 {
     34 	if (measure_distance() <= 40)
     35 		open_door();
     36 	if (measure_temp() > 20)
     37 		fan();
     38 	if (digitalRead(PIN_PIR) == HIGH)
     39 		digitalWrite(PIN_LED, HIGH);
     40 	else
     41 		digitalWrite(PIN_LED, LOW);
     42 
     43 	delay(10);
     44 }
     45 
     46 int
     47 measure_distance()
     48 {
     49 	long duration;
     50 	int distance;
     51 
     52 	digitalWrite(PIN_ULTRASONIC_TRIGGER, LOW);
     53 	delayMicroseconds(2);
     54 	digitalWrite(PIN_ULTRASONIC_TRIGGER, HIGH);
     55 	delayMicroseconds(10);
     56 	digitalWrite(PIN_ULTRASONIC_TRIGGER, LOW);
     57 
     58 	duration = pulseIn(PIN_ULTRASONIC_ECHO, HIGH);
     59 	distance = (float)duration * 0.344 / 20;
     60 
     61 	Serial.print("Distance: ");
     62 	Serial.print(distance);
     63 	Serial.println(" cm");
     64 
     65 	delay(100);
     66 
     67 	return (distance);
     68 }
     69 
     70 void
     71 open_door()
     72 {
     73 	long pos;
     74 	int rot;
     75 
     76 	analogWrite(PIN_L293D_IN, 30);
     77 	analogWrite(PIN_L293D_OUT, 0);
     78 	pos = enc.read() / 10;
     79 	rot = abs(pos) / 10;
     80 
     81 	Serial.print("Encoder position: ");
     82 	Serial.println(pos);
     83 	Serial.print("Encoder rotation: ");
     84 	Serial.println(rot);
     85 }
     86 
     87 float
     88 measure_temp()
     89 {
     90 	float temp;
     91 	int adc;
     92 
     93 	adc = analogRead(PIN_TEMP);
     94 	temp = (float)adc * (5000 / 1024.0);
     95 	temp = (temp - 500) / 10;
     96 
     97 	Serial.print("Temperatue: ");
     98 	Serial.print(temp);
     99 	Serial.println(" C");
    100 
    101 	delay(100);
    102 
    103 	return (temp);
    104 }
    105 
    106 void
    107 fan()
    108 {
    109 	int v;
    110 
    111 	for (v = 0; v <= 255; v += 5)
    112 		analogWrite(PIN_DCMOTOR, v);
    113 	for (v = 255; v >= 0; v -= 5)
    114 		analogWrite(PIN_DCMOTOR, v);
    115 }