#include <Servo.h>
#define NOTE_C2 65
#define NOTE_E2 82
int melody[] = {NOTE_E2, NOTE_C2};
int noteDurations[] = {4, 4};
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//int motorPinA = 10;
int ledPinGREEN = 3;
int ledPinRED = 2;
int ledPinYELLOW = 1;
int repetitions = 0;
int iterations = 0;
int days = 0;
int remainingA = 0;
int remainingB = 0;
//int motorPinB = 5;
//int motorPinC = 8;
//int servoA;
//int servoB;
//int servoC;
Servo servoA;
Servo servoB;
Servo servoC;
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
//Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int servoPosA = 0; // variable to store the servo position, all sevo positions start at zero, as they should.
int servoPosB = 0;
int servoPosC = 0;
void setup()
{
int ledPinGREEN;
int ledPinRED;
int ledPinYELLOW;
//int motorPin;
//pinMode (motorPinA, OUTPUT);
pinMode(ledPinGREEN, OUTPUT);
pinMode(ledPinRED, OUTPUT);
pinMode(ledPinYELLOW, OUTPUT);
int repetitions;
int iterations;
int days;
int myservo;
//int motorPinB;
//pinMode(motorPinB, OUTPUT);
servoA.attach(5); // attaches the servo (A) on pin 5 to the servo object
servoB.attach(8); // a servo (B) is attached to pin 8
servoC.attach(9); // a servo (C) is attached to pin 9
}
void loop()
{
lcd.init(); // initialize the LCD
// Print a message to the LCD.
lcd.backlight();
lcd.print("remainingA, remainingB"); //make the LCD display the remaining pills in the prescription
for(int days=0;days<40;days++)
{
if(iterations % 2 == 0)
{
analogWrite(ledPinGREEN, 1023);
}
else
{
analogWrite(ledPinRED, 1023);
}
if(iterations >= 2)
{
//analogWrite(motorPinA, LOW);
analogWrite(ledPinYELLOW, 0);
//Servo A needs to go to zero, 0, closed, and stay there
for(servoPosA = 90; servoPosA>=1; servoPosA-=1) // goes from 180 degrees to 0 degrees
{
servoA.write(servoPosA); // tell servo to go to position in variable 'pos'
delay(15);
}
}
else
{
for(int repetitions=0;repetitions<2;repetitions++)
{
delay(500);
analogWrite(ledPinYELLOW, 1000);
delay(500);
//Servo A needs to go to 90 degrees, open
for (int thisNote = 0; thisNote < 2; thisNote++)
{
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
for(servoPosA = 0; servoPosA < 90; servoPosA += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servoA.write(servoPosA); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(servoPosA = 90; servoPosA>=1; servoPosA-=1) // goes from 180 degrees to 0 degrees
{
servoA.write(servoPosA); // tell servo to go to position in variable 'pos'
delay(15);
}
//analogWrite(motorPinA, 70);
delay (500);
analogWrite(ledPinYELLOW, 0);
//servo A needs to be at 0, closed
//analogWrite(motorPinA, LOW);
iterations++; // # value of iterations is counted up by one.
if(iterations % 2 ==1) // this will devide iteraions by two and every time there is a remainder of one (AKA each time there is an odd number, AKA every other iteration, an (#.5 number) because that remainder 1 gets divided by two to make a .5*2 = 1, AKA once per day!)
// This means that we can give her the twice-a-day-pills and then give her the once a day pills! We just need to adjust the timeing to match a 24-hour day!
{
delay(10); //debounce the system
//make servos B and C run for once a day
//analogWrite(motorPinB, 80);
for(servoPosB = 0; servoPosB < 90; servoPosB += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servoB.write(servoPosB); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(servoPosB = 90; servoPosB>=1; servoPosA-=1) // goes from 180 degrees to 0 degrees
{
servoB.write(servoPosB); // tell servo to go to position in variable 'pos'
delay(15);
}
for(servoPosC = 0; servoPosC < 90; servoPosC += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servoA.write(servoPosC); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(servoPosC = 90; servoPosC>=1; servoPosC-=1) // goes from 180 degrees to 0 degrees
{
servoC.write(servoPosC); // tell servo to go to position in variable 'pos'
delay(15);
}
//delay(300); Motor A and Motor B may have a need for different speeds, depending on the size and shape of each of the various pills we dispense. Use this code if this is necessary
//analogWrite(motorPinC, 80);
delay(50); //how long the motors need to run to spit out exactly one pill of each
}
else
{
delay(10); //debounce the system
// leave servos B and C stay off on the even(second) run of servo A, meaning that A gives twice a day while B and C give once a day.
for(servoPosB = 90; servoPosB>=1; servoPosB-=1) // goes from 180 degrees to 0 degrees
{
servoB.write(servoPosB); // tell servo to go to position in variable 'pos'
delay(15);
}
for(servoPosC = 90; servoPosC>=1; servoPosC-=1) // goes from 180 degrees to 0 degrees
{
servoC.write(servoPosC); // tell servo to go to position in variable 'pos'
delay(15);
}
//analogWrite(motorPinB, LOW); //this is a pill that is given only once a day, motor left off
//analogWrite(motorPinC, LOW); //this is a pill that is given only once a day, motor left off
}
}
}
days++;
}
}
int remainingA (int days, int iterations)
{
return days*2 - iterations;
}
int remainingB (int remainingA)
{
int remainingB = remainingA/2;
return remainingB;
}