Hi everyone, this is my first time using this forum. I am a second year engineering student at a STEM high school and I have a project showcase on Saturday(Jan 31st). I have been working on a pill dispenser for the last month and I can’t get my code to work. I used the sciencebuddies tutorial code but then edited it for my purposes(they used a servo and I used a stepper and my RTC was external rather than built in). I have asked everyone in my class and they don’t know what’s wrong and neither do the coding teachers. I haven’t gotten any error messages and according to my teachers it should work but for some reason it isn’t. I checked my wiring and that’s all good and I know my libraries are up to date. If anyone has any ideas on what is wrong I would greatly appreciate the help. I have pasted my code below. Thank you so much. ![]()
#include <LiquidCrystal.h>#include <RTClib.h>#include <Stepper.h>int stepsPerRevolution = 2048;int motSpeed = 10;Stepper myStepper(stepsPerRevolution, 13, 15, 14, 16);
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
// time variablesint year;int Month;int dayOfTheWeek;int hour;int minutes;int seconds;
// LED pinsconst int led1 = 11;const int led2 = 12;
// initialize the library by associating any needed LCD interface pin// with the arduino pin number it is connected toconst int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// constant variables for pinsconst int myStepperPin = 6;const int buttonPin = 9;const int ledPin = 7;const int buzzerPin = 8;
// other variables //int angle = 0; //int angleIncrement = 256; // default 45 degrees for 4 compartments, change for different number of compartments //int newAngle;int buttonState;int movementDelay = 50;int debounceDelay = 1000;
// initialize the library by associating any needed LCD interface pin// with the arduino pin number it is connected tovoid updateLCD() { //Serial.println(angle); // print the stepper angle
// detect certain times and rotate the stepper mechanism ahead by one compartment. // the example code rotates the myStepper once per minute when the seconds variable equals 0. // change to detect different times of day, for example (hours == 9 && minutes == 0 && seconds == 0) would detect 9:00:00 AM // use additional "else if" conditions to detect more than one time
if (hour == 10 && minutes == 39 && seconds == 0) { // check for seconds = 0 (one minute intervals) myStepper.step(256); // move the stepper Serial.println(256); // print the angle delay(movementDelay); // delay to slow down movement } //else { // if the new angle is greater than 180, reset angle to 0 //while (angle > 0) { // decrease angle until it reaches 0 //angle = angle - 1; // decrease angle by 1 //myStepper.step(256); // move the stepper //Serial.println(angle); // print the angle //delay(movementDelay); // delay to slow down movement }}}
void flashLED(int numFlashes, int flashDelay) { // alarm function to flash LED and sound buzzer lcd.clear(); // clear the LCD screen lcd.setCursor(0, 0); // set cursor to top left lcd.print("It's time to take your medicine! :) "); // display message for (int i = 0; i < numFlashes; i++) { // loop to flash LED/buzzer numFlashes times digitalWrite(ledPin, HIGH); // turn LED on digitalWrite(buzzerPin, HIGH); // turn buzzer on delay(flashDelay); // delay digitalWrite(ledPin, LOW); // turn LED off digitalWrite(buzzerPin, LOW); // turn buzzer off delay(flashDelay); // delay } // wait for button press - the code will get stuck in this loop until you press the button while (digitalRead(buttonPin) == LOW) {}; delay(1000); // delay before clearing screen lcd.clear(); // clear screen}
void setup() { // setup code that only runs once pinMode(buttonPin, INPUT); // set button pin as input pinMode(ledPin, OUTPUT); // set LED pin as output pinMode(buzzerPin, OUTPUT); // set buzzer pin as output digitalWrite(ledPin, LOW); // make sure LED is off digitalWrite(buzzerPin, LOW); // make sure buzzer is off myStepper.step(0); // set stepper to initial angle Serial.begin(9600); // initialize serial for debugging // LED pin modes pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW);
// set up the LCD's number of columns and rows: lcd.begin(16, 2);#endif
if (!rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (1) delay(10); }
if (rtc.lostPower()) { Serial.println("RTC lost power, let's set the time!"); // When time needs to be set on a new device, or after a power loss, the // following line sets the RTC to the date & time this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); }
// set up the LCD's number of columns and rows: lcd.begin(16, 2);
// initialize the RTC and set the current date and time // you will need to manually adjust the time right before you upload the code rtc.begin();
//DateTime startTime(16, Month::JANUARY, 2026, 9, 09, 20, DayOfWeek::FRIDAY, SaveLight::SAVING_TIME_ACTIVE);
DateTime startTime = rtc.now(); rtc.adjust(startTime);}
void loop() { // code that loops forever updateLCD(); // display the current date and time on the screen (see function below) // get the current time from RTC DateTime now = rtc.now();
// store current time variables year = now.year(); Month = now.month(); dayOfTheWeek = now.dayOfTheWeek(); hour = now.hour(); minutes = now.minute(); seconds = now.second();
// set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 0); // print Month/day/year // add leading spaces if Month or day are less than 10 to keep spacing consistent // (always use 2 characters for Month and day) if (Month < 10) { lcd.print(" "); } lcd.print(Month); lcd.print("/"); if (dayOfTheWeek < 10) { lcd.print(" "); } lcd.print(dayOfTheWeek); lcd.print("/"); lcd.print(year);
// print time in hh:mm:ss format // add leading zeroes if minutes or seconds are less than ten to keep spacing consistent lcd.setCursor(0, 1); if (hour < 10) { lcd.print(" "); } lcd.print(hour); lcd.print(":"); if (minutes < 10) { lcd.print("0"); } lcd.print(minutes); lcd.print(":"); if (seconds < 10) { lcd.print("0"); } lcd.print(seconds);
delay(2000);
// control LEDs based on time // if(seconds == 0){ // digitalWrite(led1,HIGH); // digitalWrite(led2,LOW); // digitalWrite(led3,LOW); // } // if(seconds == 20){ // digitalWrite(led1,LOW); // digitalWrite(led2,HIGH); // digitalWrite(led3,LOW); // } // if(seconds == 40){ // digitalWrite(led1,LOW); // digitalWrite(led2,LOW); // digitalWrite(led3,HIGH); // }}
Edit: I took out the word urgent since it made some people mad, I’m actually really sorry about that. Also, I did read the readme for this but I guess I was in such an anxiety induced state that I forgot what it said, and for that I am also sorry. The reason I “have not looked at any responses yet so I must be a bot,” is because the forum has refused to load on my computer since about an hour after I posted this. In terms of what exactly is wrong is that the stepper motor won’t rotate at the time it is supposed to but the LCD and everything else is lighting up(forgetting to mention that is totally on me, my bad). Additionally, I did not just “not pay attention to the tutorial,” I watched it five times and then referred to the website with the code and circuit for three days. I’m very sorry about everything that made everyone so mad at me, I apologize from the bottom of my heart. I have attached a mockup of my circuit(it is pulled directly from the website). The only edits I have made are the external RTC in GND, 3.3V, A4, and A5, and a stepper motor is used where the servo is.
-
LCD connections. Note that pin labels may vary depending on where you bought your board. Pin numbers go from left to right when viewing the screen horizontally, but from bottom to top in Figure 6 since the screen is rotated.
-
Pin 1 (VSS or GND) to GND
-
Pin 2 (VDD or VCC) to 5V
-
Pin 3 (V0) to potentiometer center pin
-
Pin 4 (RS) to Arduino pin 12
-
Pin 5 (RW) to GND
-
Pin 6 (E) to Arduino pin 11
-
Pin 7 (D0) - not used
-
Pin 8 (D1) - not used
-
Pin 9 (D2) - not used
-
Pin 10 (D3) - not used
-
Pin 11 (D4) to Arduino pin 5
-
Pin 12 (D5) to Arduino pin 4
-
Pin 13 (D6) to Arduino pin 3
-
Pin 14 (D7) to Arduino pin 2
-
Pin 15 (LED or A) to current limiting resistor to 5V
-
Pin 16 (LED or K) to GND
-
Potentiometer
-
One outer pin to 5V
-
Center pin to pin 3 (V0) on LCD screen
-
Other outer pin to GND
-
LED
-
Positive (longer) leg to Arduino pin 7
-
Negative (shorter) leg to current limiting resistor to GND
-
Buzzer
-
Positive pin to Arduino pin 8
-
Negative pin to GND
-
Pushbutton
-
One side to 5V
-
Other side to GND through 10 kΩ pull-down resistor and to Arduino pin 9
This is the circuitry
This is the part I printed with the stepper motor inside(it’s supposed to dispense the medication)
Link to original project:
https://www.sciencebuddies.org/science-fair-projects/project-ideas/Elec_p105/electricity-electronics/automatic-pill-dispenser?ytid=00VXq03n_y0&ytsrc=description
Thank you to everyone who responded, I greatly appreciate your time and effort to try and help me(even though it appears as though I am beyond saving at this point).



