-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
83 lines (70 loc) · 1.84 KB
/
main.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//LIBRARIES
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
//Declare servo variables
Servo servo1;
Servo servo2;
//declare ultrasonic sensor pins
const int pingPin = 4;
const int echoPin = 5;
//declare an EEPROM value
int left = 118;
//declare LCD pins (Liquid Crystal Display)
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() //runs once on initialize (startup)
{
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
servo1.attach(7); //declare signal pin servo1
servo2.attach(8); //declare singal pin servo2
Serial.begin(9600); //initliaze serial monitor (9600 because baud rate)
left = EEPROM.read(0); //read what's on bit one for EEPROM
EEPROM.write(0,left); //saves the current value for left at bit 1 (0)
}
void servoRotate() {
for(int i = 0; i < 180; i++) {
servo1.write(i);
servo2.write(180-i);
delay(10);
lcd.setCursor(3, 0);
}
}
void loop()
{
lcd.clear();
lcd.print("Dispenses: ");
lcd.print(left);
delay(500);
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
if (inches <= 2) {
Serial.println("DISPENSING");
servoRotate();
delay(10);
left = --left;
EEPROM.write(0,left);
}
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}