Code not working

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. :blush:


#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.

  1. 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.

  2. Pin 1 (VSS or GND) to GND

  3. Pin 2 (VDD or VCC) to 5V

  4. Pin 3 (V0) to potentiometer center pin

  5. Pin 4 (RS) to Arduino pin 12

  6. Pin 5 (RW) to GND

  7. Pin 6 (E) to Arduino pin 11

  8. Pin 7 (D0) - not used

  9. Pin 8 (D1) - not used

  10. Pin 9 (D2) - not used

  11. Pin 10 (D3) - not used

  12. Pin 11 (D4) to Arduino pin 5

  13. Pin 12 (D5) to Arduino pin 4

  14. Pin 13 (D6) to Arduino pin 3

  15. Pin 14 (D7) to Arduino pin 2

  16. Pin 15 (LED or A) to current limiting resistor to 5V

  17. Pin 16 (LED or K) to GND

  18. Potentiometer

  19. One outer pin to 5V

  20. Center pin to pin 3 (V0) on LCD screen

  21. Other outer pin to GND

  22. LED

  23. Positive (longer) leg to Arduino pin 7

  24. Negative (shorter) leg to current limiting resistor to GND

  25. Buzzer

  26. Positive pin to Arduino pin 8

  27. Negative pin to GND

  28. Pushbutton

  29. One side to 5V

  30. 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).

what does not work?

You have parallel LCD, Stepper Motor, and RTC. What are the purposes of these devices in your project?

Please, post the complete sketch with code tags .

Which Arduino are you using?

  • Please format your complete code. Suggest you place { and } on lines by themselves so we can better read your sketch.

  • Always show us a good schematic of your proposed circuit. Show us good images of your ‘actual’ wiring.

yeah - looking at the code more carefully, it can't even compile... (like the stray #endif in the middle of the code)

Have you posted the real thing?

  • Sorry but this attachment isn’t even close to the way you should be presenting it to us.

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

  • Please read the forum sticky about asking questions on these forums.

All your lines run together, making it impossible to tell comments from code from commented out code.

Clean it up.

Welcome!

Addressing Your Issue:

You can spend weeks spinning your wheels, or you might get lucky and solve your problem quickly. To avoid unnecessary delays, it’s crucial to provide an annotated schematic of your circuit as you have it wired, showing all connections, including power, ground, and power supplies. I recommend it be in English, you can translate before posting if needed.

Why Detailed Information Matters:

Annotated Schematics: These are essential because they show exactly how your circuit is set up. Without them, it's difficult for anyone to understand what you’ve done, which makes troubleshooting nearly impossible. Fritzing diagrams or unclear pictures are not enough. A good schematic can save days in time when problems occur.

Technical Information: Many modules look similar and may even have the same name, but they can function differently. This is why we always ask for links to detailed technical information, not just sales pages like those on Amazon, which often lack the specifics we need.

Show All Connections: It’s important to include every connection, especially power, ground and power sources in your schematic. Missing these details makes it hard to determine if a setup issue might be causing your problem.

Post your Software Without that we do not have a clue as to how it is expected to operate. Be sure to use code tags.

My Process:

When I see a question, I spend a moment assessing it. If it’s missing critical information, I might ask for it. However, if it's repeatedly lacking important details, I may assume the questioner is not serious and move on to another query.

What You Need to Consider:

We don’t know your skill level or what resources you have available. If you’re missing key technical details or seem unprepared, it may indicate that you need to spend more time learning the basics before starting your project.

Providing the right information upfront will help you get the best possible assistance and avoid the frustration of running into dead ends. Let us help you by sharing what you have clearly and completely!

You haven’t really defined the problem yet, and in my experience this particular part often fails due to a hardware issue, not the code. The quickest and most reliable way to diagnose it is to post an annotated schematic not a text description.

Please include:

  1. All connections, including power and ground
  2. All power sources and how they relate to each device
    1. Any wire or lead longer than 25 cm (10")
    • Links or part numbers for the hardware you’re using, including the processor

With a complete schematic, we can help you far more accurately and avoid guessing.

7 posts to ask for fixing the code should probably be enough :)

  • But it’s URGENT

:roll_eyes:

I understand that the OP is under time pressure, but that doesn’t make this an emergency for me. The OP’s lack of response to our questions suggests that the deadline may not be real, which is fine, as I don’t believe this will be completed in time anyway.

It’s also possible the OP is in over their head and the project will fail. Perhaps in the remaining two days a schematic and properly formatted code will appear.

You copied a PDF. Where is the original link? (found it, next post)

PDFs will not "copy" words from first to last. PDFs put words in columns from side to side, column to column.

@morgie_fitz - After a few minutes investigation...

I doubt your claim to your status.

The ?stuff? you posted is meant to be a automated pill dispenser.

The complete project is on-line including hardware list, 3D-prints, wiring diagram, schematic, video, and the un-adulterated code. If following directions on a PDF is too difficult, or ten minutes not scrolling tictac to locate a web site, what will things be like sitting for tests in a STEM? Yes, I'll take fries with my triple-bacon-cheesburger, heavy pickle... and a diet coke, no ice.

Hi, @morgie_fitz

MOST URGENT.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

MOST URGENT.
Can you post some images of your project?
So we can see your component layout.

You should already have the first request as you built your project.

Tom.... :smiley: :+1: :coffee: :australia:

The link I posted is a finished, marketed product, complete with all the things that you mentioned. I would gladly dig out code, schematics, wiring and BOM, and pass them to you on a PM. LMN (that's my version undermining the cliche; LMK).

I did not drag (all the things) out to post here because OP has no urgency, has no interest, and probably none of their posted status.

If the project doesn't work when it is completely assembled then you have to write a number of smaller separate programs to test each stage:

  1. write "Hello World" on the display.
  2. rotate the stepper a number of degrees and back
  3. operate the buzzer for a number of seconds.
  4. set the RTC with the current time and then display the time on the serial monitor.
  5. detect if the button has been pressed and light the led.

Once you have validated the hardware with the above tests then start the integration with the main code.

Given the lack of answers it’s possible that OP is just a bot or fake account. Let’s wait to see if another post pops up later from that account with garbage…

He is real.Bots are much better at formatting.

I will try that, thank you.

Hi,
Just a bit of advice.
When you answer posts that ask for information, please do not go back to previous posts and edit/update them.
Any new info please put in a new post, that way it makes the thread easier to read and follow.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia: